php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #53393 XPath path issue with namespaced elements created with DOMDocument
Submitted: 2010-11-24 04:01 UTC Modified: 2011-01-11 19:02 UTC
From: paul dot visco at roswellpark dot org Assigned:
Status: Not a bug Package: DOM XML related
PHP Version: 5.3.3 OS: CENTOS 5.5/Fedora 14
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: paul dot visco at roswellpark dot org
New email:
PHP Version: OS:

 

 [2010-11-24 04:01 UTC] paul dot visco at roswellpark dot org
Description:
------------
When creating a namespaced DOMDocument programmatically (appendChild, etc), I cannot get DOMXPath to return the same results on the same underlying XML document as I can when use DOMDocument->loadXML() to populate the documents XML.

I compiled and tested the latest PHP 5.3.4RC2-dev (cli) (built: Nov 23 2010 21:51:46) using --with-dom and --with-xml and used the latest stable libxml2-devel.x86_64 2.7.7-2.fc14 from fedora 14 and the problem still persists.

Sorry its like 25 lines of code with comments, I don't have somewhere else to put them.

Test script:
---------------
//Constructing the DOMDocument from XML string allows DOMXPath to work
$xml = <<<EOT
<?xml version="1.0" encoding="UTF-8"?>
<entry xmlns="http://www.w3.org/2005/Atom" xmlns:other="http://other.w3.org/other"><id>uYG7-sPwjFg</id><published>2009-05-17T18:29:31.000Z</published></entry>
EOT;
$doc = new DOMDocument;
$doc->loadXML($xml);
$xpath = new DOMXPath($doc);
$xpath->registerNamespace('atom', "http://www.w3.org/2005/Atom");
$entries = $xpath->evaluate('//atom:entry/atom:published/text()');
///prints 2009-05-17T18:29:31.000Z as expected
print $entries->item(0)->nodeValue ;

//Constructing the same exact document programmatically causes DOMXPath to not work
$doc = new DOMDocument("1.0", "UTF-8");
$entry = $doc->createElement('entry');
$doc->appendChild($entry);
$entry->setAttribute('xmlns', "http://www.w3.org/2005/Atom");
$entry->setAttribute('xmlns:other', "http://other.w3.org/other");
$id = $entry->appendChild($doc->createElement('id'));;
$id->appendChild($doc->createTextNode("uYG7-sPwjFg"));
$published = $entry->appendChild($doc->createElement('published'));
$published->appendChild($doc->createTextNode("2009-05-17T18:29:31.000Z"));

$xpath = new DOMXPath($doc);
$xpath->registerNamespace('atom', "http://www.w3.org/2005/Atom");
$entries = $xpath->evaluate('//atom:entry/atom:published/text()');
//throws error as node is not found
print $entries->item(0)->nodeValue;

Expected result:
----------------
I would expect both of the above examples to print 2009-05-17T18:29:31.000Z

Actual result:
--------------
Example one prints 2009-05-17T18:29:31.000Z and example 2 fails because the node is not found by Xpath

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2011-01-11 19:02 UTC] rrichards@php.net
-Status: Open +Status: Bogus
 [2011-01-11 19:02 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

must use dom level 2 namespace aware methods
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Wed Feb 05 06:01:32 2025 UTC