(Note: This post has been edited to show specific use case. See bottom.)
I want to use the current node's value of position() inside an xpath expression (in which the context changes). Unfortunately, I don't see any simple way of doing this because current()/position() appears to always equal 1.
[I assume this is because current()/position() says "what is the position of node current() within the context of... the one-node set containing only current().]
To be specific, let's say I have a sequence of nodes $seq and I want to return the *n*th node in the sequence ($seq[n]) where n equals the current value of position() (that is, the value of position() prior to the statement being evaluated).
Clearly, $seq[position()] won't work, because that expands to seq[position() = position()], returning the entire sequence.
$seq[position(current())] is jibberish.
The only thing I could find that appears to maybe work is
for $pos in position() return $seq[$pos]
I assume there is a simpler way.
Edit It was requested that I give a more specific example of what I'm trying to do. I actually found the first person's answer sufficient to my needs, though not much simpler than what I had come up with myself (the "for $pos in position() return $correctOrder[$pos]" option).
Here is the actual situation: I have a set of "session" nodes, each of which has a @startTime attribute. I want to find which ones are out of order with respect to that @startTime attribute, or at least not in the proper place they would be if the input document's nodes were properly sorted by @startTime.
This is just one of many tests that are performed on each of the session nodes.
What I wanted to do was create a sequence that had the nodes in the correct order and compare the nth element in the document-ordered sequence with the nth-element in the correctly ordered sequence.
Something like this:
<xsl:template match="/">
<xsl:variable name="correctOrder" as="node()*">
<xsl:for-each select="session">
<xsl:sort select="@xs:dateTime(@startTime)"/>
<xsl:sequence select="."/>
</xsl:for-each>
</xsl:variable>
<xsl:for-each select="session">
<---- Lots of various tests ---->
<xsl:if test="current()/@startTime eq
$correctOrder[<-here I want to put the value "position()" had prior to this <xsl:if> statement->]/@startTime"
<error description="Node out of order."/>
</xsl:if>
<--- Lots of other tests----->
</xsl:for-each>