Reputation: 823
I am working on transforming the following xml.
<root>
<node1>
<node2 id="1">xyz</node2>
</node1>
<node1>
<node2 id="2">abc</node2>
</node1>
<parent>
<child>abc</child>
</parent>
</root>
I want to transform it to the following format:
<root>
<node1>
<node2 id="1">xyz</node2>
</node1>
<parent>
<child>abc</child>
<node1>
<node2 id="2">abc</node2>
</node1>
</parent>
</root>
I have added the template to copy child element
I need to check id attribute and if it is equal to 2, then copy the parent node. I would be grateful if anyone help in this.
Thank you in advance.
Upvotes: 0
Views: 1116
Reputation: 5432
If your input XML's format is not deviating much from as in your question, this XSLT will work:
<xsl:transform version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="/*/*[*[@id = '2']]"/>
<xsl:template match="parent">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
<xsl:copy-of select="/*/*[*[@id = '2']]"/>
</xsl:copy>
</xsl:template>
</xsl:transform>
Upvotes: 1