【问题标题】:xmlstarlet, change xmlnodes, given a list of identifier nodesxmlstarlet,更改 xmlnodes,给定标识符节点列表
【发布时间】:2017-05-02 07:39:28
【问题描述】:

我用过xmlstarlet,如下

替换: xmlstarlet ed -u //author[text()='Gambardella, Matthew'] -v 'Gambardella' bookstore.xml,在以下 XML 文件中:

<?xml version="1.0"?>
<catalog>
   <book id="bk101">
      <author>Gambardella, Matthew</author>
      <title>XML Developer's Guide</title>
      <genre>Computer</genre>
      <price>44.95</price>
      <publish_date>2000-10-01</publish_date>
      <description>An in-depth look at creating applications 
      with XML.</description>
   </book>
   <book id="bk102">
      <author>Ralls, Kim</author>
      <title>Midnight Rain</title>
      <genre>Fantasy</genre>
      <price>5.95</price>
      <publish_date>2000-12-16</publish_date>
      <description>A former architect battles corporate zombies, 
      an evil sorceress, and her own childhood to become queen 
      of the world.</description>
   </book>
   <book id="bk103">
      <author>Corets, Eva</author>
      <title>Maeve Ascendant</title>
      <genre>Fantasy</genre>
      <price>5.95</price>
      <publish_date>2000-11-17</publish_date>
      <description>After the collapse of a nanotechnology 
      society in England, the young survivors lay the 
      foundation for a new society.</description>
   </book>

我如何代替搜索和替换过程,在 xml 文件中使用图书 ID 标识符专门设置值?

例如,对于图书 id="bk101",对于图书 id="bk103",作者节点必须成为 Gambadella,无论它现在有什么(所以我们不搜索和替换,但我们设置value),对于书 id="bk102",作者必须设置为 Clems Lebof。所以我们提供了一些逗号分隔的book id值,对应的值我们需要设置,所以输出最终会变成:

<?xml version="1.0"?>
<catalog>
   <book id="bk101">
      <author>Gambardella</author>
      <title>XML Developer's Guide</title>
      <genre>Computer</genre>
      <price>44.95</price>
      <publish_date>2000-10-01</publish_date>
      <description>An in-depth look at creating applications 
      with XML.</description>
   </book>
   <book id="bk102">
      <author>Clems Lebof</author>
      <title>Midnight Rain</title>
      <genre>Fantasy</genre>
      <price>5.95</price>
      <publish_date>2000-12-16</publish_date>
      <description>A former architect battles corporate zombies, 
      an evil sorceress, and her own childhood to become queen 
      of the world.</description>
   </book>
   <book id="bk103">
      <author>Gambardella</author>
      <title>Maeve Ascendant</title>
      <genre>Fantasy</genre>
      <price>5.95</price>
      <publish_date>2000-11-17</publish_date>
      <description>After the collapse of a nanotechnology 
      society in England, the young survivors lay the 
      foundation for a new society.</description>
   </book>

如何使用 xmlstarlet 完成上述操作?

【问题讨论】:

    标签: xml xmlstarlet


    【解决方案1】:

    更改 XPath 以匹配作为 book 子元素的 author 元素,其中 id 属性等于“bk101”或“bk103”:

    xmlstarlet ed -u "//book[@id='bk101' or @id='bk103']/author" -v 'Gambardella' bookstore.xml
    

    【讨论】:

    • 还有一个提示,用于进行多次顺序替换?我如何多次运行上面的命令,比如我必须做 1000 次替换,就像上面的例子一样?我应该制作一个包含 1000 个类似上述命令的 bash 脚本吗?或者我可以将输入文件用于书籍 ID 及其相应的作者替换吗?
    • 这是我的错误,@har07 但我的问题中最重要的部分是如何实现上述规模,例如,如果上述命令应该运行 1000 次替换怎么办?就像我们有 1000 组 id group1: (@id='bk101' or @id='bk103'..) group2 (@id='bk104' or @id='bk105'...) 和每个以上各组,有对应的替换值
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-06-15
    • 1970-01-01
    • 1970-01-01
    • 2022-11-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多