2
votes

I have the following XML file:

<?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>

I need to write a XML path that returns a book published after 2003, category is children and I just need to display the author and title of the book? What is frustrating is that I have everything in the XPATH that wrote except I am missing the author.

1
Here is my Xpath: /bookstore/book/title/author|/bookstore/book[@category="CHILDREN"]user3696029

1 Answers

5
votes

Use the following xpath expression:

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

Here we select every book with the category attribute equaling to CHILDREN and with year node value more than 2003. *[name()="author" or name()="title"] helps to select only specific children: author and title.

Demo using xmllint tool:

$ 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>

If you want to see the values of author and title nodes, add /text() to the end of xpath expression:

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