Improvements to namespace handling using new test case 21/08/2012 00:18
This commit is contained in:
parent
ee77e63662
commit
e6ed9928a6
1 changed files with 43 additions and 32 deletions
|
@ -54,27 +54,36 @@ function simplexml_dump(SimpleXMLElement $sxml)
|
||||||
$dump .= $indent . $indent . 'String Content: \'' . (string)$item . '\'' . PHP_EOL;
|
$dump .= $indent . $indent . 'String Content: \'' . (string)$item . '\'' . PHP_EOL;
|
||||||
|
|
||||||
// Now some statistics about attributes and children, by namespace
|
// Now some statistics about attributes and children, by namespace
|
||||||
$dump .= $indent . $indent . 'Structural Content:' . PHP_EOL;
|
|
||||||
|
|
||||||
// If the default namespace is not declared, it will never show up using the below code
|
|
||||||
// (This is probably bad XML, or at least bad practice, but the aim here is to leave nothing invisible)
|
|
||||||
if ( ! array_key_exists('', $item->getDocNamespaces()) )
|
|
||||||
{
|
|
||||||
$dump .= $indent . $indent . $indent . '[Default, Undeclared Namespace]' . PHP_EOL;
|
|
||||||
|
|
||||||
$dump .= $indent . $indent . $indent . $indent . 'Children: ' . count($item->children(NULL)) . PHP_EOL;
|
|
||||||
$dump .= $indent . $indent . $indent . $indent . 'Attributes: ' . count($item->attributes(NULL)) . PHP_EOL;
|
|
||||||
}
|
|
||||||
|
|
||||||
$all_ns = $item->getNamespaces(true);
|
$all_ns = $item->getNamespaces(true);
|
||||||
|
// If the default namespace is never declared, it will never show up using the below code
|
||||||
|
// This will still mess up in the case where a parent element is missing the xmlns declaration,
|
||||||
|
// but a child adds it, because SimpleXML will look ahead and fill $all_ns[''] incorrectly
|
||||||
|
if ( ! array_key_exists('', $all_ns) )
|
||||||
|
{
|
||||||
|
$all_ns[''] = NULL;
|
||||||
|
}
|
||||||
|
|
||||||
foreach ( $all_ns as $ns_alias => $ns_uri )
|
foreach ( $all_ns as $ns_alias => $ns_uri )
|
||||||
{
|
{
|
||||||
$ns_label = (($ns_alias == '') ? '[Default Namespace]' : "Namespace $ns_alias");
|
$children = count($item->children($ns_uri));
|
||||||
$dump .= $indent . $indent . $indent . $ns_label . PHP_EOL;
|
$attributes = count($item->attributes($ns_uri));
|
||||||
|
|
||||||
$dump .= $indent . $indent . $indent . $indent . 'Namespace URI: \'' . $ns_uri . '\'' . PHP_EOL;
|
// Don't show zero-counts, as they're not that useful
|
||||||
$dump .= $indent . $indent . $indent . $indent . 'Children: ' . count($item->children($ns_uri)) . PHP_EOL;
|
if ( $children == 0 && $attributes == 0 )
|
||||||
$dump .= $indent . $indent . $indent . $indent . 'Attributes: ' . count($item->attributes($ns_uri)) . PHP_EOL;
|
{
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
$ns_label = (($ns_alias == '') ? 'Default Namespace' : "Namespace $ns_alias");
|
||||||
|
$dump .= $indent . $indent . 'Content in ' . $ns_label . PHP_EOL;
|
||||||
|
|
||||||
|
if ( ! is_null($ns_uri) )
|
||||||
|
{
|
||||||
|
$dump .= $indent . $indent . $indent . 'Namespace URI: \'' . $ns_uri . '\'' . PHP_EOL;
|
||||||
|
}
|
||||||
|
$dump .= $indent . $indent . $indent . 'Children: ' . $children . PHP_EOL;
|
||||||
|
$dump .= $indent . $indent . $indent . 'Attributes: ' . $attributes . PHP_EOL;
|
||||||
}
|
}
|
||||||
|
|
||||||
$dump .= $indent . '}' . PHP_EOL;
|
$dump .= $indent . '}' . PHP_EOL;
|
||||||
|
@ -89,25 +98,27 @@ function simplexml_dump(SimpleXMLElement $sxml)
|
||||||
}
|
}
|
||||||
|
|
||||||
$x = <<<XML
|
$x = <<<XML
|
||||||
<?xml version="1.0"?>
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
<foo xmlns="http://example.com" xmlns:a="http://example.com/a" xmlns:b="http://example.com/b">
|
<Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
|
||||||
<bar>some text</bar>
|
<loofah />
|
||||||
<bar>ooo <![CDATA[some cdata yaha!]]></bar>
|
<soapenv:Body>
|
||||||
<bar b:thing="Thing!" b:nowt="" />
|
<requestContactResponse xmlns="http://webservice.foo.com">
|
||||||
<deeper bob="jane">
|
<requestContactReturn>
|
||||||
<a:bar />
|
<errorCode xsi:nil="true"/>
|
||||||
<a:bar />
|
<errorDesc xsi:nil="true"/>
|
||||||
</deeper>
|
<id>744</id>
|
||||||
</foo>
|
<soapenv:id>744</soapenv:id>
|
||||||
|
</requestContactReturn>
|
||||||
|
</requestContactResponse>
|
||||||
|
</soapenv:Body>
|
||||||
|
</Envelope>
|
||||||
XML;
|
XML;
|
||||||
|
|
||||||
$sx = simplexml_load_string($x);
|
$sx = simplexml_load_string($x);
|
||||||
|
|
||||||
simplexml_dump($sx);
|
simplexml_dump($sx);
|
||||||
simplexml_dump($sx->children());
|
simplexml_dump($sx->children(NULL));
|
||||||
simplexml_dump($sx->deeper);
|
simplexml_dump($sx->children("soapenv", true)->Body);
|
||||||
simplexml_dump($sx->deeper->children('a', true));
|
simplexml_dump($sx->children("soapenv", true)->Body->children(NULL)->requestContactResponse->requestContactReturn->id);
|
||||||
simplexml_dump($sx->bar[2]->attributes('b', true));
|
simplexml_dump($sx->children("soapenv", true)->Body->children(NULL)->requestContactResponse->requestContactReturn->children('soapenv', true)->id);
|
||||||
simplexml_dump($sx->deeper['bob']);
|
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue