only for RuBoard |
xsl:key defines a named key for a node or set of nodes that are accessed through the XSLT key() function. Keys are comprised of three components : the node(s) referenced by the key, the name of the key, and the value of the key. Keys can be useful for lookup information or temporarily caching values for later use. Keys can also help avoid complex queries for decendants of ancestors .
The key function does for keys what the id function does for IDs. The first argument specifies the name of the key. The value of the argument must be a QName , which is expanded. When the second argument to the key function is of type node-set, the result is the union of the result of applying the key function to the string value of each of the nodes in the argument node-set. When the second argument to key is of any other type, the argument is converted to a string as if by a call to the string function; it returns a node-set that contains the nodes in the same document as the context node that has a value for the named key equal to this string.
Attribute Name | Enforced | Description | Values |
---|---|---|---|
Name | Required | The name of the key | QName |
Match | Required | The pattern that matches the key | Pattern |
Use | Required | Indicates the value of the key | Expression |
Suppose that you have a list of keywords that you'd like to provide a hyperlink to each time the keyword is referenced. Instead of including the definition as a child element for each instance of the keyword, you can separate the references to the keywords and the definitions themselves . Consider the following XML instance document, definitions.xml :
<?xml version="1.0" encoding="utf-8" ?> <root> <p> This document explains some of the relationships between <keyword>XML</keyword>, and <keyword>XSLT</keyword>. <keyword>HTML</keyword> is familiar to most people. <keyword>XSLT</keyword> is a growing technology that is rapidly gaining interest. </p> <defs> <def name="XML">Extensible Markup Language</def> <def name="XSLT">Extensible Stylesheet Language Transformations</def> <def name="HTML">Hypertext Markup Language</def> </defs> </root>
The following stylesheet, defs.xsl , generates hyperlinks between instances of keywords in text and the definitions of the keywords:
<?xml version="1.0" encoding="UTF-8" ?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="html"/> <xsl:key name="definition" match="def" use="@name" /> <xsl:template match="/"> <html> <body> <xsl:apply-templates /> </body> </html> </xsl:template> <xsl:template match="p"> <xsl:apply-templates /> </xsl:template> <xsl:template match="keyword"> <b> <a href="#{generate-id(key('definition',.))}"> <xsl:apply-templates /> </a> </b> </xsl:template> <xsl:template match="def"> <p> <a name="{generate-id()}"> <b> <xsl:value-of select="@name" /> </b> <xsl:text> - </xsl:text> <xsl:value-of select="." /> </a> </p> </xsl:template> </xsl:stylesheet>
The result of this transformation is as follows :
<html> <body> This document explains some of the relationships between <b><a href="#IDA3TZCB">XML</ a></b>, and <b><a href="#IDAAUZCB">XSLT</a></b>. <b><a href="#IDADUZCB">HTML</a></b> is familiar to most people. <b><a href="#IDAAUZCB">XSLT</a></b> is a growing technology that is rapidly gaining interest. <p><a name="IDA3TZCB"><b>XML</b> - Extensible Markup Language</a></p> <p><a name="IDAAUZCB"><b>XSLT</b> - Extensible Stylesheet Language Transformations</a></ p> <p><a name="IDADUZCB"><b>HTML</b> - Hypertext Markup Language</a></p> </body> </html>
xsl:stylesheet or xsl:transform
None.
only for RuBoard |