RE: Understanding xsl:key
Heiner, >> <xsl:key name="test" match="title" use="substring-before(name, ' ')"/> >> will create >> node key value >> title 'Design' >> title 'Pattern' >> title 'Building' >> >> I assume though I haven't tried it. > >I have done something similar, which was working fine. But I wanted >to know how to do it with the key() function, and that's what I don't >get working. The key function is just a simple look up - you give it a key value, it tells you the nodes that are associated with that value. There isn't any functionality in keys that allow you to look at all the key values and find those that fulfill a certain pattern. In your 'Pattern' example, say, to get the functionality that you want, the processor would have to go through every single key value in the key, check whether it contained the string 'Pattern', and then return those nodes that have a key value that fulfill that test. This would probably be a lot more complicated for the processor, certainly take a longer time. Which isn't to say that it wouldn't be useful. In particular, we have to jump through quite a lot of hoops to find out what the unique key values are when doing grouping - a function that gave a list of all the key values would be really helpful in doing this quickly. If you know that the only titles that you're going to be interested in are those containing the word 'Pattern', then you can hard code this into your stylesheet with: <xsl:key name="pattern-titles" match="title" use="contains(., 'Pattern')" /> This will give you a key that has two possible key values, 'true' and 'false'. If you then do key('pattern-titles', 'true'), then you will get a list of those titles that contain 'Pattern'. Obviously this technique is useless if the search string you're interested in changes each time you run the stylesheet. The other thing that you can do (my thanks to Mike Kay's book for pointing this possibility out) is assign each title multiple key values within the same key space. So you can do: <xsl:key name="test" match="title" use="." /> <xsl:key name="test" match="title" use="substring-before(., ' ')" /> <xsl:key name="test" match="title" use="substring-after(., ' ')" /> If all your titles are less than two words long, then this will allow you to do: key('test', 'Pattern') and get all the 'title' elements whose title is 'Pattern' or whose first word is 'Pattern' or whose last word (in a two-word title) is 'Pattern'. You can add extra keys to your heart's content in order to break down the title further: <xsl:key name="test" match="title" use="substring-before(substring-after(., ' '), ' '))" /> <xsl:key name="test" match="title" use="substring-before(substring-after(substring-after(., ' '), ' '), ' ')" /> ... but it is a bit laborious! :) I hope that this helps, anyway, Jeni Jeni Tennison http://www.jenitennison.com/ XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
PURCHASE STYLUS STUDIO ONLINE TODAY!
Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!
Download The World's Best XML IDE!
Accelerate XML development with our award-winning XML IDE - Download a free trial today!
Subscribe in XML format