Partager via


Exemple de <xsl:key>

Cet exemple définit une clé appelée "title-search" qui localise les éléments book ayant un attribut author égal à "David Perry".

Fichier XML (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>

Fichier XSLT (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>

Sortie

Voici les données en sortie formatées :

XML Aujourd'hui

XML et Microsoft

Voici les données en sortie du processeur :

<HTML>

<BODY>

<div>XML Today</div>

<div>XML and Microsoft</div>

</BODY>

</HTML>

Voir aussi

Référence

Fonction key
Fonction id