[xquery-talk] Question on expected XQuery return per standards

G. Ken Holman gkholman at CraneSoftwrights.com
Fri Mar 27 20:00:28 PST 2009


At 2009-03-27 14:31 -0400, David Sewell wrote:
>Over on another list there has been a discussion of unexpected results
>from an XQuery like the following. The issues are how the parser
>interprets, in particular, the abbreviated syntax '//' and '[N]' as an
>equivalent to '[position() eq N]'.
>
>My belief is that the standard requires that two nodes (p2, p4) should
>be returned for result sets 1-4 below, because the interpretation of the
>XPath should in each case be identical.

Yes, because "//" is a step abbreviation (it isn't an axis 
abbreviation) and because when the data type of a predicate is 
numeric that is an abbreviation for position()=.

>Set 5, on the other hand, should return only p2.

Agreed.

>We're seeing different behavior from a couple of XML parsers. Anyone
>care to read from the stone tablets on this one?

Quoting chapter and verse, http://www.w3.org/TR/xpath20/ section 3.2 
explains the "//" abbreviation found at the start of an expression, 
and cites section 3.2.4 for the use of the non-initial "//" abbreviation.

And you are lucky because there is a note in 3.2.4(3) that states:

   "The path expression //para[1] does not mean the same as the path
    expression /descendant::para[1]. The latter selects the first
    descendant para element; the former selects all descendant para
    elements that are the first para children of their respective
    parents."

... which should help you categorically explain the result to an 
implementation that is returning something different than two items 
for the first four sets and one item for the last set.

I hope this helps.

. . . . . . . . . . Ken

>(: begin query :)
>
>let $xml :=
>   <root>
>     <div>
>       <p>p1</p>
>       <p>p2</p>
>     </div>
>     <div>
>       <p>p3</p>
>       <p>p4</p>
>     </div>
>   </root>
>
>return (
>   <set1>{ $xml/descendant-or-self::node()/p[position() eq 2] }</set1>,
>   <set2>{ $xml//p[position() eq 2] }</set2>,
>   <set3>{ $xml/descendant-or-self::node()/p[2] }</set3>,
>   <set4>{ $xml//p[2] }</set4>,
>   <set5>{ $xml/descendant::p[2] }</set5>
>)
>
>(: end query :)


--
XQuery/XSLT/XSL-FO training in Los Angeles (New dates!) 2009-06-08
Training tools: Comprehensive interactive XSLT/XPath 1.0/2.0 video
Video lesson:    http://www.youtube.com/watch?v=PrNjJCh7Ppg&fmt=18
Video overview:  http://www.youtube.com/watch?v=VTiodiij6gE&fmt=18
G. Ken Holman                 mailto:gkholman at CraneSoftwrights.com
Crane Softwrights Ltd.          http://www.CraneSoftwrights.com/q/
Male Cancer Awareness Nov'07  http://www.CraneSoftwrights.com/q/bc
Legal business disclaimers:  http://www.CraneSoftwrights.com/legal



More information about the talk mailing list