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

Descendant-or-self and ancestor-or-self axes

The "descendant-or-self::" axis contains all descendants of a node and the node itself. Similarly, "ancestor-or-self::" contains all ancestors of the node and the node itself.

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>
                        <i>
                              <xsl:apply-templates  select="aaa/descendant::*"/>
                        </i>
                        <ii>
                              <xsl:apply-templates  select="aaa/descendant-or-self::*"/>
                        </ii>
                  </xxx>
                  <yyy>
                        <i>
                              <xsl:apply-templates  select="aaa/bbb/ccc/ddd/ancestor::*"/>
                        </i>
                        <ii>
                              <xsl:apply-templates  select="aaa/bbb/ccc/ddd/ancestor-or-self::*"/>
                        </ii>
                  </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>
            <i>
                  <iii  name="bbb"
                              position="1"/>
                  <iii  name="ccc"
                              position="2"/>
                  <iii  name="ddd"
                              position="3"/>
            </i>
            <ii>
                  <iii  name="aaa"
                              position="1"/>
                  <iii  name="bbb"
                              position="2"/>
                  <iii  name="ccc"
                              position="3"/>
                  <iii  name="ddd"
                              position="4"/>
            </ii>
      </xxx>
      <yyy>
            <i>
                  <iii  name="aaa"
                              position="1"/>
                  <iii  name="bbb"
                              position="2"/>
                  <iii  name="ccc"
                              position="3"/>
            </i>
            <ii>
                  <iii  name="aaa"
                              position="1"/>
                  <iii  name="bbb"
                              position="2"/>
                  <iii  name="ccc"
                              position="3"/>
                  <iii  name="ddd"
                              position="4"/>
            </ii>
      </yyy>


Previous chapter: Sorting
Next chapter: Named-Templates
Previous page: Self axis
Next page: - - -