【发布时间】: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