php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #69712 XPath selecting namespaced nodes only matches every second one
Submitted: 2015-05-27 13:45 UTC Modified: 2015-05-27 20:50 UTC
From: danielklein at airpost dot net Assigned:
Status: Not a bug Package: DOM XML related
PHP Version: 5.6.9 OS: *
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 you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: danielklein at airpost dot net
New email:
PHP Version: OS:

 

 [2015-05-27 13:45 UTC] danielklein at airpost dot net
Description:
------------
A simple XPath that matches namespaces should return all nodes, however, it only returns every second one.

Using the below link I confirmed this issue for all versions of PHP 5.0.0 - 5.6.9

http://3v4l.org/4qZhu

Test script:
---------------
<?php
$doc = new DOMDocument('1.0', 'UTF-8');
$doc->loadXML('<root xmlns:ns="foo" ns:id="1"> <one/> <two/> <three/> <four/> <five/> </root>');
$finder = new DOMXPath($doc);
$nodes = $finder->query("//node()[namespace::ns]");
foreach ($nodes as $n) {
  var_dump($doc->saveXML($n));
}

Expected result:
----------------
string(78) "<root xmlns:ns="foo" ns:id="1"> <one/> <two/> <three/> <four/> <five/> </root>"
string(6) "<one/>"
string(6) "<two/>"
string(8) "<three/>"
string(7) "<four/>"
string(7) "<five/>"

Actual result:
--------------
string(78) "<root xmlns:ns="foo" ns:id="1"> <one/> <two/> <three/> <four/> <five/> </root>"
string(6) "<two/>"
string(7) "<four/>"

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2015-05-27 20:50 UTC] requinix@php.net
-Status: Open +Status: Not a bug
 [2015-05-27 20:50 UTC] requinix@php.net
Unfortunately this looks like a bug with libxml and not with PHP: Python

  import libxml2
  doc = libxml2.parseFile("69712.xml")
  ctxt = doc.xpathNewContext()
  res = ctxt.xpathEval("//node()[namespace::ns]")
  print "Found %d nodes\n" % len(res)

finds 3 nodes as well.

Take a look through their tracker[1] to see if there's a bug report for it already (I didn't see one) and if not please make one. This may also be a symptom of libxml's general problem with namespaces (eg, [2]).

[1] https://bugzilla.gnome.org/buglist.cgi?product=libxml2
[2] https://bugzilla.gnome.org/show_bug.cgi?id=705413
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Thu Mar 13 19:01:31 2025 UTC