PHP XML xlink:href属性

时间:2014-12-06 20:51:56

标签: php xml simplexml

我在使用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
                    )

            )

    )

)

有没有人有任何想法?我很难过。

1 个答案:

答案 0 :(得分:2)

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

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

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

我们的想法是,根元素可以改为xmlns:foobar="http://www.w3.org/1999/xlink",而每个<article>都有foobar:href="..."个属性,上面的代码仍然有用,因为绑定的前缀只是一个提高可读性的方法。重要的是命名空间URL,而不是它的前缀。

相关问题