Reputation: 1
I have this xml file.
Xml input file
<Node Name="A100" Id="0x1" Type="1" >
<First Name="First" Val0="0" Val1="8" Val2="3" Val3="4" Val4="8" Val5="3" Val6="4">
<Second Name="N1" Val0="7" Val1="3"/>
<Second Name="N2" Val0="0" Val1="2"/>
<Second Name="N3" Val0="NoFunction" Val1="ab"/>
<Second Name="N4" Val0="0" Val1="xy"/>
</First>
<Second Name="N5" Val="No"/>
<Second Name="N6" Val="No" />
<Second Name="N7" Val="No" />
</Node>
<Node Name="B200" Id="0x2" Type="1" >
<First Name="First" Val0="0" Val1="8" Val2="7" Val3="8" Val4="5" Val5="1" Val6="0">
<Second Name="N1" Val0="7" Val1="3"/>
<Second Name="N2" Val0="0" Val1="2"/>
<Second Name="N3" Val0="NoFunction" Val1="ab"/>
<Second Name="N4" Val0="0" Val1="xy"/>
</First>
<Second Name="N5" Val="No"/>
<Second Name="N6" Val="No" />
<Second Name="N7" Val="No" />
</Node>
I need to transform in another xml file using xsl like this:
Xml output file
<Node Name="A100" Id="0x1" Type="1" >
<First Name="First" New="A100" Val0="0" Val1="8" Val2="3" Val3="4" Val4="8" Val5="3" Val6="4">
<Second Name="N1" Val0="7" Val1="3"/>
<Second Name="N2" Val0="0" Val1="2"/>
<Second Name="N3" Val0="NoFunction" Val1="ab"/>
<Second Name="N4" Val0="0" Val1="xy"/>
</First>
<Second Name="N5" Val="No"/>
<Second Name="N6" Val="No" />
<Second Name="N7" Val="No" />
</Node>
<Node Name="B200" Id="0x2" Type="1" >
<First Name="First" New="B200" Val0="0" Val1="8" Val2="7" Val3="8" Val4="5" Val5="1" Val6="0">
<Second Name="N1" Val0="7" Val1="3"/>
<Second Name="N2" Val0="0" Val1="2"/>
<Second Name="N3" Val0="NoFunction" Val1="ab"/>
<Second Name="N4" Val0="0" Val1="xy"/>
</First>
<Second Name="N5" Val="No"/>
<Second Name="N6" Val="No" />
<Second Name="N7" Val="No" />
</Node>
I want name of Node to be in First like New=@Name of Node. Can anyone help me with this? Thank you
Upvotes: 0
Views: 158
Reputation: 70618
You should learn about the XSLT Identity Template
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
This will cater for all then nodes and attributes you wish to copy across unchanged. Then, all you need is a template that matches the First
element, and adds a new attribute to it...
<xsl:template match="First">
<First New="{../@Name}">
Note the use of curly braces, which indicate an Attribute Value Template, and so will be evaluated as an expression, rather than output literally.
Try this XSLT
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml" indent="yes" />
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="First">
<First New="{../@Name}">
<xsl:apply-templates select="@*|node()"/>
</First>
</xsl:template>
</xsl:stylesheet>
Upvotes: 1