php 7 DomDocument XML xpath->查询获取子节点值

php 7 DomDocument XML xpath->query get subnode values

我正在尝试从 eBaysvc.wsdl 中提取一些信息: 传递 API_Name 我想检索此类 API 所需的节点名称和更多信息 假设我们想要获得 AddDispute 个节点,AddDisputeNode 作为第一个节点...

<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>

因此我构建了以下脚本:

$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;  
}

但我还需要节点的值,我希望在 foreach 循环中添加这条指令:

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

虽然我没有得到结果:

我得到它的唯一方法是添加 2 个嵌套循环:

$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;
    }
  }

似乎不​​接受内部节点没有xs:命名空间..

但这在我看来是胡说八道,但我找不到正确的解决方案。 可以建议我做错了什么吗?

由于完整文档声明 xmlns="urn:ebay:apis:eBLBaseComponents"CallInfo 等非前缀元素最终出现在该默认命名空间 urn:ebay:apis:eBLBaseComponents 中,因此,与默认命名空间中的任何元素一样, select 它们使用 XPath 1.0,您需要使用前缀,例如

$xpath->registerNamespace('ebl', 'urn:ebay:apis:eBLBaseComponents');

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

作为您当前的尝试,例如CallInfo 尝试在 no 命名空间中 select 具有本地名称 CallInfo 的元素,而您需要 select CallInfo 元素在那个特定的命名空间中。