Previous | Next | Indexes
Zvon > Tutorials > XSLT 2.0 Tutorial > Recursions (2/6) >

Recursive xsl:apply-templates

Recursive structures are common in XSLT. In this example there is just one xsl:template with select equal to "*". But it achieves a lot, the program lists names of all elements in the source document.

Please note that the xsl:apply-templates calls the same xsl:template where it resides and so it is a typical recursive behavior.

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="*">
                  <xxx>
                        <xsl:value-of  select="name()"/>
                  </xxx>
                  <xsl:apply-templates  select="*"/>
            </xsl:template>

      </xsl:stylesheet>
XML

      <aaa>
            <bbb>
                  <ccc/>
                  <ddd/>
            </bbb>
            <eee>
                  <fff>
                        <ggg/>
                  </fff>
                  <hhh/>
            </eee>
      </aaa>
Output

      <xxx>aaa</xxx>
      <xxx>bbb</xxx>
      <xxx>ccc</xxx>
      <xxx>ddd</xxx>
      <xxx>eee</xxx>
      <xxx>fff</xxx>
      <xxx>ggg</xxx>
      <xxx>hhh</xxx>


Previous chapter: Keys
Next chapter: Date and Time
Previous page: Recursion
Next page: Recursive renaming of elements