Find the position of an element within its parent with XSLT / XPath

Lukas Eder picture Lukas Eder · Jul 20, 2011 · Viewed 30k times · Source

Apart from rewriting a lot of XSLT code (which I'm not going to do), is there a way to find the position of an element within its parent, when the context is arbitrarily set to something else? Here's an example:

<!-- Here are my records-->
<xsl:for-each select="/path/to/record">
  <xsl:variable name="record" select="."/>

  <!-- At this point, I could use position() -->
  <!-- Set the context to the current record -->
  <xsl:for-each select="$record">

    <!-- At this point, position() is meaningless because it's always 1 -->
    <xsl:call-template name="SomeTemplate"/>
  </xsl:for-each>
</xsl:for-each>


<!-- This template expects the current context being set to a record -->
<xsl:template name="SomeTemplate">

  <!-- it does stuff with the record's fields -->
  <xsl:value-of select="SomeRecordField"/>

  <!-- How to access the record's position in /path/to or in any other path? -->
</xsl:template>

NOTE: This is a simplified example. I have several constraints keeping me from implementing obvious solutions, such as passing new parameters to SomeTemplate, etc. I can really only modify the internals of SomeTemplate.

NOTE: I'm using Xalan 2.7.1 with EXSLT. So those tricks are available

Any ideas?

Answer

Tomalak picture Tomalak · Jul 20, 2011

You could use

<xsl:value-of select="count(preceding-sibling::record)" />

or even, generically,

<xsl:value-of select="count(preceding-sibling::*[name() = name(current())])" />

Of course this approach will not work if you process a list of nodes that is not uniform, i.e.:

<xsl:apply-templates select="here/foo|/somewhere/else/bar" />

Position information is lost in such a case, unless you store it in a variable and pass that to the called template:

<xsl:variable name="pos" select="position()" />
<xsl:for-each select="$record">
  <xsl:call-template name="SomeTemplate">
    <xsl:with-param name="pos" select="$pos" />
  </xsl:call-template>
</xsl:for-each>

but obviously that would mean some code rewriting, which I realize you want to avoid.


Final hint: position() does not tell you the position of the node within its parent. It tells you the position of the current node relative to the list of nodes you are processing right now.

If you only process (i.e. "apply templates to" or "loop over") nodes within one parent, this happens to be the same thing. If you don't, it's not.

Final hint #2: This

<xsl:for-each select="/path/to/record">
  <xsl:variable name="record" select="."/>
  <xsl:for-each select="$record">
    <xsl:call-template name="SomeTemplate"/>
  </xsl:for-each>
</xsl:for-each>

is is equivalent to this:

<xsl:for-each select="/path/to/record">
  <xsl:call-template name="SomeTemplate"/>
</xsl:for-each>

but the latter works without destroying the meaning of position(). Calling a template does not change context, so . will refer to the correct node withing the called template.