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?
You could use
or even, generically,
Of course this approach will not work if you process a list of nodes that is not uniform, i.e.:
Position information is lost in such a case, unless you store it in a variable and pass that to the called template:
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
is is equivalent to this:
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.