【问题标题】:XML namespaces - how to get them?XML 命名空间 - 如何获取它们?
【发布时间】: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>";
  }

但是我的输出是空白的。我在这里做错了什么?请帮忙...!

【问题讨论】:

    标签: php xml xpath


    【解决方案1】:

    您确实在 nopaste 中注册了错误的命名空间。这是文档中的两个命名空间。

    • http://mws.amazonservices.com/schema/Products/2011-10-01
      没有前缀的元素
    • http://mws.amazonservices.com/schema/Products/2011-10-01/default.xsd
      以 ns2 为前缀的元素

    title 使用前缀 ns2。您不必注册文档中使用的前缀。你可以而且应该只注册你自己的。在 SimpleXML 中,您必须在您喜欢调用 xpath() 方法的任何元素上执行此操作。它有助于为它创建一个小功能。

    $xmlns = [
      'p' => 'http://mws.amazonservices.com/schema/Products/2011-10-01',
      'pd' => 'http://mws.amazonservices.com/schema/Products/2011-10-01/default.xsd'
    ];
    
    function registerNamespacesOnElement(
      SimpleXMLElement $element, array $namespaces
    ) {
      foreach ($namespaces as $prefix => $namespace) {
        $element->registerXpathNamespace($prefix, $namespace);
      }
    }
    
    $sxe=new SimpleXMLElement($xml);
    registerNamespacesOnElement($sxe, $xmlns);
    $result=$sxe->xpath('//pd:title');
    foreach ($result as $title) {
      echo $title . "<br>\n";
    }
    

    输出:

    You Are a Spiritual Healer<br>
    

    【讨论】:

    • 嗨@ThW,我刚刚意识到亚马逊API调用在它给我的XML数据的开头和结尾没有&lt;&lt;&lt;XMLXML..我试过使用你的代码,我的本地主机上仍然出现一个空白屏幕:(也许它与 eval.in 不同...但 API 调用本身是否已经有 XML 标记?
    • $变量 = $variable = "..." 的替代语法。
    • 在这种情况下,我的 $variable 是否能够被解析?那么为什么它在这种情况下不起作用?
    • 您能提供一些指导吗?
    • 就像我写的那样,您确实注册了错误的命名空间:eval.in/615011
    【解决方案2】:

    您是否将标头设置为text/xml?默认情况下,PHP 将 Content-Type 设置为 text/html,因此浏览器会尝试将您的 XML 显示为 HTML。这就是为什么您可能会得到空白结果。

    尝试添加这个:

    header('Content-Type: text/xml');
    

    【讨论】:

      【解决方案3】:
      // Register namespace, set in xml declaration
      $sxe->registerXPathNamespace('ns2','http://mws.amazonservices.com/schema/Products/2011-10-01/default.xsd');
      // And use the same prefix as in xml
      $result=$sxe->xpath('//ns2:title'); 
      

      demo

      或者这样

      $ns = $sxe->getNamespaces(true);
      $sxe->registerXPathNamespace('ns2',$ns['ns2']);
      $result=$sxe->xpath('//ns2:title');
      

      demo

      【讨论】:

        猜你喜欢
        • 2013-05-15
        • 2016-05-30
        • 1970-01-01
        • 2021-11-03
        • 1970-01-01
        • 2022-11-10
        • 1970-01-01
        • 1970-01-01
        • 2014-09-14
        相关资源
        最近更新 更多