XML 文件中的条件语句使用 xmlstarlet
Conditionals in XML file using xmlstarlet
考虑到 here and 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:
我一直在想如果我有两本书同名会怎样。我已经编辑了上面的代码,现在我有 2 本书叫哈利波特,每本都有不同的出版日期和上架
按照Daniel Haley的方法,我想知道所有书名是哈利波特的书:
xmlstarlet sel -t -v "/*/book[Description/title='Harry Potter']/Location/shelf"
但是我只得到第一个结果,但是我想要所有的结果
不需要使用条件:
有了这本书,我得到了有库存书的作者
xmlstarlet sel -t -m "/bookstore/book/Description[stock='YES']" -v author -n book.xml
这将提供所有的货架。越来越近了,但不是我们想要的
xmlstarlet sel -t -m "/bookstore/book/Description[stock='YES']" -v "/bookstore/book/Location/shelf" -n book.xml
最后这一篇,给大家上架有货的书
xmlstarlet sel -t -m "/bookstore/book[Description[stock='YES']]" -v "Location/shelf" -n book.xml
除了不需要条件(-i
)之外,其实也不需要匹配(-m
);只需获取值 (-v
)...
xmlstarlet sel -t -v "/*/book[Description/stock='YES']/Location/shelf" -n book.xml
考虑到 here and 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:
我一直在想如果我有两本书同名会怎样。我已经编辑了上面的代码,现在我有 2 本书叫哈利波特,每本都有不同的出版日期和上架
按照Daniel Haley的方法,我想知道所有书名是哈利波特的书:
xmlstarlet sel -t -v "/*/book[Description/title='Harry Potter']/Location/shelf"
但是我只得到第一个结果,但是我想要所有的结果
不需要使用条件:
有了这本书,我得到了有库存书的作者
xmlstarlet sel -t -m "/bookstore/book/Description[stock='YES']" -v author -n book.xml
这将提供所有的货架。越来越近了,但不是我们想要的
xmlstarlet sel -t -m "/bookstore/book/Description[stock='YES']" -v "/bookstore/book/Location/shelf" -n book.xml
最后这一篇,给大家上架有货的书
xmlstarlet sel -t -m "/bookstore/book[Description[stock='YES']]" -v "Location/shelf" -n book.xml
除了不需要条件(-i
)之外,其实也不需要匹配(-m
);只需获取值 (-v
)...
xmlstarlet sel -t -v "/*/book[Description/stock='YES']/Location/shelf" -n book.xml