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

Axes preceding-sibling and following-sibling

The "preceding-sibling::" axis of an element contains all elements which precedes the element in the document order and which share parent with the element.

The "following-sibling::" axis of an element contains all elements which follows the element in the document order and which share parent with the element.

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/ccc-b-3/preceding-sibling::*"/>
                  </xxx>
                  <yyy>
                        <xsl:apply-templates  select="/aaa/ccc/ccc-b/ccc-b-3/following-sibling::*"/>
                  </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-3/>
                        <ccc-b-4/>
                        <ccc-b-5/>
                  </ccc-b>
                  <ccc-c/>
            </ccc>
            <ddd>
                  <ddd-a/>
                  <ddd-b/>
            </ddd>
      </aaa>
Output

      <xxx>
            <zzz  name="ccc-b-1"
                        position="1"/>
            <zzz  name="ccc-b-2"
                        position="2"/>
      </xxx>
      <yyy>
            <zzz  name="ccc-b-4"
                        position="1"/>
            <zzz  name="ccc-b-5"
                        position="2"/>
      </yyy>


Previous chapter: Sorting
Next chapter: Named-Templates
Previous page: Axes preceding and following
Next page: Attribute axis