使用 xmlstarlet 的 XML 文件中的条件 [英] Conditionals in XML file using xmlstarlet

查看:24
本文介绍了使用 xmlstarlet 的 XML 文件中的条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑此处这里,我已经改变了它们来解释我的问题.现在代码看起来像这样:

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

但我无法执行条件操作.在 xmlstarlet 指南中,它说我应该使用 -i 或 --if,但我一直在尝试这样做:

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?

使用以下方法,我不会出错,但不会出错

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'

编辑 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

按照 Daniel Haley 的方法,我想知道所有标题为 Harry Potter 的书:

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.

推荐答案

除了不需要条件语句 (-i) 之外,也没有必要匹配 (-m代码>)要么;只需获取值 (-v)...

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

这篇关于使用 xmlstarlet 的 XML 文件中的条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆