Re: [saxon] position() changes value if context node is accessed From: Michael Kay - 2008-10-23 13:18:04 ```> > "In a single-variable for expression, the variable is called > the range variable, the value of the expression that follows > the in keyword is called the binding sequence, and the > expression that follows the return keyword is called the > return expression. The result of the for expression is > obtained by evaluating the return expression once for each > item in the binding sequence, with the range variable bound > to that item. " > > So the key sentence is: > > "The result of the for expression is obtained by evaluating > the return expression once for each item in the binding > sequence, with the range variable bound to that item." > > which for me says the "range variable" (\$x) is just a pointer > to the item in the "binding sequence" (/foolist/foo) so the > return expression does know about the size of the binding > sequence and therefore the item's position() within it. > > ...but it's easy to understand both perspectives here. \$x isn't a pointer to anything, it is an expression whose value is a single item. It's not the meaning of a for expression that matters here, it's the meaning of \$x/position(). 3.2 Path Expressions says: "Each operation E1/E2 is evaluated as follows: Expression E1 is evaluated, and ... Each node resulting from the evaluation of E1 then serves in turn to provide an inner focus for an evaluation of E2, as described in 2.1.2 Dynamic Context. 2.1.2 Dynamic context says: When an expression E1/E2 or E1[E2] is evaluated, the context position in the inner focus for an evaluation of E2 is the position of the context item in the sequence obtained by evaluating E1. Since E1 is a singleton (that is, count(\$x) is 1), the context position while evaluating E2 can only be 1. Note that E1/E2 behaves exactly the same as E1[E2], a construct where it is much more likely that E2 will depend on position(). If you wrote for \$x in /foolist/foo return \$x[1] then you would get all four foo elements back, which is again because position() within E2 is always 1 when E1 is a singleton. Michael Kay http://www.saxonica.com/ ```