简体   繁体   English

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

[英]Copying XML element from one file to another using command line

How can I copy an element together with any content it has from a XML file to another using a command-line tool?如何使用命令行工具将元素及其包含的任何内容从 XML 文件复制到另一个文件?

For example, is there a way to copy all the content from file X below to file Y using a command such as "copy to file-y.mynotes from file-x.note"?例如,有没有办法使用诸如“从文件-x.note 复制到文件-y.mynotes”之类的命令将下面文件 X 中的所有内容复制到文件 Y 中?

File X:文件 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>

File Y:文件 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>

It's a little difficult but can be done with xidel:这有点困难,但可以用 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM