PHP XML xlink:href属性

问题描述:

我在使用SimpleXML读取Spreadshirt API中的属性时遇到了困难。我无法从资源中获取xlink:href属性,这是我所需要的,因为它不会显示在收到的数据中。似乎能够抓住一切,但。PHP XML 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 
        ) 

      ) 

    ) 

) 

有没有人有什么想法?我很难过。

+0

您不显示返回的数据。要显示数据,使用** SimpleXMLElement **对象的' - > asXML('php:// output')'方法,不要使用'print_r'或'var_dump',这些函数隐藏任何对象的数据。 – hakre 2014-12-22 11:32:15

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

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,而不是它的前缀。

+0

太棒了,谢谢! – 2014-12-06 21:14:49