我在用SimpleXML读取Spread恤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
)
)
)
)有人有什么想法吗?我很困惑。
发布于 2014-12-06 21:09:28
isDuplicate和id属性与元素位于同一个名称空间中。
href元素位于http://www.w3.org/1999/xlink命名空间中,如在<articles>根元素上注册的xlink前缀所示。
若要访问命名空间的所有元素,请调用$element->attributes('http://www.w3.org/1999/xlink')。
其思想是根元素可以改为xmlns:foobar="http://www.w3.org/1999/xlink",每个<article>都有foobar:href="..."属性,上面的代码仍然可以工作,因为绑定前缀只是提高可读性的一种方式。重要的是名称空间URL,而不是其前缀。
https://stackoverflow.com/questions/27336368
复制相似问题