Reputation: 53
As a part of my learning curve of XSLT. I came across the following situation and trying to accomplish it.
I have an xml in the following format.
<parent>
<A> .. </A>
<T1>
<d1>..</d1>
<d2>..</d2>
</T1>
<T1>
<d1>..</d1>
<d2>..</d2>
</T1>
...
<T1>
<d1>..</d1>
<d2>..</d2>
</T1>
</parent>
and I am trying to move the node 'A' to under T1,T1...T1. so that final output looks like this
<parent>
<T1>
<A> .. </A>
<d1>..</d1>
<d2>..</d2>
</T1>
<T1>
<A> .. </A>
<d1>..</d1>
<d2>..</d2>
</T1>
...
<T1>
<A> .. </A>
<d1>..</d1>
<d2>..</d2>
</T1>
</parent>
Following is the xslt that i have been working with
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:DTCC="http://www.informatica.com/B2B/DTCC/NSCC/2014_03">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="@*|node()" name="identity">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:variable name="submittingHeader">
<xsl:copy-of select="./A/*" />
</xsl:variable>
<xsl:template match="T1" >
<xsl:value-of select="$submittingHeader"/>
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
Here I am trying to move atleast node A to under T1. Once if i get that then I will put for-each loop over matching template T1.
The output which i see is same the as input xml only. I dont see node 'A' getting moved under 'T1'. I think I am not correctly copying the content of a variable or i am missing something? Any guidance
Upvotes: 1
Views: 152
Reputation: 3788
What I think you are not doing properly:
submittingHeader
variable is not selecting the A
node (you should have used //A
, as the variable is outside any template)A
element from the outputA
before copying T1
This should produce the desired result:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:DTCC="http://www.informatica.com/B2B/DTCC/NSCC/2014_03">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<!-- avoid copying A in its original position -->
<xsl:template match="A"/>
<xsl:template match="T1" >
<xsl:copy>
<!-- copy A inside the copied T1 -->
<xsl:copy-of select="../A" />
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
Upvotes: 2