使用命令行将XML元素从一个文件复制到另一个文件

问题描述 投票:0回答:1

如何使用命令行工具将元素及其具有的所有内容从XML文件复制到另一个文件?

例如,有一种使用诸如“从file-x.note复制到file-y.mynotes”这样的命令将所有内容从下面的文件X复制到文件Y的方法?

文件X:

<?xml version="1.0" encoding="UTF-8"?>
<note>
  <to>Tove</to>
  <from>Jani</from>
  <heading>Reminder</heading>
  <body>Don't forget me this weekend!</body>
</note>

文件Y:

<?xml version="1.0" encoding="UTF-8"?>
<mynotes>

  <!-- The content of file X here -->

  <note>
    <to>Tove2</to>
    <from>Jani2</from>
    <heading>Reminder2</heading>
    <body>Don't forget me this weekend2!</body>
  </note>

  <note>
    <to>Tove3</to>
    <from>Jani3</from>
    <heading>Reminder3</heading>
    <body>Don't forget me this weekend!3</body>
  </note>

<mynotes>
xml bash shell xml-parsing
1个回答
0
投票

这有点困难,但是可以用xidel完成:

xidel  file_y.xml --xquery 'let $abc := (./mynotes/note) \
let $def := doc("file_x.xml")//*[1] return insert-before($abc, 0, $def)' --output-format xml
© www.soinside.com 2019 - 2024. All rights reserved.