xmlstarlet,更改xmlnodes,给出标识符节点列表

时间:2017-05-02 07:39:28

标签: xml xmlstarlet

我使用了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>

我如何使用book id标识符在xml文件中专门设置值而不是搜索和替换过程?

例如,对于书籍id =&#34; bk101&#34;,对于书籍id =&#34; bk103&#34;作者节点,必须成为Gambardella,无论它现在有什么(所以我们不搜索和替换,但是我们设置了值),对于book id =&#34; bk102&#34;,作者必须设置为say 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完成上述工作?

1 个答案:

答案 0 :(得分:0)

更改XPath以匹配author的子book元素,其中id属性等于&#39; bk101&#39;或&#39; bk103&#39; :

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