需要XML路径

use*_*029 2 xml xpath

我有以下XML文件:

<?xml version="1.0" encoding="UTF-8"?>
<bookstore>
<book category="COOKING">
  <title lang="en">Everyday Italian</title>
  <author>Giada De Laurentiis</author>
  <year>2005</year>
  <price>30.00</price>
</book>
<book category="CHILDREN">
  <title lang="en">Harry Potter</title>
  <author>J K. Rowling</author>
  <year>2005</year>
  <price>29.99</price>
</book>
<book category="WEB">
  <title lang="en">XQuery Kick Start</title>
  <author>James McGovern</author>
  <author>Per Bothner</author>
  <author>Kurt Cagle</author>
  <author>James Linn</author>
  <author>Vaidyanathan Nagarajan</author>
  <year>2003</year>
  <price>49.99</price>
  </book>
<book category="WEB">
  <title lang="en">Learning XML</title>
  <author>Erik T. Ray</author>
  <year>2003</year>
  <price>39.95</price>
  </book>
</bookstore>
Run Code Online (Sandbox Code Playgroud)

我需要编写一个XML路径来返回2003年之后发布的书,类别是孩子,我只需要显示书的作者和标题?令人沮丧的是,除了我错过了作者之外,我在XPATH中写了所有内容.

ale*_*cxe 5

使用以下xpath表达式:

/bookstore/book[@category="CHILDREN" and year>2003]/*[name()="author" or name()="title"]
Run Code Online (Sandbox Code Playgroud)

在这里,我们选择具有category等于CHILDRENyear节点值的属性的每本书2003.*[name()="author" or name()="title"]有助于只选择特定的孩子:authortitle.

演示使用xmllint工具:

$ xmllint input.xml --xpath '/bookstore/book[@category="CHILDREN" and year > 2003]/*[name()="author" or name()="title"]'
<title lang="en">Harry Potter</title>
<author>J K. Rowling</author>
Run Code Online (Sandbox Code Playgroud)

如果要查看authortitle节点的值,请添加/text()到xpath表达式的末尾:

$ xmllint input.xml --xpath '/bookstore/book[@category="CHILDREN" and year > 2003]/*[name()="author" or name()="title"]/text()'
Harry Potter
J K. Rowling
Run Code Online (Sandbox Code Playgroud)