How to use AND operator in XPath?

后端 未结 2 1961
被撕碎了的回忆
被撕碎了的回忆 2021-01-13 15:24

XML is like

 
   value1
   value2
 

I want to write XPath to find id of <

相关标签:
2条回答
  • 2021-01-13 16:05

    The following XPath,

    //a[b = 'value1' and b = 'value2']/@id
    

    will select id attributes of all a elements with a child b element having string value equal to value1 AND another child b element having string value equal to value2 as requested.

    0 讨论(0)
  • 2021-01-13 16:06

    contains function can do it.

    $nodes = $xpath->query("//b[contains(., 'value1')] | //b[contains(., 'value2')]");
    

    And then, to get the parent id

    $parent_id = $nodes->item(0)->parentNode->getAttribute("id");
    

    A demo (with HTML) here.

    0 讨论(0)
提交回复
热议问题