Previous | Next | Indexes
Zvon > Tutorials > XSLT 2.0 Tutorial > Axes (3/8) >

Descendant and ancestor axes

The axis "descendant::" includes all children of a node, children of these children, children of children of children and so on.

The axis "ancestor::" includes parent of a node, parent of the parent, parent of parent of parent, and so on.

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:apply-templates  select="aaa/descendant::*"/>
                  </xxx>
                  <yyy>
                        <xsl:apply-templates  select="aaa/bbb/ccc/ddd/ancestor::*"/>
                  </yyy>
            </xsl:template>

            <xsl:template  match="*">
                  <iii  name="{name(.)}"
                              position="{position()}"/>
            </xsl:template>

      </xsl:stylesheet>
XML

      <aaa>
            <bbb>
                  <ccc>
                        <ddd/>
                  </ccc>
            </bbb>
      </aaa>
Output

      <xxx>
            <iii  name="bbb"
                        position="1"/>
            <iii  name="ccc"
                        position="2"/>
            <iii  name="ddd"
                        position="3"/>
      </xxx>
      <yyy>
            <iii  name="aaa"
                        position="1"/>
            <iii  name="bbb"
                        position="2"/>
            <iii  name="ccc"
                        position="3"/>
      </yyy>


Previous chapter: Sorting
Next chapter: Named-Templates
Previous page: Child and parent axes
Next page: Axes preceding and following