Re: [xml] relative xpath matches nodes anywhere in tree
- From: Daniel Veillard <veillard redhat com>
- To: Noam Postavsky <npostavs users sourceforge net>
- Cc: xml gnome org
- Subject: Re: [xml] relative xpath matches nodes anywhere in tree
- Date: Fri, 25 May 2012 16:44:06 +0800
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"?>
<x>
<y>Test</y>
</x>
~/tmp$ xmllint --xpath y test.xml
<y>Test</y>
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:
1 ELEMENT y
/ >
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"?>
<x>
<z>
<y>Test</y>
</z>
</x>
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:
http://sourceforge.net/tracker/index.php?func=detail&aid=3527850&group_id=66612&atid=515106
I think that's unrelated :-)
Daniel
--
Daniel Veillard | libxml Gnome XML XSLT toolkit http://xmlsoft.org/
daniel veillard com | Rpmfind RPM search engine http://rpmfind.net/
http://veillard.com/ | virtualization library http://libvirt.org/
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]