[jdom-interest] XPath.selectNodes(doc) via Jaxen: nodes not in document-order

Jason Hunter jhunter at xquery.com
Mon Aug 16 10:42:36 PDT 2004


> At 11:36 AM +0200 8/16/04, Kai Wörner wrote:
> 
>> Hi List,
>>
>> as far as I've understood the documentation, a xpath.selectNodes() should
>> return a list including the xpath results in document order. Given the
>> following XML:
> 
> That's not always true. Reverse axes such as ancestor and 
> ancestor-or-self should return their node lists in reverse document 
> order. However the descendant-or-self axis you're using is a forward 
> axis, so that does look like a bug. It's probably in Jaxen, not JDOM, 
> though; so that's where it would need to be fixed.

Reverse axes are evaluated in reverse document order when applying 
positional predicates.  That's for sure.  But the result of any XPath is 
always a sequence of nodes in document order, and I can't find anything 
in the XPath 1.0 spec to contradict this.

So given:

<a>
   <b>
     <c/>
   </b>
</a>

/a/b/c/ancestor::* returns the same as (/a, /a/b) not (/a/b, /a).

While:

/a/b/c/ancestor::*[1] returns the same as /a/b.

This is made more explicit in the XQuery 1.0 spec referring to XPath 2.0 
where:

fn:string-join(for $n in ancestor-or-self::* return name($n), '/')
returns "doc/chap/section"

See http://www.w3.org/TR/xpath-functions/#func-string-join

-jh-



More information about the jdom-interest mailing list