php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #50325 DOMXPath can not find anything without the namespace specified
Submitted: 2009-11-29 14:40 UTC Modified: 2009-12-02 16:25 UTC
From: winner5 at interia dot pl Assigned:
Status: Not a bug Package: DOM XML related
PHP Version: 5.2.11 OS: Linux
Private report: No CVE-ID: None
 [2009-11-29 14:40 UTC] winner5 at interia dot pl
Description:
------------
DOMXPath do not find anything, like it should due to php.net online guide, it requires to registerNamespace in DOMXPath

Reproduce code:
---------------
<?php
 $dom = new DOMDocument();
 $dom->loadXML('<?xml version="1.0" encoding="utf-8" standalone="no"?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"><html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" dir="ltr"><head><title>Title</title></head><body><div><div id="read"><p>This</p></div></div></body></html>');
 header('Content-Type: application/xhtml+xml; charset="utf-8"');
 $xpath = new DOMXPath($dom);
 $nodelist = $xpath->query('/html/body/div/div[@id=\'read\'][1]');
 $root = $nodelist->item(0);
 $root->appendChild($dom->createElement('p', $root->nodeName));
 print($dom->saveXML());
?>

Expected result:
----------------
Just the same as:
<?php
 $dom = new DOMDocument();
 $dom->loadXML('<?xml version="1.0" encoding="utf-8" standalone="no"?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"><html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" dir="ltr"><head><title>Title</title></head><body><div><div id="read"><p>This</p></div></div></body></html>');
 header('Content-Type: application/xhtml+xml; charset="utf-8"');
 $xpath = new DOMXPath($dom);
 $xpath->registerNamespace('x', $dom->documentElement->namespaceURI);
 $nodelist = $xpath->query('/x:html/x:body/x:div/x:div[@id=\'read\'][1]');
 $root = $nodelist->item(0);
 $root->appendChild($dom->createElement('p', $root->nodeName));
 print($dom->saveXML());
?>

Actual result:
--------------
Fatal error: Call to a member function appendChild() on a non-object in ./index.php on line 10

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2009-11-30 10:19 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

Default namespaces must always be registered. A prefix is required to 
use namespaces in xpath queries. Not a PHP issue
 [2009-12-02 16:25 UTC] winner5 at interia dot pl
Thank you. In php guide actually there was an example without xmlns defined. My mistake.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat May 18 02:01:33 2024 UTC