php.net |  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
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please !
Your email address:
MUST BE VALID
Solve the problem:
22 - 4 = ?
Subscribe to this entry?

 
 [2003-06-15 11:16 UTC] markus dot pfefferle at web dot de
Description:
------------
By the definition of XPath from http://www.w3.org/TR/xpath, 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

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2003-06-15 16:37 UTC] sniper@php.net
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:
+  http://www.w3.org/TR/xpath#section-Introduction
+
+Daniel

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

read the docs for xpath_eval:
"The optional contextnode can be specified for doing relative XPath queries." 
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Apr 16 08:01:32 2024 UTC