Example of <xsl:key>

MSXML 5.0 SDK

Microsoft XML Core Services (MSXML) 5.0 for Microsoft Office - XSLT Reference

Example of <xsl:key>

This example defines a key named "title-search" that locates book elements that have an author attribute equal to "David Perry".

Note To test this example, you need to use a script. For more information, see Initiate XSLT in a Script.

XML File (titles.xml)

<?xml version='1.0'?>
<?xml-stylesheet type="text/xsl" href="key_sample.xsl" ?>
<titles>
   <book title="XML Today" author="David Perry"/>
   <book title="XML and Microsoft" author="David Perry"/>
   <book title="XML Productivity" author="Jim Kim"/>
</titles>

XSLT File (key_sample.xsl)

<?xml version='1.0'?>
<xsl:stylesheet version="1.0"
      xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >

<xsl:key name="title-search" match="book" use="@author"/>

<xsl:template match="/">
   <HTML>
      <BODY>
        <xsl:for-each select="key('title-search', 'David Perry')">
         <div>
         <xsl:value-of select="@title"/>
         </div>
      </xsl:for-each>
      </BODY>
   </HTML>
</xsl:template>

</xsl:stylesheet>

Output

This is the formatted output:

XML Today

XML and Microsoft

This is the processor output:

<HTML>
<BODY>
<div>XML Today</div>
<div>XML and Microsoft</div>
</BODY>
</HTML>

See Also

key Function | id Function