php 具有多个包含在不同元素上的 XPath

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18547410/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 17:47:02  来源:igfitidea点击:

XPath with multiple contains on different elements

phpxmlxpathsimplexmlcontains

提问by TeaCupApp

Is it possible to have XPathexpression with multiple contains of different element values?

是否可以有XPath多个包含不同元素值的表达式?

XML

XML

<data>
   <person>
      <firstname>Kerry</firstname>
      <lastname>Packer</lastname>
      <address>Crown</address>
   <person>
   <person>
      <firstname>Kerry</firstname>
      <lastname>Murdoch</lastname>
      <address>California</address>
   <person>
<data>

PHP

PHP

$xml = simplexml_load_string($data);
$elements = $xml->xpath("(//person)[firstname[contains(., 'Kerr')]] and [lastname[contains(., 'och')]]");

Currently above XPathexpression is flagged as invalid. However if I use it with one element,

目前上面的XPath表达式被标记为无效。但是,如果我将它与一个元素一起使用,

$xml->xpath("(//person)[firstname[contains(., 'Kerr')]]"); 

then it works fine.

然后它工作正常。

回答by Martin Honnen

You simply want

你只是想要

//person[contains(firstname, 'Kerr') and contains(lastname, 'och')]