获取空数组试图用属性解析xml

时间:2023-01-31 21:54:25

I am attempting to parse an xml file with attributes, and keep getting an empty array. Here is a sample of the xml, which is parsed as a simplexml_load_string:

我试图用属性解析一个xml文件,并继续得到一个空数组。以下是xml的示例,它被解析为simplexml_load_string:

     <NumberOfOfferListings>
    <OfferListingCount condition="Any">61</OfferListingCount>
    <OfferListingCount condition="Used">45</OfferListingCount>
    <OfferListingCount condition="New">16</OfferListingCount>
  </NumberOfOfferListings>

Here is the php code that I am using

这是我正在使用的PHP代码

$priceComp_xml = amazonCompPrice_xml($asin);
$compPricing = $priceComp_xml->xpath('OfferListingCount[@condition="Any"]'); 

amazonCompPrice($asin) is the parsed xml file based on the ASIN value.

amazonCompPrice($ asin)是基于ASIN值的解析xml文件。

I need to extract just:

我需要提取:

<OfferListingCount condition="Any">61</OfferListingCount>

I have looked at many examples on here to get to this point, and it looks like what I have is correct, just returns an empty array when I use either print_r($compPricing) or var_dump. How do I fix this to get the information that I need?? I can upload any more snippets of code that will help resolve this issue.

我在这里看了很多例子来达到这一点,看起来我的东西是正确的,当我使用print_r($ compPricing)或var_dump时,只返回一个空数组。我如何解决这个问题以获取我需要的信息?我可以上传任何有助于解决此问题的代码片段。

1 个解决方案

#1


1  

There are (at least) two separate issues at play here.

这里有(至少)两个不同的问题。

The XPath

Your XPath, OfferListingCount[@condition="Any"], will only return matching <OfferListingCount> elements which are children of the element held in $priceComp_xml. It will not match descendant elements which are grand-children, or further down the tree.

您的XPath,OfferListingCount [@ condition =“Any”]将仅返回匹配的 元素,这些元素是$ priceComp_xml中保存的元素的子元素。它不会匹配作为孙子的后代元素,或者在树的下方。

So, it needs to be amended to match the <OfferListingCount> element(s). A quick fix usually to employ the shorthand // (short for /descendant-or-self::node()/), like //OfferListingCount[@condition="Any"].

因此,需要修改它以匹配 元素。快速修复通常使用简写//(/ descendant-or-self :: node()/的缩写,例如// OfferListingCount [@ condition =“Any”]。

Namespaces

Your question did not mention this, but some digging uncovered that the XML document probably has a default namespace applied to it. This can be recognised by looking at the document element for xmlns="…". When using XPath, this namespace needs to be registered and used when querying.

你的问题没有提到这一点,但是有些挖掘发现XML文档可能有一个默认的命名空间。通过查看xmlns =“...”的文档元素可以识别这一点。使用XPath时,需要在查询时注册和使用此命名空间。

$priceComp_xml->registerXPathNamespace('products', 'http://mws.amazonservices.com/schema/Products/2011-10-01');
$compPricing = $priceComp_xml->xpath('//products:OfferListingCount[@condition="Any"]');

Finally, remember that SimpleXMLElement::xpath() returns an array, so your matching <offerListingCount> element will be available as $compPricing[0].

最后,请记住SimpleXMLElement :: xpath()返回一个数组,因此匹配的 元素将作为$ compPricing [0]提供。

$count = (int) $compPricing[0];

#1


1  

There are (at least) two separate issues at play here.

这里有(至少)两个不同的问题。

The XPath

Your XPath, OfferListingCount[@condition="Any"], will only return matching <OfferListingCount> elements which are children of the element held in $priceComp_xml. It will not match descendant elements which are grand-children, or further down the tree.

您的XPath,OfferListingCount [@ condition =“Any”]将仅返回匹配的 元素,这些元素是$ priceComp_xml中保存的元素的子元素。它不会匹配作为孙子的后代元素,或者在树的下方。

So, it needs to be amended to match the <OfferListingCount> element(s). A quick fix usually to employ the shorthand // (short for /descendant-or-self::node()/), like //OfferListingCount[@condition="Any"].

因此,需要修改它以匹配 元素。快速修复通常使用简写//(/ descendant-or-self :: node()/的缩写,例如// OfferListingCount [@ condition =“Any”]。

Namespaces

Your question did not mention this, but some digging uncovered that the XML document probably has a default namespace applied to it. This can be recognised by looking at the document element for xmlns="…". When using XPath, this namespace needs to be registered and used when querying.

你的问题没有提到这一点,但是有些挖掘发现XML文档可能有一个默认的命名空间。通过查看xmlns =“...”的文档元素可以识别这一点。使用XPath时,需要在查询时注册和使用此命名空间。

$priceComp_xml->registerXPathNamespace('products', 'http://mws.amazonservices.com/schema/Products/2011-10-01');
$compPricing = $priceComp_xml->xpath('//products:OfferListingCount[@condition="Any"]');

Finally, remember that SimpleXMLElement::xpath() returns an array, so your matching <offerListingCount> element will be available as $compPricing[0].

最后,请记住SimpleXMLElement :: xpath()返回一个数组,因此匹配的 元素将作为$ compPricing [0]提供。

$count = (int) $compPricing[0];