使用XPath选择/过滤XML元素

here is my amazon mws api responce

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

and to select amount (12.49) from

<ListingPrice>
    <CurrencyCode>USD</CurrencyCode>
    <Amount>12.49</Amount>
</ListingPrice>

I am trying ,

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

I am expecting more then one value for this, but I am getting none at all.

Sorry, I am not good at XPath, can somebody guide me?

3个回答



目前我在代码中发现错误:</ p>


首先: 使用两个 // </ code>来选择一个元素,无论它在xml树中的位置如何。 </ p>

  $ queryResult = $ xpathvar-&gt; query('// Amount'); 
</ code> </ pre>


第二:谢谢@Ranon。 您将处理文档xml命名空间:</ p>

  //注册命名空间mws 
$ xpathvar-&gt; registerNamespace(“mws”,'http:// mws。 amazonservices.com/schema/Products/2011-10-01');
</ code> </ pre>

...并使用它,意思是:</ p>

  $ queryResult = $ xpathvar-&gt; query('// mws:Amount'); 
</ code> </ pre>


:如果要选择文本节点(在&lt; amount&gt; </ code>节点之间),您应该使用:</ p>

  $ queryResult = $ xpathvar-&gt  ; query('// mws:Amount / text()'); 
</ code> </ pre>

否则您可以选择父元素&lt; Amount&gt; </ code >(正如您已经在做的那样)并使用PHP检索值。 然后你必须将代码更改为:</ p>

  $ queryResult = $ xpathvar-&gt; query('// mws:Amount'); 
foreach($ queryResult as $ result ){
echo $ result-&gt; nodeValue; //回显节点值,而不是节点'本身'
}
</ code> </ pre>


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

  $ document-&gt; loadXML($ result); 
</ code> </ pre >


第五:您告诉您要在&lt; ListingPrice&gt; </ code>中检索&lt; Amount&gt; </ code>元素表单 元素。 请注意,&lt; RegularPrice&gt; </ code>元素中还有&lt; Amount&gt; </ code>元素。 因此确实重要</ strong> &lt; Amount&gt; </ code>元素位于树中。 使用以下查询仅获取列表价格金额:</ p>

  $ queryResult = $ xpathvar-&gt; query('// mws:ListingPrice / mws:Amount'); 
</ code> </ pre>
</ div>

展开原文

原文

Currently I found errors in your code:


First: Use two // to select an element regardless of where it is located in the xml tree.

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

Second: thanks @Ranon. You'll take care of the documents xml namespace:

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

... and use it, means:

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

Third: If you want to select the text node (between the <amount> nodes) you should use:

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

Otherwise you can select the parent element <Amount> (as you already doing) and retrieve the value with PHP. Then you have to change your code to:

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

Fourth: Also note another error in your code. When you create a DOMDocument from an xml string you'll have to use:

$document->loadXML($result);

Fifth: You told that you want to retrieve the <Amount> elements form inside <ListingPrice> elements. Note that there are also <Amount> elements inside <RegularPrice> elements. So it does matter where the <Amount> element is located in tree. Use the following query to obtain only listing price amounts:

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

doulan2827
doulan2827 别客气 ;)
7 年多之前 回复
dslfq06464
dslfq06464 我真的很感谢你所有这一切,你真是太棒了,帮助我......再次感谢
7 年多之前 回复
doutan5844
doutan5844 你试过w3schools.com/xpath/default.asp吗? 我学到的关于xpath的大部分内容都来自这个网站
7 年多之前 回复
doukun5339
doukun5339 pastebin.com/cANutDaR
7 年多之前 回复
dongqing220586
dongqing220586 最后一个请求,从哪里我可以学习这个狗屎正确的方式,尝试了这么多教程,但我还没有能够抓住它:(
7 年多之前 回复
douxin2011
douxin2011 我不能不止一次地投票:(你应得的
7 年多之前 回复
douding7189
douding7189 目前正在pastebin上创建新版本。 请阅读我的答案更新.. :)
7 年多之前 回复
duandu8707
duandu8707 pastebin.com/2m8Cw98N这是一个好方法吗?
7 年多之前 回复
doulian4762
doulian4762 没有没有......等等..还有一些你不在
7 年多之前 回复
douchangmian0305
douchangmian0305 $文档 - >的loadXML($结果); 我错过了这个,它的工作,谢谢兄弟,选择listPrice->金额我将得到$ result-> item [1] - > nodeValue; 我对吗?
7 年多之前 回复
dougua4836
dougua4836 那不是'完整代码'。 你确定卷曲呼叫成功吗?
7 年多之前 回复
douwei1944
douwei1944 不,看到这个pastebin.com/VPeYThT4,我做错了什么?
7 年多之前 回复
doulaozhi6835
doulaozhi6835 工作中?
7 年多之前 回复
duandeng7132
duandeng7132 但没有得到任何东西,我的屏幕只是空的
7 年多之前 回复
douzhang1299
douzhang1299 阅读我的更新。 :)无法快速输入。 :)
7 年多之前 回复
duanshanduo3363
duanshanduo3363 已经尝试过,没有工作
7 年多之前 回复



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

   $ ch = curl_init(); 
curl_setopt($ ch,CURLOPT_URL,$ url);
curl_setopt($ ch,CURLOPT_RETURNTRANSFER,true);
//来自curl
$ result = curl_exec($ ch);
$ xmldoc = new DOMDocument();
$ xmldoc-&gt; loadXML($ result);
$ xpathvar = new Domxpath($ xmldoc);
//注册命名空间mws
$ xpathvar-&gt; registerNamespace(“mws “,'http://mws.amazonservices.com/schema/Products/2011-10-01');

//使用命名空间mws查询
$ queryResult = $ xpathvar-&gt; query('// mws:Amount');
foreach($ queryResult as $ result){
echo $ result-&gt; nodeValue;
}
</ code> </ pre>

我选择了命名空间 如果需要,可以从子域中任意选择标识符 mws </ code>。如果需要,可以选择另一个。</ p>

我更正了@ hek2mgl找到的代码中的其他一些错误。< / em> </ p>
</ div>

展开原文

原文

Amazon returns XML using a namespace which you have to declare and use.

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

I selected the namespace identifier mws arbitrarily from the subdomain, you can choose another if you want.

I corrected some other errors in the code found by @hek2mgl.

dongsaolian8786
dongsaolian8786 <Amount />也继承了命名空间。 $ xpathvar->查询( '// MWS:ListingPrice / MWS:金额'); 应该没事。
7 年多之前 回复
dragon87836215
dragon87836215 这样做$ xpathvar-> query('// mws:ListingPrice / Amount'); 是正确的? 但它不起作用,我该怎么办呢?
7 年多之前 回复
dsvjw20866
dsvjw20866 谢谢,它仍然空白,没有回应
7 年多之前 回复



XPath表达式错误。 您需要'// Amount'</ code>来选择所有“Amount”元素</ p>
</ div>

展开原文

原文

The XPath expression is wrong. You need '//Amount' to select all the "Amount" elements

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐