Input XML:
<root>
<data>
<Data>
<rt>ArgoElectrnicsShop</rt>
<report>
<row>
<tn>new tv</tn>
<value>Samsung</value>
</row>
<row>
<tn>new radio</tn>
<value>Sony</value>
</row>
<row>
<tn>new WashingMachine</tn>
<value>Whirlpool</value>
</row>
</report>
</Data>
</data>
</root>
XSL:
<xsl:output method="xml" indent="yes"/>
<xsl:template match="@* | node()"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/">
<xsl:element name="DataModel">
<xsl:apply-templates select="/root/data/Data/rt"/>
</xsl:element>
</xsl:template>
<xsl:template match="/root/data/Data/rt[text()='ArgoElectrnicsShop']">
<xsl:element name="T_New">
<xsl:apply-templates select="../report" mode="check"/>
</xsl:element>
</xsl:template>
<xsl:template match="report" mode="check">
<xsl:variable name="lowercase" select="'abcdefghijklmnopqrstuvwxyz:"/>
<xsl:variable name="uppercase" select="'ABCDEFGHIJKLMNOPQRSTUVWXYZ'"/>
<xsl:variable name="EntireValue">
<xsl:value-of select="row[substring-after(tn, ' ')]/tn"/>
</xsl:variable>
<xsl:variable name="Field">
<xsl:value-of select="row[substring-after($EntireValue, ' ')]"/>
</xsl:variable>
<xsl:call-template name="sample">
<xsl:with-param name="tn" select="translate($EntireValue,$lowercase,$uppercase)"/>
<xsl:with-param name="value" select="row[substring-after(tn, ' ')=$Field]/value"/>
</xsl:call-template>
</xsl:template>
<xsl:template name="sample">
<xsl:param name="tn"/>
<xsl:param name="value"/>
<xsl:choose>
<xsl:when test="substring-after($tn, ' ')='TV'">
<xsl:element name="tv">
<xsl:value-of select="$value"/>
</xsl:element>
</xsl:when>
<xsl:when test="substring-after($tn, ' ')='RADIO'">
<xsl:element name="radio">
<xsl:value-of select="$value"/>
</xsl:element>
</xsl:when>
<xsl:when test="substring-after($tn, ' ')='WASHINGMACHINE'">
<xsl:element name="washingmachine">
<xsl:value-of select="$value"/>
</xsl:element>
</xsl:when>
</xsl:choose>
</xsl:template>
**Output XML:**
<DataModel>
<T_New>
<tv>Samsung</tv>
</T_New>
</DataModel>
**Expected Output XML by running through all rows:**
<DataModel>
<T_New>
<tv>Samsung</tv>
<radio>Sony</radio>
<washingmachine>Whirlpool</washingmachine>
</T_New>
</DataModel>
- I want to run the loop for all the rows to get the values. The values inside the tn may be of any case, so i have taken the value from the tn of a row and translated it to uppercase and compared the value in the template sample and written the correct values in the output xml.
- I could not run through all the rows in the xml. Could anyone help me to solve this.