Previous | Next | Indexes
Zvon > Tutorials > XSLT 2.0 Tutorial > Variables and Parameters (7/14) >

Nodes as variable values

An XML tree can be set as a value of a variable. The name of variable is now the root of the XML tree (like "/" is for an XML document).

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:template  match="/aaa">
                  <xsl:variable  name="xxx">
                        <ooo>
                              <ppp  r="1"/>
                        </ooo>
                  </xsl:variable>
                  <xxx>
                        <xsl:value-of  select="name($xxx)"/>
                  </xxx>
                  <yyy>
                        <xsl:value-of  select="name($xxx/*)"/>
                  </yyy>
                  <zzz>
                        <xsl:value-of  select="$xxx/ooo/ppp/@r"/>
                  </zzz>
                  <sss>
                        <xsl:copy-of  select="$xxx"/>
                  </sss>
            </xsl:template>

      </xsl:stylesheet>
XML

      <aaa/>
Output

      <xxx/>
      <yyy>ooo</yyy>
      <zzz>1</zzz>
      <sss>
            <ooo>
                  <ppp  r="1"/>
            </ooo>
      </sss>


Previous chapter: Named-Templates
Next chapter: Stylesheet Functions
Previous page: Shadowing local variables
Next page: Storing parts of XML document to a variable