'How to extract only specific part from xml files and merge them?

I have many xml files which I want to merge into one file. I don't want to merge them from root but from one of the child. How to proceed with it using grep/sed/awk statements?

XML 1:

<root>
   <version>AB</version>
   <Data>
       <Title>MyTitle</Title>
       <SubTitle>Mysub</SubTitle>
   </Data>
   <file author="JXJX" name="MyFile1">
       <desc>File1</desc>
       <field>Random Field</field>
   </file>
<root>

XML 2:

<root>
   <version>AB</version>
   <Data>
       <Title>MyTitle 2</Title>
       <SubTitle>Mysub 2</SubTitle>
   </Data>
   <file author="HIGH" name="MyFile2">
       <desc>File2</desc>
       <field>Random Field</field>
   </file>
<root>

I want the following XML file:

<root>
    <file author="JXJX" name="MyFile1">
       <desc>File1</desc>
       <field>Random Field</field>
    </file>
    <file author="HIGH" name="MyFile2">
       <desc>File2</desc>
       <field>Random Field</field>
    </file>
</root>


Solution 1:[1]

Please don't parse XML with regex, but use a proper parser like instead:

$ xidel -se '
  element root {
    doc("1.xml")//file,
    doc("2.xml")//file
  }
' --output-node-format=xml --output-node-indent
<root>
  <file author="JXJX" name="MyFile1">
    <desc>File1</desc>
    <field>Random Field</field>
  </file>
  <file author="HIGH" name="MyFile2">
    <desc>File2</desc>
    <field>Random Field</field>
  </file>
</root>

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1