【发布时间】:2016-05-20 17:50:47
【问题描述】:
考虑到here 和here 中的代码,我已更改它们以解释我的问题。现在代码如下所示:
<?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>
在我的个人问题中,我想首先检查一本书是否有库存,如果有,请检查它所在的书架。如果非常直接,可以访问股票的价值:
xmlstarlet sel -t -c "/bookstore/book/Description[stock='YES']" book.xml
但是我不能做有条件的。在 xmlstarlet 指南中,它说我应该使用 -i 或 --if,但我一直在尝试这样做:
xmlstarlet sel -t -c -i "/bookstore/book/Description[stock='YES']" -v "/bookstore/book/Location/shelf" book.xml
因为我看到了类似的问题,但它现在可以工作了。有什么想法吗?
编辑:
使用以下方法,我没有收到错误,但什么都没有
cat book.xml | xmlstarlet sel -t -m "/bookstore/book/Description" -i "@stock='YES'" -v '/bookstore/book/Location/shelf'
编辑 2:
我一直在想,如果我有两本同名的书会发生什么。我已经编辑了上面的代码,现在我有两本书叫哈利波特,每一本书都有不同的出版日期和书架
按照 Daniel Haley 的方法,我想知道所有标题为哈利波特的书:
xmlstarlet sel -t -v "/*/book[Description/title='Harry Potter']/Location/shelf"
但我只得到第一个结果,但我想要所有结果。
【问题讨论】:
标签: xml xpath xmlstarlet