XPath根据childs子值选择元素


208

尝试根据其子项之一的值选择一个子项

考虑以下内容但不起作用,感谢您的帮助,谢谢

./book[/author/name = 'John'] 要么

./book[/author/name text() = 'John']

想要所有作者姓名=“约翰”的书

xml文件

<list>
   <book>
      <author>
         <name>John</name>
         <number>4324234</number>
      </author>
      <title>New Book</title>
      <isbn>dsdaassda</isbn>
   </book>
   <book>...</book>
   <book>...</book>
</list>

Answers:


300

差不多了。在谓词中,您需要相对路径,因此请更改

./book[/author/name = 'John'] 

要么

./book[author/name = 'John'] 

要么

./book[./author/name = 'John'] 

您将匹配您的元素。您当前的谓词返回到文档的根目录以查找author


1
没有.对我没有用(需要./author/name
MichaelChirico

是否可以在函数中使用此语法?例如,如何实现诸如./book[contains(author/name, "John")]
只是一个学习者,

1
@OgrishMan新问题应该放在New Question中,但是在此之前,我只是尝试一下!顺便说一句,通常,对于xpath中的字符串文字,您需要单引号'而不是双引号"
AakashM '18 -10-22
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.