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

Php SimpleXML xpath到具有特定属性值的元素?,php,xml,xpath,xml-parsing,simplexml,Php,Xml,Xpath,Xml Parsing,Simplexml,我有这样一些XML: <item> <custom-attributes> <custom-attribute attribute-id="taco">false</custom-attribute> <custom-attribute attribute-id="htmlContent" xml:lang="en-US">testValue</custom-attribute>

我有这样一些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>
var_dump($xml->xpath("custom-attribute[@attribute-id='taco']"));
但这不起作用。我可以遍历
自定义属性
元素并查找我需要的内容,但是通过
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;
更新

有关在
项目
节点内搜索的评论中的问题;您可以使用
/
从当前节点开始搜索

// 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;

在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;
更新

有关在
项目
节点内搜索的评论中的问题;您可以使用
/
从当前节点开始搜索

// 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;

所以xpath返回一个数组,而不是SimpleXMLElement?实际上xpath表达式返回一个节点列表或标量,具体取决于表达式,SimpleXML将其转换为SimpleXMLElement objects.Hmmm的数组。是否可以使用xpath查找
,然后对其结果执行xpath以查找自定义属性元素?在我的问题中,我可能过于简化了XML。当然,请参阅更新的答案。您可能还想查看有关XPath语法的页面。谢谢您的详细回答。我现在正试图使用注册的名称空间来实现这一点:
xpath(//ns:item[@item id='item34-test']/custom attributes/custom attribute[@attribute id='taco'])但它就是不起作用。我认为名称空间造成了问题。我可能会发布另一个更具体的问题。再次感谢。所以xpath返回一个数组,而不是SimpleXMLElement?实际上xpath表达式返回一个节点列表或标量,具体取决于表达式,SimpleXML将其转换为SimpleXMLElement对象的数组。Hmmm。是否可以使用xpath查找
,然后对其结果执行xpath以查找自定义属性元素?在我的问题中,我可能过于简化了XML。当然,请参阅更新的答案。您可能还想查看有关XPath语法的页面。谢谢您的详细回答。我现在正试图使用注册的名称空间来实现这一点:
xpath(//ns:item[@item id='item34-test']/custom attributes/custom attribute[@attribute id='taco'])但它就是不起作用。我认为名称空间造成了问题。我可能会发布另一个更具体的问题。再次感谢。