Finding an Element with a Particular Key

The key() function, defined in the XSLT Recommendation, obtains the node whose key value matches the specified key. The format is

node-set key(string, object)
               

            

The first argument specifies the name of the key. The value of this argument must be a qualified name. The second argument specifies the node or nodes to examine. When the second argument is a node set, the result is the union of the results of applying the key() function to the string value of each of the nodes in the set. When the second argument is any other type, the XPath processor converts the argument to a string, as if by a call to the string() function. The key() function returns a node set that contains the nodes in the same document as the context node that have a value for the named key that is equal to this string.

For example, the videos.xml document, which is in the examples directory of the Stylus Studio installation directory, contains the following elements:

<result> 
               

              
<actors> 
                   
<actor id="00000003">Jones, Tommy Lee</actor> 
                   
... 
                   
</actors> 
                   
<videos> 
                   
<video id="id1235AA0"> 
                   
<title>The Fugitive</title> 
                   
... 
                   
<actorRef>00000003</actorRef> 
                   
<actorRef>00000006</actorRef> 
                   
... 
                   
</video> 
                   
... 
                   
</videos> 
                   

                
</result>

When you display information about a video in a Web browser, you want to display the names of the actors. Because the actors are referenced only by an ID number, you create a key table in your stylesheet:

<xsl:key 
               

              
name="actors" 
                   
match="/result/actors/actor" 
                   
use="@id"/>
                   

                

This indexes all actors by their ID. To process a video, your stylesheet specifies the following:

<xsl:for-each select="actorRef"> 
               

              
<xsl:value-of select="key('actors', .)"/> 
                   

                
</xsl:for-each>

This instructs the XPath processor to look up the actor element in the actors key table by using the actorRef element as a key.

Data Integration Tools: Using the Stylus Studio XML Pipeline to Visually Integrate and Publish Data

Data Integration Tools from Stylus Studio include a visual pipeline editor, support for non-XML data sources, scalable data integration components, cross-language debugging and Java code generation. Try it now.

Forest & Paper Products

Many forest & paper product companies realize the best XML suite doesn't have "pay per" features. That's why these top forest & paper product companies choose Stylus Studio 2006 to get the world's best XML features all in one bundle!

XML Unicode Encoding

Stylus Studio supports over 300 different file encodings, including UTF-8, UTF-16, EUC-JP, SHIFT-JIS, and more, alowing you to work with any XML document. Robust text editors support standard editing features, and Sense:X intelligent auto-completion.

EDI Mapping

To transform EDI to XML, whether for X12 or EDIFACT, Stylus Studio provides the design tools and run-time support. EDIFACT, X12 and custom EDI formats can be translated, validated and mapped using the EDI Mapping adapter technology described here.

Stylus Most Wanted

 
Free Stylus Studio XML Training:
W3C Member