Previous | Next | Indexes
Zvon > Tutorials > XSLT 2.0 Tutorial > Templates - basic usage (7/12) >

Current position

The function position() gives position of the item in currently processed sequence.

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="/">
                  <xsl:apply-templates  select="aaa"/>
            </xsl:template>

            <xsl:template  match="aaa">
                  <xxx>
                        <xsl:apply-templates  select="bbb"/>
                  </xxx>
                  <yyy>
                        <xsl:apply-templates  select="bbb[position()>2]"/>
                  </yyy>
            </xsl:template>

            <xsl:template  match="bbb">
                  <zzz>
                        <xsl:value-of  select="position()"/>
                        <xsl:text>. </xsl:text>
                        <xsl:value-of  select="."/>
                  </zzz>
            </xsl:template>

      </xsl:stylesheet>
XML

      <aaa>
            <bbb>A</bbb>
            <bbb>B</bbb>
            <bbb>C</bbb>
            <bbb>D</bbb>
      </aaa>
Output

      <xxx>
            <zzz>1. A</zzz>
            <zzz>2. B</zzz>
            <zzz>3. C</zzz>
            <zzz>4. D</zzz>
      </xxx>
      <yyy>
            <zzz>1. C</zzz>
            <zzz>2. D</zzz>
      </yyy>


Previous chapter: - - -
Next chapter: Sequences
Previous page: Element xsl:apply-templates
Next page: Shared templates for several elements