【问题标题】:Selecting / Filtering XML Element with XPath使用 XPath 选择/过滤 XML 元素
【发布时间】:2013-01-30 22:01:24
【问题描述】:

这是我的亚马逊 mws api 响应

<?xml version="1.0"?>
<GetMyPriceForSKUResponse xmlns="http://mws.amazonservices.com/schema/Products/2011-10-01">
<GetMyPriceForSKUResult SellerSKU="ds-tru-6sss" status="Success">
  <Product xmlns="http://mws.amazonservices.com/schema/Products/2011-10-01" xmlns:ns2="http://mws.amazonservices.com/schema/Products/2011-10-01/default.xsd">
    <Identifiers>
      <MarketplaceASIN>
        <MarketplaceId>Assssssss</MarketplaceId>
        <ASIN>sss</ASIN>
      </MarketplaceASIN>
      <SKUIdentifier>
        <MarketplaceId>Afasrfd</MarketplaceId>
        <SellerId>ssssss</SellerId>
        <SellerSKU>dssss</SellerSKU>
      </SKUIdentifier>
    </Identifiers>
    <Offers>
      <Offer>
        <BuyingPrice>
          <LandedPrice>
            <CurrencyCode>USD</CurrencyCode>
            <Amount>12.49</Amount>
          </LandedPrice>
          <ListingPrice>
            <CurrencyCode>USD</CurrencyCode>
            <Amount>12.49</Amount>
          </ListingPrice>
          <Shipping>
            <CurrencyCode>USD</CurrencyCode>
            <Amount>0.00</Amount>
          </Shipping>
        </BuyingPrice>
        <RegularPrice>
          <CurrencyCode>USD</CurrencyCode>
          <Amount>12.49</Amount>
        </RegularPrice>
        <FulfillmentChannel>MERCHANT</FulfillmentChannel>
        <ItemCondition>New</ItemCondition>
        <ItemSubCondition>New</ItemSubCondition>
        <SellerId>Aadada</SellerId>
        <SellerSKU>ssss</SellerSKU>
      </Offer>
      <Offer>
        <BuyingPrice>
          <LandedPrice>
            <CurrencyCode>USD</CurrencyCode>
            <Amount>1000.00</Amount>
          </LandedPrice>
          <ListingPrice>
            <CurrencyCode>USD</CurrencyCode>
            <Amount>1000.00</Amount>
          </ListingPrice>
          <Shipping>
            <CurrencyCode>USD</CurrencyCode>
            <Amount>0.00</Amount>
          </Shipping>
        </BuyingPrice>
        <RegularPrice>
          <CurrencyCode>USD</CurrencyCode>
          <Amount>1000.00</Amount>
        </RegularPrice>
        <FulfillmentChannel>MERCHANT</FulfillmentChannel>
        <ItemCondition>New</ItemCondition>
        <ItemSubCondition>New</ItemSubCondition>
        <SellerId>ssss</SellerId>
        <SellerSKU>sss</SellerSKU>
      </Offer>
    </Offers>
  </Product>
</GetMyPriceForSKUResult>
<ResponseMetadata>
  <RequestId>e0ef1c2c-4f35-4316-8629-faadadd</RequestId>
</ResponseMetadata>
</GetMyPriceForSKUResponse>

并从

中选择amount (12.49)
<ListingPrice>
    <CurrencyCode>USD</CurrencyCode>
    <Amount>12.49</Amount>
</ListingPrice>

我在努力,

// from curl
$result = curl_exec ($ch);
$xmldoc = new DOMDocument();
$xmldoc->load($result);
$xpathvar = new Domxpath($xmldoc);

$queryResult = $xpathvar->query('/Amount');
foreach($queryResult as $result){
    echo $result;
}

我对此的期望值不止一个,但我根本没有得到。

对不起,我不擅长 XPath,有人可以指导我吗?

【问题讨论】:

    标签: php dom xpath namespaces xml-namespaces


    【解决方案1】:

    目前我在您的代码中发现了错误:


    首先:使用两个// 选择一个元素,无论它位于xml 树中的什么位置。

     $queryResult = $xpathvar->query('//Amount');
    

    第二:感谢@Ranon。您将处理文档 xml 命名空间:

    // Register Namespace mws
    $xpathvar->registerNamespace("mws", 'http://mws.amazonservices.com/schema/Products/2011-10-01');
    

    ...并使用它,意味着:

     $queryResult = $xpathvar->query('//mws:Amount');
    

    第三:如果你想选择文本节点(&lt;amount&gt; 节点之间)你应该使用:

    $queryResult = $xpathvar->query('//mws:Amount/text()');
    

    否则,您可以选择父元素 &lt;Amount&gt;(就像您已经做的那样)并使用 PHP 检索该值。然后您必须将代码更改为:

    $queryResult = $xpathvar->query('//mws:Amount');
    foreach($queryResult as $result){
       echo $result->nodeValue; // echo the node value, not the node 'itself'
    }
    

    第四:还要注意代码中的另一个错误。当您从 xml 字符串创建 DOMDocument 时,您必须使用:

    $document->loadXML($result);
    

    第五:您告诉过您要检索 &lt;ListingPrice&gt; 元素中的 &lt;Amount&gt; 元素表单。请注意,&lt;RegularPrice&gt; 元素内部也有 &lt;Amount&gt; 元素。因此,&lt;Amount&gt; 元素在树中的位置确实很重要。使用以下查询仅获取标价金额:

    $queryResult = $xpathvar->query('//mws:ListingPrice/mws:Amount');
    

    【讨论】:

    • 什么都没有,我的屏幕是空的
    • 那不是“完整代码”。你确定 curl 调用成功了吗?
    • $document->loadXML($result);我错过了这个,它的工作,谢谢兄弟,选择 listPrice->amount 我将不得不 $result->item[1]->nodeValue;我说的对吗?
    • 当前在 pastebin 上创建新版本。请阅读我的答案的更新.. :)
    • @NewBee 你试过w3schools.com/xpath/default.asp 吗?我学到的关于 xpath 的大部分东西都来自这个网站
    【解决方案2】:

    Amazon 使用您必须声明和使用的命名空间返回 XML。

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    // from curl
    $result = curl_exec($ch);
    $xmldoc = new DOMDocument();
    $xmldoc->loadXML($result);
    $xpathvar = new Domxpath($xmldoc);
    // Register Namespace mws
    $xpathvar->registerNamespace("mws", 'http://mws.amazonservices.com/schema/Products/2011-10-01');
    
    // Query using namespace mws
    $queryResult = $xpathvar->query('//mws:Amount');
    foreach($queryResult as $result){
        echo $result->nodeValue;
    }
    

    我从子域中任意选择了命名空间标识符mws,您可以根据需要选择另一个。

    我更正了@hek2mgl 发现的代码中的一些其他错误。

    【讨论】:

    • 这样做 $xpathvar->query('//mws:ListingPrice/Amount');是正确的?但它不起作用,我该怎么办?
    • &lt;Amount/&gt; 也继承了命名空间。 $xpathvar-&gt;query('//mws:ListingPrice/mws:Amount'); 应该没问题。
    【解决方案3】:

    XPath 表达式错误。您需要'//Amount' 选择所有“金额”元素

    【讨论】:

      猜你喜欢
      • 2017-07-22
      • 1970-01-01
      • 2015-06-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-23
      相关资源
      最近更新 更多