|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
[2009-11-30 10:19 UTC] rrichards@php.net
[2009-12-02 16:25 UTC] winner5 at interia dot pl
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Tue Oct 28 19:00:01 2025 UTC |
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