php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #49490 XPath namespace prefix conflict
Submitted: 2009-09-07 08:41 UTC Modified: 2010-10-23 07:31 UTC
Votes:1
Avg. Score:4.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:1 (100.0%)
From: olav dot morken at uninett dot no Assigned: kalle (profile)
Status: Closed Package: Documentation problem
PHP Version: 5.3.0 OS: Linux (Debian)
Private report: No CVE-ID: None
 [2009-09-07 08:41 UTC] olav dot morken at uninett dot no
Description:
------------
When processing an XML document with namespaces, an XPath query for a node with a different namespace but the same namespace prefix fails.

This appears to be a conflict between the XPath namespaces and the document namespaces. It works if either:
- The prefix in the query is replaced with a prefix that doesn't
  exist in the document.
- If the prefix in the query matches the prefix in the document.

This was tested with:
- PHP 5.3 from debian experimental: 5.3.0-3
- libxml2 2.7.3.dfsg-2.1


Reproduce code:
---------------
$doc = new DOMDocument();
$doc->loadXML('<prefix:root xmlns:prefix="urn:a" />');

$xp = new DOMXPath($doc);
$xp->registerNamespace('prefix', 'urn:b');

echo($xp->query('//prefix:root')->length . "\n");


Expected result:
----------------
It should not find the root node, since we ask for a node in a different prefix. I.e. it should print '0'.



Actual result:
--------------
It finds the root node, i.e. it prints '1'.




Patches

bug49490 (last revision 2010-05-02 16:00 UTC by david dot zuelke at bitextender dot com)

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2010-04-17 11:14 UTC] thomas at weinert dot info
It looks like DOMXPath->evaluate()/DOMXPath->query() registers the namespace prefixes of the given context and overrides any definition from DOMXPath->registerNamespace(). PHP should not register any namespaces from the context or at least prefer manual registrations over automatic.

Reproduce code:
---------------
$dom = new DOMDocument();
$dom->loadXML(
  '<foobar><a:foo xmlns:a="urn:a">'.
  '<b:bar xmlns:b="urn:b"/></a:foo>'.
  '</foobar>'
);
$xpath = new DOMXPath($dom);

//get context node and check "a:foo"
$context = $dom->documentElement->firstChild;
var_dump($context->tagName);

// try to override the context node
$xpath->registerNamespace('a', 'urn:b');
var_dump(
  $xpath->evaluate(
    'descendant-or-self::a:*',
    $context
  )->item(0)->tagName
);

// use a prefix not used in context
$xpath->registerNamespace('prefix', 'urn:b');
var_dump(
  $xpath->evaluate(
    'descendant-or-self::prefix:*',
    $context
  )->item(0)->tagName
);

Expected result:
----------------
string(5) "a:foo"
string(5) "b:bar"
string(5) "b:bar"

Actual result:
----------------
string(5) "a:foo"
string(5) "a:foo"
string(5) "b:bar"
 [2010-05-03 10:38 UTC] johannes@php.net
-Status: Open +Status: Assigned -Assigned To: +Assigned To: rrichards
 [2010-05-04 17:41 UTC] rrichards@php.net
Automatic comment from SVN on behalf of rrichards
Revision: http://svn.php.net/viewvc/?view=revision&amp;revision=298974
Log: fix bug #49490 (XPath namespace prefix conflict)
add test
 [2010-05-04 17:47 UTC] rrichards@php.net
-Status: Assigned +Status: Closed
 [2010-05-04 17:47 UTC] rrichards@php.net
This bug has been fixed in SVN.

Snapshots of the sources are packaged every three hours; this change
will be in the next snapshot. You can grab the snapshot at
http://snaps.php.net/.
 
Thank you for the report, and for helping us make PHP better.

Automatic namespace registration really wasn't a good idea from the get go. 
Performance starts taking a hit the deeper in the tree a context node. The patch 
in the bug report was not used; rather a new third argument has been added to 
the query() and evalute() methods, which allows automatic registration of the 
context node to be disabled. For BC reasons auto registration is enabled by 
default. When auto registration is disabled and context nodes are used XPath 
performance will be increased.

i.e.
echo($xp->query('//prefix:root', null, false)->length . "\n");
 [2010-05-07 23:10 UTC] bjori@php.net
-Status: Closed +Status: Re-Opened -Type: Bug +Type: Documentation Problem
 [2010-05-07 23:10 UTC] bjori@php.net
Re-open as documentation issue:
New argument added.
 [2010-05-07 23:10 UTC] bjori@php.net
-Assigned To: rrichards +Assigned To:
 [2010-10-22 15:49 UTC] kalle@php.net
-Package: DOM XML related +Package: Documentation problem
 [2010-10-23 07:30 UTC] kalle@php.net
Automatic comment from SVN on behalf of kalle
Revision: http://svn.php.net/viewvc/?view=revision&amp;revision=304636
Log: Fixed bug #49490 (XPath namespace prefix conflict)
 [2010-10-23 07:31 UTC] kalle@php.net
-Status: Re-Opened +Status: Closed -Assigned To: +Assigned To: kalle
 [2010-10-23 07:31 UTC] kalle@php.net
This bug has been fixed in the documentation's XML sources. Since the
online and downloadable versions of the documentation need some time
to get updated, we would like to ask you to be a bit patient.

Thank you for the report, and for helping us make our documentation better.


 [2020-02-07 06:08 UTC] phpdocbot@php.net
Automatic comment on behalf of kalle
Revision: http://git.php.net/?p=doc/en.git;a=commit;h=22c05e49caf450e747cb101a3943f8d2b209fc22
Log: Fixed bug #49490 (XPath namespace prefix conflict)
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Apr 20 04:01:28 2024 UTC