Bug #49490 [Com]: XPath namespace prefix conflict

[email protected] ("thomas at weinert dot info")
Newsgroups php.bugs
Message-ID <[email protected]>
Edit report at http://bugs.php.net/bug.php?id=49490&edit=1

 ID:               49490
 Comment by:       thomas at weinert dot info
 Reported by:      olav dot morken at uninett dot no
 Summary:          XPath namespace prefix conflict
 Status:           Open
 Type:             Bug
 Package:          DOM XML related
 Operating System: Linux (Debian)
 PHP Version:      5.3.0

 New Comment:

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"


Previous Comments:
------------------------------------------------------------------------
[2009-09-07 08:41:26] 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'.





------------------------------------------------------------------------



-- 
Edit this bug report at http://bugs.php.net/bug.php?id=49490&edit=1
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.