Consider the following XML:
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Body>
<myResponse xmlns="https://example.com/foo">
<myResult xmlns:a="https://example.com/bar" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<a:accountNumber>AAA</a:accountNumber>
<a:accountName>BBB</a:accountName>
<a:accountType>CCC</a:accountType>
</myResult>
</myResponse>
</s:Body>
</s:Envelope>
I am attempting to select myResult and all the elements underneath it.
The closest I have gotten is:
//*[local-name()='myResult']//a:*
Which gets me the values of the elements, but I don’t know which which value belongs to which element.
I am doing this in PHP, here is (roughly) the code I am using:
<?php
$xmlObject = new SimpleXMLElement($result);
$namespaces = $xmlObject->getNamespaces(true);
foreach($namespaces as $key => $value) {
if($key == '') {
$key = 'ns';
}
$xmlObject->registerXPathNamespace($key, $value);
}
$element = $xmlObject->xpath("//myResult");
?>
I know there have been a bunch of questions about XPath and XML namespaces (Oh, how I’ve searched), but I haven’t found one that matches my particular case. Is what I want to do even possible?
Your
//*[local-name()='myResult']//a:*works fine. You just need to loop through and usegetNameto get the tag’s name.Demo: http://codepad.org/BAefIKZ4
EDIT: Since you are registering the namespaces, why not use them?
Demo: http://codepad.org/9MKq5oDt