xml XPath 根据孩子的子值选择元素

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9683054/
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-09-06 15:15:37  来源:igfitidea点击:

XPath to select element based on childs child value

xmlxpath

提问by James Walsh

Trying to select an element based on the value of one of it's childrens childrens

试图根据其中一个孩子的孩子的值来选择一个元素

Thinking the following but not working, appreciate any help, thanks

考虑以下但不起作用,感谢任何帮助,谢谢

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

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

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

Want all books where the author name = 'John'

想要所有作者姓名 = 'John' 的书

Xml file

xml文件

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

回答by AakashM

Almost there. In your predicate, you want a relativepath, so change

差不多好了。在你的谓词中,你想要一个相对路径,所以改变

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

to either

要么

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

or

或者

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

and you will match your element. Your current predicate goes back to the root of the document to look for an author.

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