ID: 27709
Updated by: [EMAIL PROTECTED]
-Summary: SimpleXML xpath function doesn't like default
namespaces
Reported By: fjortiz at comunet dot es
Status: Open
-Bug Type: SimpleXML related
+Bug Type: Feature/Change Request
Operating System: Irrelevant
PHP Version: 5.0RC3-dev
Assigned To: rrichards
New Comment:
Or use the xpath expression recommended by Rob "//
*[local-name() = 'a']". It's a feature request.
Reclassified.
Previous Comments:
------------------------------------------------------------------------
[2004-06-04 11:59:29] [EMAIL PROTECTED]
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-03-26 08:09:55] [EMAIL PROTECTED]
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-03-26 04:51:30] 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
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/?id=27709&edit=1