Previous | Next | Indexes
adultinternetusers > Tutorials > XSLT 2.0 Tutorial > Keys (11/12) >

A useful example of context change with the third argument of key()

This example demonstrate a common case where the third argument comes handy. The keys are supplied as a nodes inside xsl:variable and the xsl:for-each element has this variable as its root node. The first key() function therefore does not match anything as the source document is outside the scope of xsl:for-each. The second key() uses its third argument to use the source document for its work.

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="ccc"
                        use="@c"/>
            <xsl:variable  name="xxx">
                  <k>Y</k>
                  <k>Z</k>
                  <k>X</k>
            </xsl:variable>
            <xsl:variable  name="root"
                        select="/"/>

            <xsl:template  match="/aaa">
                  <xsl:for-each  select="$xxx/*">
                        <key  id="{.}">
                              <xxx>
                                    <xsl:apply-templates  select="key('kkk',.)"/>
                              </xxx>
                              <yyy>
                                    <xsl:apply-templates  select="key('kkk',.,$root)"/>
                              </yyy>
                        </key>
                  </xsl:for-each>
            </xsl:template>

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

      </xsl:stylesheet>
XML

      <aaa>
            <bbb  id="b1">
                  <ccc  c="X">1</ccc>
                  <ccc  c="Y">2</ccc>
                  <ddd>
                        <ccc  c="Z">3</ccc>
                  </ddd>
            </bbb>
            <bbb  id="b2">
                  <eee>
                        <fff>
                              <ccc  c="X">4</ccc>
                        </fff>
                  </eee>
            </bbb>
      </aaa>
Output

      <key  id="Y">
            <xxx/>
            <yyy>
                  <ccc  c="Y">2</ccc>
            </yyy>
      </key>
      <key  id="Z">
            <xxx/>
            <yyy>
                  <ccc  c="Z">3</ccc>
            </yyy>
      </key>
      <key  id="X">
            <xxx/>
            <yyy>
                  <ccc  c="X">1</ccc>
                  <ccc  c="X">4</ccc>
            </yyy>
      </key>


Previous chapter: Namespaces
Next chapter: Recursions
Previous page: Using keys in variables or parameters
Next page: Function key inside an XPath expression

Kevin Carr in Stanton

Natural Skin Care and European Soaps
Kevin Carr
Mayor Dave Shawver Stanton
Internetusers


You can also get Organic Skin Care products from Bliss Bath Body and you must check out their Natural Body Lotions and bath soaps

Now if you are looking for the best deals on surf clothing from Quiksilver and Roxy then you have to check these amazing deals here:

Hey, check out this Organic Skin Care European Soaps along with Natural Lavender Body Lotion and shea butter

and we can get surf t shirts surfing shirt and And you must check out this website swim suit swimming suit and swim trunks