11.2 More Than One Key

You can, of course, declare and invoke more than one key in a stylesheet. keys.xsl makes use of two keys, each using a different expression to find a value:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="text"/> <xsl:key name="State" match="state" use="name"/> <xsl:key name="UN" match="state" use="@cc"/>     <xsl:template match="/">  <xsl:value-of select="key('State', 'Germany')/name"/>  <xsl:text> </xsl:text>  <xsl:value-of select="key('UN', 'de')/admitted"/> </xsl:template>     </xsl:stylesheet>

The declared key named State helps find a node that matches the text nodes in state elements, such as Germany; the key named UN finds a node that matches the value of cc attributes, such as de.

Apply the stylesheet keys.xsl to un.xml with:

xalan un.xml keys.xsl

and you get this output:

Germany 18 Sep. 1973

It may seem odd that the call to key( ) in the first value-of is followed by the path /name because the key itself matches a name element. If you leave off the path /name, the pattern in the match attribute of the key declaration will be matched, and all children of the matched node will be processed, not just the name node. Adding the location step /name at the end of the key( ) function call refines what gets returned by the expression.



Learning XSLT
Learning XSLT
ISBN: 0596003277
EAN: 2147483647
Year: 2003
Pages: 164

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net