首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >SimpleXML xpath具有特定属性值的元素?

SimpleXML xpath具有特定属性值的元素?
EN

Stack Overflow用户
提问于 2015-02-24 23:15:27
回答 1查看 2.4K关注 0票数 1

我有一些这样的XML:

代码语言:javascript
复制
<item>
    <custom-attributes>
      <custom-attribute attribute-id="taco">false</custom-attribute>
      <custom-attribute attribute-id="htmlContent" xml:lang="en-US">testValue</custom-attribute>
      <custom-attribute attribute-id="htmlContent" xml:lang="default">testing123</custom-attribute>
  </custom-attribute>
</item>

如何使用xpath查找具有custom-attribute属性id的taco元素?

如何使用xpath找到custom-attribute元素,而不是htmlContent的属性ids?在这种情况下有两个。而且,它们上也有名称空间。我不确定这是否重要。

我试过这样的方法:

代码语言:javascript
复制
var_dump($xml->xpath("custom-attribute[@attribute-id='taco']"));

但这不管用。我可以通过custom-attribute元素迭代并查找我需要的内容,但是通过xpath选择它要容易得多。

我在这里错过了什么?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-02-24 23:27:50

在xpath的开头添加双斜杠:

代码语言:javascript
复制
$source = <<<X
<item>
    <custom-attributes>
      <custom-attribute attribute-id="taco">false</custom-attribute>
      <custom-attribute attribute-id="htmlContent" xml:lang="en-US">testValue</custom-attribute>
      <custom-attribute attribute-id="htmlContent" xml:lang="default">testing123</custom-attribute>
  </custom-attributes>
</item>
X;

$xml = new SimpleXMLElement($source);

$taco             = $xml->xpath("//custom-attribute[@attribute-id='taco']")[0];
$htmlContentArray = $xml->xpath("//custom-attribute[@attribute-id='htmlContent']");

echo "taco       : ", $taco, PHP_EOL;
echo "htmlContent: ", implode(', ', $htmlContentArray), PHP_EOL;

输出:

代码语言:javascript
复制
taco       : false
htmlContent: testValue, testing123

更新

对于注释中的问题,有关在item节点中的搜索;您可以使用.//从当前节点开始搜索。

代码语言:javascript
复制
// Find an item, then do an xpath on the result of that
// to find the custom attribute element.
// <items> tag added as <item> would otherwise be the root element,
// which would make the example quite pointless.
$source = <<<X
<items>
    <item>
        <custom-attributes>
            <custom-attribute attribute-id="taco">false</custom-attribute>
            <custom-attribute attribute-id="htmlContent" xml:lang="en-US">testValue</custom-attribute>
            <custom-attribute attribute-id="htmlContent" xml:lang="default">testing123</custom-attribute>
        </custom-attributes>
    </item>
    <item>
        <custom-attributes>
            <custom-attribute attribute-id="taco">true</custom-attribute>
            <custom-attribute attribute-id="htmlContent" xml:lang="en-US">item 2 testValue</custom-attribute>
            <custom-attribute attribute-id="htmlContent" xml:lang="default">item 2 testing456</custom-attribute>
          </custom-attributes>
    </item>
</items>
X;

$xml = new SimpleXMLElement($source);

$item             = $xml->item[1]; // Get second item
$taco             = $item->xpath(".//custom-attribute[@attribute-id='taco']");
$htmlContentArray = $item->xpath(".//custom-attribute[@attribute-id='htmlContent']");

echo "taco from second item: ", $taco[0], PHP_EOL;
echo "html from second item: ", implode(', ', $htmlContentArray), PHP_EOL;

输出:

代码语言:javascript
复制
taco from second item: true
html from second item: item 2 testValue, item 2 testing456
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28708250

复制
相关文章

相似问题

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