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

Axes preceding and following

The "preceding::" axis of an element contains all elements which precedes the element in the document order and which are not its ancestors.

The "following::" axis of an element contains all elements which follows the element in the document order and which are not its descendants.

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/ccc/ccc-b/preceding::*"/>
                  </xxx>
                  <yyy>
                        <xsl:apply-templates  select="/aaa/ccc/ccc-b/following::*"/>
                  </yyy>
            </xsl:template>

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

      </xsl:stylesheet>
XML

      <aaa>
            <bbb>
                  <bbb-a/>
                  <bbb-b/>
            </bbb>
            <ccc>
                  <ccc-a/>
                  <ccc-b>
                        <ccc-b-1/>
                        <ccc-b-2/>
                  </ccc-b>
                  <ccc-c/>
            </ccc>
            <ddd>
                  <ddd-a/>
                  <ddd-b/>
            </ddd>
      </aaa>
Output

      <xxx>
            <zzz  name="bbb"
                        position="1"/>
            <zzz  name="bbb-a"
                        position="2"/>
            <zzz  name="bbb-b"
                        position="3"/>
            <zzz  name="ccc-a"
                        position="4"/>
      </xxx>
      <yyy>
            <zzz  name="ccc-c"
                        position="1"/>
            <zzz  name="ddd"
                        position="2"/>
            <zzz  name="ddd-a"
                        position="3"/>
            <zzz  name="ddd-b"
                        position="4"/>
      </yyy>


Previous chapter: Sorting
Next chapter: Named-Templates
Previous page: Descendant and ancestor axes
Next page: Axes preceding-sibling and following-sibling