具有特定属性值的元素的 SimpleXML xpath?
SimpleXML xpath to element with certain attribute value?
我有一些 XML 这样的:
<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 查找属性 ID 为 taco
的 custom-attribute
元素?
如何使用 xpath 查找 custom-attribute
元素而不是 htmlContent
的属性 ID?在这种情况下有 2 个。这些也有名称空间。我不确定这是否重要。
我试过这样的方法:
var_dump($xml->xpath("custom-attribute[@attribute-id='taco']"));
但这不起作用。我可以遍历 custom-attribute
元素并寻找我需要的东西,但是通过 xpath
.
简单地 select 它会容易得多
我在这里错过了什么?
在 xpath 的开头添加双斜杠:
$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;
输出:
taco : false
htmlContent: testValue, testing123
更新
关于您在评论中的问题,关于在 item
节点内搜索;您可以使用 .//
从当前节点开始搜索。
// 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;
输出:
taco from second item: true
html from second item: item 2 testValue, item 2 testing456
我有一些 XML 这样的:
<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 查找属性 ID 为 taco
的 custom-attribute
元素?
如何使用 xpath 查找 custom-attribute
元素而不是 htmlContent
的属性 ID?在这种情况下有 2 个。这些也有名称空间。我不确定这是否重要。
我试过这样的方法:
var_dump($xml->xpath("custom-attribute[@attribute-id='taco']"));
但这不起作用。我可以遍历 custom-attribute
元素并寻找我需要的东西,但是通过 xpath
.
我在这里错过了什么?
在 xpath 的开头添加双斜杠:
$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;
输出:
taco : false
htmlContent: testValue, testing123
更新
关于您在评论中的问题,关于在 item
节点内搜索;您可以使用 .//
从当前节点开始搜索。
// 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;
输出:
taco from second item: true
html from second item: item 2 testValue, item 2 testing456