簡體   English   中英

php 7 DomDocument XML xpath->查詢獲取子節點值

[英]php 7 DomDocument XML xpath->query get subnode values

我正在嘗試從eBaysvc.wsdl 中提取一些信息:傳遞 API_Name 我想檢索此類 API 所需的節點名稱和更多信息讓我們假設我們想要獲取AddDispute節點, AddDisputeNod e 作為第一個節點...

<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 ,因此,與默認命名空間中的任何元素一樣,選擇它們與 XPath 1.0 一起使用,您需要使用前綴,例如

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

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

因為您當前嘗試使用例如CallInfo嘗試在沒有命名空間中選擇具有本地名稱CallInfo元素,而您需要選擇該特定命名空間中的CallInfo元素。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM