Previous | Next | Indexes
Zvon > Tutorials > XSLT 2.0 Tutorial > Keys (2/12) >

Key matching based on element context

In this example the xsl:key "kkk" matches all "bbb" elements in the source document. The function key() selects all "bbb" elements which have its contents equal to 'Y' (determined by use).

XSLT

      <xsl:stylesheet
                  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                  version="2.0">
            <xsl:output  method="xml"
                        indent="yes"
                        omit-xml-declaration="yes"/>
            <xsl:key  name="kkk"
                        match="bbb"
                        use="."/>

            <xsl:template  match="/aaa">
                  <xsl:apply-templates  select="key('kkk','Y')"/>
            </xsl:template>

            <xsl:template  match="bbb">
                  <uuu>
                        <xsl:copy-of  select="."/>
                  </uuu>
            </xsl:template>

      </xsl:stylesheet>
XML

      <aaa>
            <bbb  x="a">X</bbb>
            <ccc>
                  <bbb  x="b">Y</bbb>
            </ccc>
            <ddd>
                  <eee>
                        <bbb  x="c">Z</bbb>
                  </eee>
            </ddd>
      </aaa>
Output

      <uuu>
            <bbb  x="b">Y</bbb>
      </uuu>


Previous chapter: Namespaces
Next chapter: Recursions
Previous page: Key instruction and key function.
Next page: Keys usage for cross-referencing