【问题标题】:PHP XML xlink:href Attributes [duplicate]PHP XML xlink:href属性[重复]
【发布时间】:2015-02-04 19:58:05
【问题描述】:

我在使用 SimpleXML 从 Spreadshirt API 读取属性时遇到问题。我无法从资源中获取 xlink:href 属性,这是我需要的,因为它没有显示在接收到的数据中。不过,似乎可以抓住其他所有东西。

这是我正在阅读的 XML:

 <articles xmlns:xlink="http://www.w3.org/1999/xlink" xmlns="http://api.spreadshirt.net" xlink:href="http://api.spreadshirt.net/api/v1/shops/800323/articles?fullData=true" offset="0" limit="50" count="16" sortField="default" sortOrder="default">
    <article isDuplicate="false" xlink:href="http://api.spreadshirt.net/api/v1/shops/800323/articles/100402428" id="100402428">
      <name>Hammer T-Shirt</name>
      <price>
        <vatExcluded>13.33</vatExcluded>
        <vatIncluded>16.00</vatIncluded>
        <vat>20.00</vat>
        <currency xlink:href="http://api.spreadshirt.net/api/v1/currencies/2" id="2"/>
      </price>
      <resources>
        <resource mediaType="png" type="preview" xlink:href="http://image.spreadshirt.net/image-server/v1/products/125642560/views/1"/>
      </resources>
    </article>
 </atricles>

这是从 SimpleXML 返回的数据:

SimpleXMLElement Object
(
[@attributes] => Array
    (
        [isDuplicate] => false
        [id] => 27368595
    )

[name] => Hammer Boxers
[price] => SimpleXMLElement Object
    (
        [vatExcluded] => 10.00
        [vatIncluded] => 12.00
        [vat] => 20.00
        [currency] => SimpleXMLElement Object
            (
                [@attributes] => Array
                    (
                        [id] => 2
                    )

            )

    )

[resources] => SimpleXMLElement Object
    (
        [resource] => SimpleXMLElement Object
            (
                [@attributes] => Array
                    (
                        [mediaType] => png
                        [type] => preview
                    )

            )

    )

)

有人有什么想法吗?我难住了。

【问题讨论】:

  • 您不会显示返回的数据。要显示数据,请在 SimpleXMLElement 对象上使用-&gt;asXML('php://output') 方法,不要使用print_rvar_dump,这些函数会隐藏任何对象的数据。

标签: php xml simplexml


【解决方案1】:

isDuplicateid 属性与元素位于同一命名空间中。

href 元素位于 http://www.w3.org/1999/xlink 命名空间中,如在 &lt;articles&gt; 根元素上注册的 xlink 前缀所示。

要访问命名空间的所有元素,请调用$element-&gt;attributes('http://www.w3.org/1999/xlink')

想法是根元素可以改为xmlns:foobar="http://www.w3.org/1999/xlink",并且每个&lt;article&gt; 将具有foobar:href="..." 属性,并且上面的代码仍然可以工作,因为绑定前缀只是提高可读性的一种方式。重要的是命名空间 URL,而不是其前缀。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-27
    • 2023-03-08
    • 1970-01-01
    • 2012-06-30
    • 2015-10-05
    相关资源
    最近更新 更多