如何使用xmlstarlet替换xml的一部分?

时间:2019-04-07 14:02:46

标签: xml bash shell sh xmlstarlet

我正在尝试使用xmlstarlet替换或交换xml的一部分。 这是之前和之后的示例。

之前:

<root>
  <catalog>
    <book id="bk101">
        <author>Gambardella, Matthew</author>
        <title>XML Developer's Guide</title>
        <genre>Computer</genre>
    </book>
  </catalog>

<keys>
  <key>
    <name>Robert D. Wilsey</name>
    <street>4854 Charter Street</street>
  </key>
  <key>
    <name>Elizabeth Fletcher</name>
    <street>1528 Station Street</street>
  </key>
</keys>
</root>

之后:

<root>
  <catalog>
    <book id="bk101">
        <author>Gambardella, Matthew</author>
        <title>XML Developer's Guide</title>
        <genre>Computer</genre>
    </book>
  </catalog>

<objects>
  <object>
    <guid>55866ddd-7601-42b6-8b6b-a2adfaa01125</guid>
  </object>
</objects>
</root>

脚本:

read -r -d '' XML <<"EOF"
<root>
  <catalog>
    <book id="bk101">
        <author>Gambardella, Matthew</author>
        <title>XML Developer's Guide</title>
        <genre>Computer</genre>
    </book>
  </catalog>

<keys>
  <key>
    <name>Robert D. Wilsey</name>
    <street>4854 Charter Street</street>
  </key>
  <key>
    <name>Elizabeth Fletcher</name>
    <street>1528 Station Street</street>
  </key>
</keys>
</root>
EOF

xmlstarlet ed -u '//root\keys' -v "
<objects>
  <object>
    <guid>55866ddd-7601-42b6-8b6b-a2adfaa01125</guid>
  </object>
</objects>
" <<< "$XML"

0 个答案:

没有答案