首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >PHP xlink:href属性

PHP xlink:href属性
EN

Stack Overflow用户
提问于 2014-12-06 20:51:56
回答 1查看 2.3K关注 0票数 1

我在用SimpleXML读取Spread恤API中的属性时遇到了困难。我无法从资源中获取xlink:href属性,这正是我所需要的,因为它不会显示在接收到的数据中。不过,似乎能抓住其他一切。

这是我正在阅读的XML:

代码语言:javascript
复制
 <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返回的数据:

代码语言:javascript
复制
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
                    )

            )

    )

)

有人有什么想法吗?我很困惑。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-12-06 21:09:28

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

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27336368

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档