Considering the codes in here and here, I've changed them to explain my question. Now the code looks like this:
<?xml version="1.0" encoding="ISO-8859-1"?>
<bookstore>
<book category="COOKING">
<Description>
<title lang="en">Everyday Italian</title>
<author>Giada De Laurentiis</author>
<year>2005</year>
<stock>YES</stock>
</Description>
<Location>
<restock>UMG</restock>
<shelf>30</shelf>
</Location>
</book>
<book category="CHILDREN">
<Description>
<title lang="en">Harry Potter</title>
<author>J K. Rowling</author>
<year>2005</year>
<stock>NO</stock>
</Description>
<Location>
<restock>GIP</restock>
<shelf>20</shelf>
</Location>
</book>
<book category="CHILDREN">
<Description>
<title lang="en">Harry Potter</title>
<author>J K. Rowling</author>
<year>2015</year>
<stock>YES</stock>
</Description>
<Location>
<restock>GIP</restock>
<shelf>21</shelf>
</Location>
</book>
<book category="WEB">
<Description>
<title lang="en">XQuery Kick Start</title>
<author>James McGovern</author>
<year>2003</year>
<stock>YES</stock>
</Description>
<Location>
<restock>NGT</restock>
<shelf>11</shelf>
</Location>
</book>
In my personal issue, is that I want to first, check if a book is in stock, and if it is, check the shelf where it is located. Access to the value of stock if pretty straight forward:
xmlstarlet sel -t -c "/bookstore/book/Description[stock='YES']" book.xml
But I am unable to do the conditional. In the xmlstarlet guide, it says the I should use -i or --if, but I've been trying to do it as follows:
xmlstarlet sel -t -c -i "/bookstore/book/Description[stock='YES']" -v "/bookstore/book/Location/shelf" book.xml
Because I saw a similar problem, but it is now working. Any ideas?
EDIT:
Using the following approach, I don't get errors, but anything at all
cat book.xml | xmlstarlet sel -t -m "/bookstore/book/Description" -i "@stock='YES'" -v '/bookstore/book/Location/shelf'
EDIT 2:
I've been thinking about what would happen if I have two books called the same. I've edited the code above, and now I have 2 book called Harry Potter, each one with different publishing dates and shelves
Following Daniel Haley approach, I want to know all the book with the title harry potter:
xmlstarlet sel -t -v "/*/book[Description/title='Harry Potter']/Location/shelf"
But I only get the first result, but I want all of them.
In addition to not needing conditionals (-i
), it's also not really necessary to match (-m
) either; just get the value (-v
)...
xmlstarlet sel -t -v "/*/book[Description/stock='YES']/Location/shelf" -n book.xml