|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #24194 XPath only supporting absolute location paths?
Submitted: 2003-06-15 11:16 UTC Modified: 2003-07-06 10:21 UTC
From: markus dot pfefferle at web dot de Assigned:
Status: Not a bug Package: DOM XML related
PHP Version: 4.3.2 OS: Windows 2000
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If this is not your bug, you can add a comment by following this link.
If this is your bug, but you forgot your password, you can retrieve your password here.
Bug Type:
From: markus dot pfefferle at web dot de
New email:
PHP Version: OS:


 [2003-06-15 11:16 UTC] markus dot pfefferle at web dot de
By the definition of XPath from, the xpath expression 'foo' (being an abbreviated syntax of 'child::foo') is a Relative Location Path and would reference to all child nodes of the given context node with a tagname of 'foo'.

The expression '/foo' ('/child::foo') is an Absolute Location Path which references the child nodes named 'foo' of the context node's document root node.

So the following small code:

  $s = '<?xml version="1.0" ?>';
  $s .= '<foo>';
  $s .= '<bar/>';
  $s .= '</foo>';

  $doc = domxml_open_mem($s);
  $ctx = xpath_new_context($doc);
  $n = xpath_eval($ctx, 'foo');

should by strict XPath definition result in the refrencing of the <foo> element just because foo is a child node of the document root node. However, this always results in an empty $n->nodeset. This is the first error.

The Absolute Location Path '/foo' however, used in the above xpath_eval(), would deliver the expected node correctly in $n->nodeset[0].

Now if we use this node as a new context node:

  $ctx = xpath_new_context($n->nodeset[0]);
  $n = xpath_eval($ctx, 'bar');

Again, by W3C Definition, this should deliver the bar-Element, but instead it will find nothing. Altering the expression to '/bar' however suddenly finds the bar-Node. But this is incorret, since the expression '/bar' would read "the child nodes of the context node's document's root node named 'bar'" - which are non-existent, because the document's root node only child node is 'foo'.

So instead the current implementation of XPath seems to treat the preceding slash as mandatory and only referencing to the context node - not the document root.

This is not in accordance to XPath!

Reproduce code:
$s = '<?xml version="1.0" ?>';
$s .= '<foo>';
$s .= '<bar>';
$s .= 'abc';
$s .= '</bar>';
$s .= '</foo>';

$doc = domxml_open_mem($s);

$ctx = xpath_new_context($doc);
$a = xpath_eval($ctx, 'foo');
$b = xpath_eval($ctx, '/foo');

$ctx = xpath_new_context($b->nodeset[0]);
$c = xpath_eval($ctx, 'bar');
$d = xpath_eval($ctx, '/bar');

echo "a = $a <br> b = $b <br> c = $c <br> d = $d";($ctx, '/foo');

Expected result:
a = Object
b = Object
c = Object
d =

Actual result:
a = 
b = Object
c = 
d = Object


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2003-06-15 16:37 UTC]
See bug #24168

 [2003-07-06 08:30 UTC] markus dot pfefferle at web dot de
I opened a trouble ticket on the GNOME bug page with this problem, as you claim this to be a libxml-related bug, and after some weeks, this was their reply:

+------- Additional Comments From daniel at veillard dot com  2003-07-05 16:12 -------
+Seems that PHP doesn't initialize the "context node" of the
+XPath evaluation context. That cannot be solved by libxml2,
+this really need to be done by PHP interface code (or an user
+accessible routine). The evaluation context is described in the
+XPath spec:

So who's right now?
 [2003-07-06 10:21 UTC]
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at and the instructions on how to report
a bug at

read the docs for xpath_eval:
"The optional contextnode can be specified for doing relative XPath queries." 
PHP Copyright © 2001-2021 The PHP Group
All rights reserved.
Last updated: Sun Jun 20 10:01:23 2021 UTC