I have an XML file where I need to copy the value of each < Source > node and insert it into corresponding < Target > node (overwrite, where not empty) .
XML file has this structure:
<Message>
<Id>VARIABLE_1</Id>
<Code>VAR</Code>
<Source>TEXT 1</Source>
<Source>TEXT 2</Source>
<Source/>
<Source>TEXT 3</Source>
<Comment/>
<Target>SOMETHING 1</Target>
<Target>SOMETHING 2</Target>
<Target/>
<Target>SOMETHING 3</Target>
<Comment/>
</Message>
I need to "transform" it into this:
<Message>
<Id>VARIABLE_1</Id>
<Code>VAR</Code>
<Source>TEXT 1</Source>
<Source>TEXT 2</Source>
<Source/>
<Source>TEXT 3</Source>
<Comment/>
<Target>TEXT 1</Target>
<Target>TEXT 2</Target>
<Target/>
<Target>TEXT 3</Target>
<Comment/>
</Message>
So, essentially, every < Target > node will inherit the value of corresponding preceding < Source > node.
There is always going to be a matching number of < Source > and < Target > nodes, but some parent nodes might contain only one < Source > and < Target > node and some might contain anywhere up to 5 of each (in this example case there are 3 of each).
I've tried this XSLT:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>
<!-- identity transform -->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="//Target">
<xsl:copy>
<xsl:value-of select="//Target/preceding-sibling::Source"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
But it copies only the very first < Source > node and inserts it into every subsequent < Target > node. And I need them to be copied in sequence - first node gets copied into first < Target > node, second < Source > node gets copied into second < Target > node, etc.