Re: [xml] relative xpath matches nodes anywhere in tree

On Fri, May 25, 2012 at 01:44:22AM -0400, Noam Postavsky wrote:
So apparently when libxml2 evaluates an XPath expression that is
relative it searches everywhere in the tree, as if the expression
started with "//". For instance

~/tmp$ cat test.xml
<?xml version="1.0"?>
~/tmp$ xmllint --xpath y test.xml

My understanding of XPath says that "y" matches all the child elements
of the context node named y. Since the context is the root, this
should match no elements. What's going on here?

  no that really is related to --xpath implementation, if you try it
from the shell you get the right behaviour:

paphio:~/XML -> xmllint --shell test.xml
/ > xpath y
Object is a Node Set :
Set contains 0 nodes:
/ > xpath //y
Object is a Node Set :
Set contains 1 nodes:
/ > 

 Actually the error is in --xpath implementation in xmllint :-)

static void doXPathQuery(
    ctxt->node = xmlDocGetRootElement(doc);
    res = xmlXPathEval(BAD_CAST query, ctxt);

  here the initialization is done with the Root element instead of
doc itself which is the classic initialization of the context node when
none is specified, leading to the error, just add a <z> elem child of x and
parent of y and the query will fail:

paphio:~/XML -> cat test.xml
<?xml version="1.0"?>

paphio:~/XML -> xmllint --xpath y test.xml
XPath set is empty
paphio:~/XML ->

  I'm fixing that --xpath node in git :-)

PS this came up because of an XMLStarlet bug report:

  I think that's unrelated :-)


Daniel Veillard      | libxml Gnome XML XSLT toolkit
daniel veillard com  | Rpmfind RPM search engine | virtualization library

[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]