Re: [xsl] current() and position()?

Subject: Re: [xsl] current() and position()?
From: "Piez, Wendell A. (Fed) wendell.piez@xxxxxxxx" <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx>
Date: Mon, 2 Dec 2019 16:53:39 -0000
Dear David and XSL-List,

Mike points out this isn't about current(): it is a red herring. Nonetheless
it might help to keep in mind that while position() is an XPath expression,
current() is not. It is defined only by XSLT.

This is because its entire purpose is to refer from inside an XPath expression
- that is, from a processing context that has changed from the context at the
start of the path - back out to the calling context. XPath without XSLT has no
such context (by definition, it is provided), so it does not define this
function. Other languages embedding XPath offer other ways of dealing with
this issue. Mostly this is by providing for variable bindings to be made
outside the path, permitting us to reference back out to some context that
way. Or simply by not supporting relative XPaths from arbitrary locations, so
the calling context is always the document root.

Indeed, since we can bind variables in XSLT, as in XQuery, we don't (mostly)
need current() in XSLT either. Most always we could rewrite it (apologies for
smart keyboard making dumb syntax errors):

<xsl:variable name="letters" as="xs:string+" select="'a', 'b', 'c'"/>
<xsl:for-each select="$letters">
    <xsl:variable name="current" select="."/>
    <xsl:message select="position(), $current ! position()"/>

This might suggest why the expression "current() ! position()" behaves the way
it does. It is the same as "for $c in (.) return position()".

So position() is not different in XPath from XSLT. The difference is only
where the processing context is defined, which includes position along with a
context node (or item as here), context size (returned by last()) and variable
bindings in scope. Where the XPath is called, this context - it can be thought
of as a set of properties used to initialize the XPath evaluation - is defined
by the calling context in the XSLT. But deeper inside an XPath is a different
matter, since each step provides context(s) for the next one (changes focus).
The difference is not how position() works but whose position within which
sequence is being asked for.

Cheers, Wendell

Current Thread