Reputation: 43
I am new to XSLT . I need an XSLT to transform my XML of one form to another. Below are the input & Output
INPUT
<AssetDataCollection>
<!-- Asset Data will be repeated for each service tag -->
<AssetData>
<REF_ID_LIST>
<REF_ID NAME="MANUFACTUREDATE" VALUE="05/03/2013" />
<REF_ID NAME="REGION" VALUE="DAO" />
<REF_ID NAME="EMBMAC1" VALUE="D4BED9403E24" />
<REF_ID NAME="EMBMAC2" VALUE="D4BED9403E24" />
<REF_ID NAME="EMBMAC3" VALUE="D4BED9403E24" />
<REF_ID NAME="AC1" VALUE="D4BED9403E24" />
<REF_ID NAME="MAC2" VALUE="D4BED9403E24" />
<REF_ID NAME="MAC3" VALUE="D4BED9403E24" />
</REF_ID_LIST>
</AssetData>
</AssetDataCollection>
OUTPUT
<AssetDataCollection>
<!-- Asset Data will be repeated for each service tag -->
<AssetData>
<MANUFACTUREDATE>05/03/2013</MANUFACTUREDATE>
<REGION>DAO</REGION>
<EMBMAC1>D4BED9403E24</EMBMAC1>
<EMBMAC2>D4BED9403E24</EMBMAC2>
<EMBMAC3>D4BED9403E24</EMBMAC3>
<AC1>D4BED9403E24</AC1>
<MAC2>D4BED9403E24</MAC2>
<MAC3>D4BED9403E24</MAC3>
</AssetData>
</AssetDataCollection>
WHAT HAVE I TRIED
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml"/>
<xsl:template match="/">
<xsl:apply-templates select="/AssetDatCollection/AssetData/REF_ID_LIST/REF_ID"/>
</xsl:template>
<xsl:template match="REF_ID">
<xsl:variable name="newnam" select=" REF_ID[@NAME]"/>
<xsl:variable name=”newval” select=” REF_ID[@VALUE]”/>
<xsl:text><</xsl:text><xsl:value-of select="$newnam/@NAME"/><xsl:text>></xsl:text>
<xsl:value-of select=” REF_ID[@VALUE]”/> <xsl:text><</xsl:text><xsl:text>/</xsl:text><xsl:value-of select="$newnam/@NAME"/><xsl:text>></xsl:text>
</xsl:template>
Tried in http://www.freeformatter.com/xsl-transformer.html, I was not getting the correct output.
Upvotes: 1
Views: 337
Reputation: 167696
You need to use three templates, the first one is the identity transformation
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
the second one makes sure the REF_ID_LIST
just processes its children
<xsl:template match="REF_ID_LIST">
<xsl:apply-templates/>
</xsl:template>
and the third one transforms the REF_ID
elements by doing
<xsl:template match="REF_ID">
<xsl:element name="{@NAME}">
<xsl:value-of select="@VALUE"/>
</xsl:element>
</xsl:template>
Upvotes: 1