Search code examples
phpxpathdomdocumentxpathquery

php 7 DomDocument XML xpath->query get subnode values


I'm trying to extract some info from eBaysvc.wsdl: passing API_Name I want to retrieve the Names of the Nodes required for such API and some more info let's suppose we want to get AddDispute Nodes, AddDisputeNode has as first node...

<xs:element name="DisputeExplanation" type="ns:DisputeExplanationCodeType" minOccurs="0">
    <xs:annotation>
        <xs:documentation>
            This enumerated ....
        </xs:documentation>
        <xs:appinfo>
            <CallInfo>
                <CallName>AddDispute</CallName>
                <RequiredInput>Yes</RequiredInput>
                <allValuesExcept>PaymentMethodNotSupported, ShipCountryNotSupported, Unspecified, UPIAssistance, UPIAssistanceDisabled</allValuesExcept>
            </CallInfo>
            <SeeLink>
                <Title>Creating and Managing Disputes With Trading API</Title>
                <URL>http://developer.ebay.com/DevZone/guides/features-guide/default.html#development/UPI-DisputesAPIManagement.html#UsingAddDispute</URL>
            </SeeLink>
        </xs:appinfo>
    </xs:annotation>
</xs:element>

Therefore I built the following script:

$file = 'ebaysvc.wsdl';
$xmlDoc = new DOMDocument('1.0','UTF-8');
$xmlDoc->preserveWhiteSpace = false;
$xmlDoc->formatOutput = true;
$xmlDoc->load($file);
$xpath = new DomXpath($xmlDoc);
$xpath->registerNamespace('xs', 'http://www.w3.org/2001/XMLSchema');

$API='AddDispute';

$Nodes = $xpath->query('//xs:complexType[@name="'.$API.'RequestType"]/xs:complexContent/xs:extension/xs:sequence')->item(0);

foreach($Nodes->childNodes as $node)
{
  $Node_name=$node->getAttribute('name');
  $Node_type=str_replace(['ns:','xs:'],'',$node->getAttribute('type'));
  echo "<br />".$Node_name.' => '.$Node_type;  
}

But I need also the Value of the node and I expected to get it adding this instruction the foreach loop:

$RequiredInput = $xpath->query('xs:annotation/xs:appinfo/CallInfo/RequiredInput',$node)->item(0));

while I get no result:

the only way I got it is to add 2 nested loops:

$RequiredInput = $xpath->query('xs:annotation/xs:appinfo',$node);//->item(0);
  foreach($RequiredInput->childNodes as $nn)
  {
    foreach($nn->childNodes as $nnn)
    {
      echo '<br />'.$nnn->nodeName.' => '.$nnn->nodeValue;
    }
  }

It seems does not accept that inner nodes does not have xs: namespace..

but this seems me a nonsense, but I can't find the right solution. Can suggest me what I'm doing wrongly?


Solution

  • As the complete document declares xmlns="urn:ebay:apis:eBLBaseComponents" the non-prefixed elements like CallInfo end up in that default namespace urn:ebay:apis:eBLBaseComponents and therefore, as with any elements in the default namespace, to select them with XPath 1.0, you need to use a prefix e.g.

    $xpath->registerNamespace('ebl', 'urn:ebay:apis:eBLBaseComponents');
    
    $RequiredInput = $xpath->query('xs:annotation/xs:appinfo/ebl:CallInfo/ebl:RequiredInput',$node)
    

    as your current attempt with e.g. CallInfo tries to select elements with local name CallInfo in no namespace while you need to select the CallInfo elements in that particular namespace.