Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XPath query result order

For another question I have created some XML related code that works on my development machine but not on viper codepad where I tested it before adding it to my answer.

I could reduce my problem to the point that the order of nodes returned by DOMXPath::query() differs between my system and the codepad.

XML: <test>This is some <span>text</span>, fine.</test>

When I query all textnodes //child::text() the result differs:

Viper Codepad:

#0: This is some 
#1: , fine.
#2: text

My Machine:

#0: This is some 
#1: text
#2: , fine.

I'm not that experienced with xpath that I do understand why this happens and how it's probably possible to influence the return order with the PHP implementation.

Edit:

Further testing has revealed that LIBXML_VERSION differs between the two systems:

Viper Codepad: 20626 (2.6.26; 6 Jun 2006)
My Machine...: 20707 (2.7.7; 15 Mar 2010)
like image 629
hakre Avatar asked Nov 19 '11 17:11

hakre


People also ask

How do you specify a child element using XPath?

In the div component with an id feature of hero //div[@id='hero'] the following items will be chosen by this XPath expression: //div[@id='hero']/* will choose every one of its children components. //div[@id='hero']/img will choose every children in it.

What is XPath selector?

XPath stands for XML Path Language. It uses a non-XML syntax to provide a flexible way of addressing (pointing to) different parts of an XML document. It can also be used to test addressed nodes within a document to determine whether they match a pattern or not.

What is XPath expression in XML?

XPath uses path expressions to select nodes or node-sets in an XML document. These path expressions look very much like the expressions you see when you work with a traditional computer file system. XPath expressions can be used in JavaScript, Java, XML Schema, PHP, Python, C and C++, and lots of other languages.


2 Answers

Technically XPath 1.0 returns node-sets rather than node sequences. In the XPath 1.0 specification there is no statement about the order of these node-sets - indeed, being sets, they have no intrinsic order.

However, XSLT 1.0 always processes the node-sets returned by XPath 1.0 in document order, and because of that precedent, there is a widespread expectation that XPath results will be in document order when XPath is invoked from languages other than XSLT. However, there is nothing in the spec to guarantee this. In XPath 2.0 the user expectation becomes part of the spec, and the results of a path expression MUST be in document order.

like image 89
Michael Kay Avatar answered Oct 12 '22 02:10

Michael Kay


I could find the following bug-report which looks like the issue: Bug 363252 - proximity position in libxml2's xmlXPathEvalExpression() reported 18 Oct 2006 and confirmed dating back since May 2006 which is before the 2.6.26 version in question.

This should have been fixed in libxml2 2.6.27.

like image 35
hakre Avatar answered Oct 12 '22 02:10

hakre