php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #27709 SimpleXML: Add registerNamespace() method like in dom
Submitted: 2004-03-26 04:51 UTC Modified: 2004-08-27 10:25 UTC
From: fjortiz at comunet dot es Assigned:
Status: Closed Package: Feature/Change Request
PHP Version: 5.0RC3-dev OS: Irrelevant
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: fjortiz at comunet dot es
New email:
PHP Version: OS:

 

 [2004-03-26 04:51 UTC] fjortiz at comunet dot es
Description:
------------
Hi,

this example works with SimpleXML xpath:

$string = <<<XML
<?xml version="1.0" encoding ="UTF-8" ?>
<a xmlns:ns="urn:1">
 <b>
  <c>text</c>
  <c>stuff</c>
 </b>
 <d>
  <c>code</c>
 </d>
</a>
XML;

$xml = simplexml_load_string($string);
$res = $xml->xpath('//a'); // returns array(1)

But if we don't use a namespace prefix (default namespace), xpath, returns an empty array, array(0), for any xpath search:

$string = <<<XML
<?xml version="1.0" encoding ="UTF-8" ?>
<a xmlns="urn:1">
 <b>
  <c>text</c>
  <c>stuff</c>
 </b>
 <d>
  <c>code</c>
 </d>
</a>
XML;

$xml = simplexml_load_string($string);
$res = $xml->xpath('//a'); // returns array(0)

This is a simple example, I found the problem with a bigger XML file (a WSDL file). This WSDL has 5 namespaces defined, and no problem at all with SimpleXML, as long as you don't define a default namespace...

Thanks for your attention




Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2004-03-26 08:09 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

This is an XPath limitation.
You need to use an expression like //*[local-name() = 'a'] or dont use default namespaces.
 [2004-06-04 11:59 UTC] davey@php.net
This *is* a bug, SimpleXML does not provide a registerNamespace() method like the DOM XPath implementation does. Without this, the XPath implementation in SimpleXML is crippled.

So its half a bug, and half a feature request. This *needs* to be added before 5.0 final IMO

The only way I can get this to workright now is:

$xml =<<<EOF
<?xml version="1.0" ?>
<xml xmlns="http://bar">
    <child attribute="value1">
        <morechildren />
        <morechildren />
        <morechildren />
        <morechildren />
    </child>
</xml>
EOF;

$simple_xml = simplexml_load_string($xml);

// Add a namespace with the same URI as the default
$simple_xml['xmlns:bar'] = "http://bar";
// Reload the XML so the namespace is recognised
$simple_xml = simplexml_load_string($simple_xml->asXML());


- Davey
 [2004-06-07 09:45 UTC] chregu@php.net
Or use the xpath expression recommended by Rob  "//
*[local-name() = 'a']". It's a feature request. 
Reclassified.
 [2004-08-27 10:25 UTC] chregu@php.net
This bug has been fixed in CVS.

Snapshots of the sources are packaged every three hours; this change
will be in the next snapshot. You can grab the snapshot at
http://snaps.php.net/.
 
Thank you for the report, and for helping us make PHP better.

it's called
registerXPathNamespace()
 
PHP Copyright © 2001-2026 The PHP Group
All rights reserved.
Last updated: Sun Feb 08 01:00:01 2026 UTC