php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #37035 Fail to produce a SimpleXML Object with namespace in root element
Submitted: 2006-04-10 23:13 UTC Modified: 2006-04-14 15:33 UTC
From: mir at datanom dot net Assigned: helly (profile)
Status: Closed Package: SimpleXML related
PHP Version: 5.1.2 OS: *
Private report: No CVE-ID: None
 [2006-04-10 23:13 UTC] mir at datanom dot net
Description:
------------
Given this xml document simplexml_load_string and simplexml_load_file returns false

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body> 
<businessList 
	generic="2.0" operator="ms.com" truncated="false" 
	xmlns="urn:uddi-org:api_v2"> 
<businessInfos> 
<businessInfo		businessKey="c13cc7b2-642d-41d0-b2dd-7bb531a18997">
<name xml:lang="en">Microsoft DRMS Dev</name> 
<serviceInfos>
<serviceInfo 
serviceKey="6166f8b2-436d-4001-9f68-f37ff8b47ea3" 
businessKey="c13cc7b2-642d-41d0-b2dd-7bb531a18997">
<name xml:lang="en">Certification</name>
</serviceInfo> 
</serviceInfos> 
</businessInfo>
</businessInfos>
</businessList>
</soap:Body> 
</soap:Envelope>

If I erase namespace references from the root element a valid simpleXML object is returned:

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope>
<soap:Body> 
<businessList 
	generic="2.0" operator="ms.com" truncated="false" 
	xmlns="urn:uddi-org:api_v2"> 
<businessInfos> 
<businessInfo		businessKey="c13cc7b2-642d-41d0-b2dd-7bb531a18997">
<name xml:lang="en">Microsoft DRMS Dev</name> 
<serviceInfos>
<serviceInfo 
serviceKey="6166f8b2-436d-4001-9f68-f37ff8b47ea3" 
businessKey="c13cc7b2-642d-41d0-b2dd-7bb531a18997">
<name xml:lang="en">Certification</name>
</serviceInfo> 
</serviceInfos> 
</businessInfo>
</businessInfos>
</businessList>
</soap:Body> 
</soap:Envelope>

Reproduce code:
---------------
class xmlParser
{
	private $document;

	public function __construct($xml)
	{
	$this->document =                              simplexml_load_string((string)$xml);
	if (!$this->document)
		throw new xmlParserException("The XML document is not well-formed: \n" . $xml);
	}
}

Expected result:
----------------
A SimpleXML Object


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2006-04-12 12:57 UTC] mir at datanom dot net
Forgot to provide a simple example:

<?php
//    xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" 
//    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
//    xmlns:xsd="http://www.w3.org/2001/XMLSchema"
$xml = <<<_XML
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope>
<soap:Body> 
<businessList 
    generic="2.0" operator="ms.com" truncated="false" 
    xmlns="urn:uddi-org:api_v2"> 
<businessInfos> 
    <businessInfo businessKey="c13cc7b2-642d-41d0-b2dd-7bb531a18997">
	<name xml:lang="en">Microsoft DRMS Dev</name> 
	<serviceInfos>
	    <serviceInfo 
		serviceKey="6166f8b2-436d-4001-9f68-f37ff8b47ea3" 
		businessKey="c13cc7b2-642d-41d0-b2dd-7bb531a18997">
		<name xml:lang="en">Certification</name>
	    </serviceInfo> 
	</serviceInfos> 
    </businessInfo>
</businessInfos>
</businessList>
</soap:Body> 
</soap:Envelope>
_XML;

$document = simplexml_load_string($xml);
if (!$document)
    echo "Failed to parse document!\n";
else
    print_r($document);
?>

This example works. Adding the 3 uncommented namespace declarations to the root element will make simplexml_load_string return false.
 [2006-04-12 13:06 UTC] mir at datanom dot net
Had a wild idea which seems to prove something noticeable.
Study the example below which works:

<?php
//    xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" 
//    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
//    xmlns:xsd="http://www.w3.org/2001/XMLSchema"
$xml = <<<_XML
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body> 
<businessList 
    generic="2.0" operator="ms.com" truncated="false" 
    xmlns="urn:uddi-org:api_v2"> 
<businessInfos> 
    <businessInfo businessKey="c13cc7b2-642d-41d0-b2dd-7bb531a18997">
	<name xml:lang="en">Microsoft DRMS Dev</name> 
	<serviceInfos>
	    <serviceInfo 
		serviceKey="6166f8b2-436d-4001-9f68-f37ff8b47ea3" 
		businessKey="c13cc7b2-642d-41d0-b2dd-7bb531a18997">
		<name xml:lang="en">Certification</name>
	    </serviceInfo> 
	</serviceInfos> 
    </businessInfo>
</businessInfos>
</businessList>
</soap:Body> 
</soap:Envelope>
_XML;

$document = simplexml_load_string($xml);
if (!$document)
    echo "Failed to parse document!\n";
else
    print_r($document);
?>

Apparently simplexml_load_string does not like namespace references in an element if this same element is extended from that same namespace.
 [2006-04-14 15:33 UTC] helly@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.

You need $sxe->children($ns) / attributes($ns)

where either $ns is the URI of your namespace or NULL if you want to access non namespaced children/attributes. SXE objects do not allow changing the namespace by property or array access.

In HEAD we now have the ability to also access the root element with specific namespace.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Apr 20 04:01:28 2024 UTC