[oXygen-user] Odd behavior of position() in a predicate
G. Ken Holman
gkholman at CraneSoftwrights.com
Sat Jan 5 13:06:38 CST 2013
At 2013-01-05 13:39 -0500, Mark Baker wrote:
> > position() is being re-evaluated *inside* the predicate, whereas $pos is
>not being re-evaluated inside the predicate.
>
>Fascinating. Unexpected, but entirely logical.
You can expect this any time you create a new context list, as
evaluating position() reflects the current item in that new context
list. In an XPath expression, the node test in the expression sets
the context list to those nodes in the direction of the axis. The
context list has just changed to those nodes, with each of those
nodes becoming the current item in turn. So when the predicate is
evaluated, position() is calculated for the item based on the context
list of the nodes of the node test. The previous context list was
pushed on a stack and is unavailable to position() in the
predicate. The previous context list is restored from that stack
when the XPath expression is finished being evaluated.
Every time you write position() you have to think of what is the
context list at this point. It changes often.
Check out pages 108-109 and 125 of my XSLT/XPath book (14th
edition). The complete book is available for "try and buy" preview at:
http://www.CraneSoftwrights.com/training/#ptux
For those readers who have subscribed to my 24-hour online video, see
lecture 17 (this lecture is not one of the free 5 hours of lectures):
http://www.CraneSoftwrights.com/links/udemy-ptux-online.htm
. . . . . . . . Ken
--
Contact us for world-wide XML consulting and instructor-led training
Free 5-hour lecture: http://www.CraneSoftwrights.com/links/udemy.htm
Crane Softwrights Ltd. http://www.CraneSoftwrights.com/z/
G. Ken Holman mailto:gkholman at CraneSoftwrights.com
Google+ profile: https://plus.google.com/116832879756988317389/about
Legal business disclaimers: http://www.CraneSoftwrights.com/legal
More information about the oXygen-user
mailing list