I'm having trouble reading attributes from the Spreadshirt API with SimpleXML. I can't grab the xlink:href attribute from resources, which is what I need as it's not displays in the data received. Seem to be able to grab everything else, though.
This is the XML I'm reading in:
<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>
This is the data coming back from 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
)
)
)
)
Does anyone have any ideas? I'm stumped.
The isDuplicate
and id
attributes are in the same namespace as the element.
The href
element is in the http://www.w3.org/1999/xlink
namespace, as indicated by the xlink
prefix that is registered on the <articles>
root element.
To access all the elements for the namespace, call $element->attributes('http://www.w3.org/1999/xlink')
.
The idea is that the root element could instead say xmlns:foobar="http://www.w3.org/1999/xlink"
, and each <article>
would have foobar:href="..."
attributes, and the code above would still work, because the bound prefix is just a way of improving readability. What counts is the namespace URL, not its prefix.