Freigeben über


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".

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

Reference

key Function
id Function