note 103461 added to domxpath.query

From: Date: Thu, 14 Apr 2011 14:31:14 +0000
Subject: note 103461 added to domxpath.query
Groups: php.notes 
Request: Send a blank email to php-notes+get-178828@lists.php.net to get a copy of this message
To help with the problem identified below where the default namespace is not registered with the DOMXPath object, you can use the following replacement to update your paths accordingly: <?php $xml = <<<EOS <root xmlns="urn:test"> <foo>bar</foo> </root> EOS; $expression = '//foo'; $prefix = 'fakeprefix'; $doc = new DOMDocument(); $doc->loadXML($xml); $context = $doc->documentElement; // or whichever element you choose $xpath = new DOMXPath($doc); // register namespace as below, and apply a regex to the expression if (null !== $context->namespaceURI) { $xpath->registerNamespace($prefix, $context->namespaceURI); $expression = preg_replace('#(::|/\s*|\A)(?![/@].+?|[a-z\-]+::)#', '$1' . $prefix . ':$2', $expression); var_dump($expression); // string(16) "//fakeprefix:foo" } $foo = $xpath->query($expression, $context)->item(0); var_dump($doc->saveXML($foo)); // string(14) "<foo>bar</foo>" ?> ---- Server IP: 69.147.83.197 Probable Submitter: 193.36.79.206 ---- Manual Page -- http://www.php.net/manual/en/domxpath.query.php Edit -- https://master.php.net/note/edit/103461 Del: integrated -- https://master.php.net/note/delete/103461/integrated Del: useless -- https://master.php.net/note/delete/103461/useless Del: bad code -- https://master.php.net/note/delete/103461/bad+code Del: spam -- https://master.php.net/note/delete/103461/spam Del: non-english -- https://master.php.net/note/delete/103461/non-english Del: in docs -- https://master.php.net/note/delete/103461/in+docs Del: other reasons-- https://master.php.net/note/delete/103461 Reject -- https://master.php.net/note/reject/103461 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#178828) next »