Thank you for your time. I will try to keep this short and to the point.
My aim is to use XSLT 1.0 to transform an XML file so that it duplicates specific nodes. The problem is, that I need to duplicate substructures of structures that also need to be duplicated. I am hoping that the following example will make my issue clear.
Here the source XML:
<?xml version="1.0" encoding="UTF-8"?>
<parent_node>
<derived_from_a>
<element1>test</element1>
<element2>test</element2>
</derived_from_a>
<derived_from_b>
<element1>test</element1>
<element2>test</element2>
<derived_from_c>
<element3>test</element3>
<element4>test</element4>
</derived_from_c>
</derived_from_b>
</parent_node>
EDIT: I need all the <derived_from_a> nodes to be duplicated into a new node with the name <a>. The same should happen with the nodes <derived_from_b> and <derived_from_c> for the new nodes <b> and <c>.
This is the desired output:
<?xml version="1.0" encoding="UTF-8"?>
<parent_node>
<derived_from_a>
<element1>test</element1>
<element2>test</element2>
</derived_from_a>
<a>
<element1>test</element1>
<element2>test</element2>
</a>
<derived_from_b>
<element1>test</element1>
<element2>test</element2>
<derived_from_c>
<element3>test</element3>
<element4>test</element4>
</derived_from_c>
<c>
<element3>test</element3>
<element4>test</element4>
</c>
</derived_from_b>
<b>
<element1>test</element1>
<element2>test</element2>
<derived_from_c>
<element3>test</element3>
<element4>test</element4>
</derived_from_c>
<c>
<element3>test</element3>
<element4>test</element4>
</c>
</b>
</parent_node>
Note that I am duplicating the node <derived_from_c> into <c> in both parent structures (<derived_from_b> and <b>). That is what I need.
I wrote the following XSLT:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<!-- -->
<xsl:template match="//derived_from_a">
<!-- first copy structure with original name -->
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
<!-- then duplicate it -->
<a>
<xsl:copy-of select="child::node()"/>
</a>
</xsl:template>
<xsl:template match="//derived_from_b">
<!-- first copy structure with original name -->
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
<!-- then duplicate it -->
<b>
<xsl:copy-of select="child::node()"/>
</b>
</xsl:template>
<xsl:template match="//derived_from_c">
<!-- first copy structure with original name -->
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
<!-- then duplicate it -->
<c>
<xsl:copy-of select="child::node()"/>
</c>
</xsl:template>
</xsl:stylesheet>
This generates this output:
<?xml version="1.0" encoding="UTF-8"?>
<parent_node>
<derived_from_a>
<element1>test</element1>
<element2>test</element2>
</derived_from_a>
<a>
<element1>test</element1>
<element2>test</element2>
</a>
<derived_from_b>
<element1>test</element1>
<element2>test</element2>
<derived_from_c>
<element3>test</element3>
<element4>test</element4>
</derived_from_c>
<c>
<element3>test</element3>
<element4>test</element4>
</c>
</derived_from_b>
<b>
<element1>test</element1>
<element2>test</element2>
<derived_from_c>
<element3>test</element3>
<element4>test</element4>
</derived_from_c>
</b>
</parent_node>
Note that the node <c> only exists in the node <derived_from_b>. Not in the node <b>.
I have tried moving the <derived_from_c> transformation to the top of my XSLT without success.
Is there a way I can duplicate everything in one mapping?
Thank you! Greetings, Nick