0

If i am having the "comment 1", "comment 2" present in the xml and needs to have the xml merge having the comments as well and having 200+ files . The details as below:

Sample File 1:

<!--- Comment 1 -->
<root>
    <element> text= "textA" >
        <element1> textB= "textB" >
        <!--- Comment 2 -->
            <elements textC= "textC" >
            </elements>
        <!--- Comment 3 -->
            <elements textD= "textD" >
            </elements>
        <!--- Comment 4 -->
            <elements textE= "textE" >
            </elements>
        </element1>
    </element>
</root>

Sample File 2:

<!--- Comment 1 -->
<root>
    <element> text= "textA1" >
        <element1> textB= "textB" >
        <!--- Comment 2 -->
            <elements textF= "textF" >
            </elements>
        <!--- Comment 3 -->
            <elements textG= "textF" >
            </elements>
        <!--- Comment 4 -->
            <elements textH= "textG" >
            </elements>
        </element1>
    </element>
</root>

What I Want:

<!--- Comment 1 -->
<root>
    <element> text= "textA1" >
        <element1> textB= "textB" >
        <!--- Comment 2 -->
            <elements textC= "textC" >
            </elements>
        <!--- Comment 3 -->
            <elements textD= "textD" >
            </elements>
        <!--- Comment 4 -->
            <elements textE= "textE" >
            </elements>
        <!--- Comment 2 -->
            <elements textF= "textF" >
            </elements>
        <!--- Comment 3 -->
            <elements textG= "textF" >
            </elements>
        <!--- Comment 4 -->
            <elements textH= "textG" >
            </elements>
        </element1>
    </element>
</root>

Please suggest.

I tried following but no success

Merge xml files with nested elements without external libraries

But this will not include the comments.

taps
  • 1
  • ElementTree ignores comments by default. See https://stackoverflow.com/q/59543824/407651 – mzjn Jan 02 '23 at 08:56
  • You could run the Identity Transform [template with comments](https://xsltfiddle.liberty-development.net/3NgtZRr) (demo does not include `document`) in XSLT using Python's `lxml`. – Parfait Jan 02 '23 at 17:28

0 Answers0