【发布时间】:2016-07-31 04:29:59
【问题描述】:
一直在尝试提取一些带有名称空间的 XML。我自己试图理解这一点;但我似乎无法确定我正在做的事情到底出了什么问题。
我已将此设置为变量$myXMLData,并运行以下代码以吐出标题属性:
$myXMLData=<<<XML
<getmatchingproductresponse xmlns="http://mws.amazonservices.com/schema/Products/2011-10-01">
<getmatchingproductresult asin="055726328X" status="Success">
<product>
<attributesets>
<ns2:itemattributes xmlns:ns2="http://mws.amazonservices.com/schema/Products/2011-10-01/default.xsd" xml:lang="en-US">
<ns2:studio>lulu.com</ns2:studio>
<ns2:title>You Are a Spiritual Healer</ns2:title>
</ns2:itemattributes>
</attributesets>
<relationships>
</relationships>
</product>
</getmatchingproductresult>
<responsemetadata>
<requestid>4304bf06-acd2-4792-804a-394a2e01656f</requestid>
</responsemetadata>
</getmatchingproductresponse>
XML;
$sxe=new SimpleXMLElement($myXMLData);
$sxe->registerXPathNamespace('ns','http://mws.amazonservices.com/schema/Products/2011-10-01');
$result=$sxe->xpath('//ns:title');
foreach ($result as $title)
{
echo $title . "<br>";
}
但是我的输出是空白的。我在这里做错了什么?请帮忙...!
【问题讨论】: