xsl-list
[Top] [All Lists]

RE: getting position of tag

2004-09-15 07:46:23

Hi.

I have this problem:

XML:
<columns>
      <column>
              <source>A</source>
      </column>
      <column>
              <source>B</source>
      </column>
</xolumns>

I want to get position() of tag column, where source='B'.

I have tried
xsl:value-of select="/columns/column[source='B']/position()"
but it is bad.

The position() function will give you the position of the node within
the current selection of nodes that are being iterated over.  So to use
the position function you must first select the nodes, then get the
position() of the node that matches your critera:

  <xsl:for-each select="/columns/column">
    <xsl:if test="source = 'B'">
      <xsl:value-of select="position()"/>
    </xsl:if>
  </xsl:for-each>

Alternatively, if you haven't selected any nodes, you will need to use
the count() function to count how many preceding <column> elements there
are from the current node:

  <xsl:template match="column">
    <xsl:if test="source = 'B'">
      <xsl:value-of select="count(preceding-sibling::column|.)" />
    </xsl:if>
  </xsl:template>

cheers
andrew


<Prev in Thread] Current Thread [Next in Thread>