|  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
 [2015-05-27 13:45 UTC] danielklein at airpost dot net
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

Test script:
$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) {

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/>"


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2015-05-27 20:50 UTC]
-Status: Open +Status: Not a bug
 [2015-05-27 20:50 UTC]
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]).

PHP Copyright © 2001-2020 The PHP Group
All rights reserved.
Last updated: Mon Sep 28 13:01:23 2020 UTC