How to split the tree structure of an XML document into the desired one.?

It was very difficult for me to program XSLT to bring me the desired result.

Here is my XML input that I am testing on ..

<aaa id="1"> <bbb id="2">text1</bbb> <ccc>text2</ccc> <ddd id="3"> <eee att="4d">text3</eee> <fff> <ggg att="3d"> <yyy>text4</yyy> <iii attr="jj"/> </ggg> </fff> <sss> <ttt info="Y"> <nnn delete="N"> <mmm>text5</mmm> </nnn> </ttt> </sss> </ddd> </aaa> 

Desired Result:

 <root> <aaa id="1"/> <bbb id="2">text1</bbb> <ccc>text2</ccc> <ddd id="3"/> <eee att="4d">text3</eee> <fff/> <ggg att="3d"/> <yyy>text4</yyy> <iii attr="jj"/> <sss/> <ttt info="Y"/> <nnn delete="N"/> <mmm>text5</mmm> </root> 

I can not write generalized code. I mean, my code should be able to provide similar output for different XML files with different tag names.

+2
source share
1 answer

Something like that?

 <xsl:template match="/"> <root> <xsl:apply-templates select="*"/> </root> </xsl:template> <xsl:template match="*"> <xsl:copy> <xsl:copy-of select="@*|text()"/> </xsl:copy> <xsl:apply-templates select="*"/> </xsl:template> 
+5
source

Source: https://habr.com/ru/post/1386866/


All Articles