Convert XML Attribute Value to Tag using XSLT
-
Wednesday, November 21, 2012 12:28 PM
Hi All, I have an XML file in the below format:
<MerchantData>
<field1 name="Channel Type">WEB</field1>
<field2 name="Part payment">No</field2>
</MerchantData>
I need to convert it to the format shown below using XSLT.
<MerchantData>
<Channel Type>WEB</Channel Type>
<Part payment>No</Part payment>
</MerchantData>
Any help on the XSLT code would be appreciated.
All Replies
-
Wednesday, November 21, 2012 4:53 PM
XML element names cannot contain spaces!!!
If your input is as follows:
<?xml version="1.0" encoding="utf-16"?>
<MerchantData>
<field1 name="Channel_Type">WEB</field1>
<field2 name="Part_payment">No</field2>
</MerchantData>The following XSLT should work:
<?xml version="1.0" encoding="utf-16"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" omit-xml-declaration="yes" indent="no" />
<xsl:template match="/">
<xsl:apply-templates select="MerchantData" />
</xsl:template>
<xsl:template match="MerchantData">
<MerchantData>
<xsl:for-each select="*">
<xsl:element name="{@name}">
<xsl:value-of select="." />
</xsl:element>
</xsl:for-each>
</MerchantData>
</xsl:template>
</xsl:stylesheet>Morten la Cour
- Edited by la Cour Wednesday, November 21, 2012 4:54 PM
-
Thursday, November 22, 2012 6:42 AMTHanks La Cour, appreciate your reply. My XML does have spaces, is there a way i can strip all spaces ?
-
Thursday, November 22, 2012 9:55 AM
This will remove all spaces:
<?xml version="1.0" encoding="utf-16"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" omit-xml-declaration="yes" indent="no" />
<xsl:template match="/">
<xsl:apply-templates select="MerchantData" />
</xsl:template>
<xsl:template match="MerchantData">
<MerchantData>
<xsl:for-each select="*">
<xsl:variable name="newName">
<xsl:call-template name="string-replace-all">
<xsl:with-param name="text" select="@name" />
<xsl:with-param name="replace" select="concat(' ','')" />
<xsl:with-param name="by" select="concat('','')" />
</xsl:call-template>
</xsl:variable>
<xsl:element name="{$newName}">
<xsl:value-of select="." />
</xsl:element>
</xsl:for-each>
</MerchantData>
</xsl:template>
<xsl:template name="string-replace-all">
<xsl:param name="text" />
<xsl:param name="replace" />
<xsl:param name="by" />
<xsl:choose>
<xsl:when test="contains($text, $replace)">
<xsl:value-of select="substring-before($text,$replace)" />
<xsl:value-of select="$by" />
<xsl:call-template name="string-replace-all">
<xsl:with-param name="text" select="substring-after($text,$replace)" />
<xsl:with-param name="replace" select="$replace" />
<xsl:with-param name="by" select="$by" />
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$text" />
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>Morten la Cour
- Marked As Answer by Ashish J Singh Friday, November 23, 2012 10:42 AM
-
Thursday, November 22, 2012 5:15 PM
Hi Morten, Appreciate all your help. The code works like a charm. It answers my questions, but I am stuck with 1 more problem here.
As you said "XML element names cannot contain spaces!!!" . Addtiionally they cant begin with numbers (0 to 9) and my data has numbers. What needs to be done to strip the numbers in addtion to spaces?
-
Friday, November 23, 2012 10:42 AM
I finally manged to solve it by using the translate function as shown below:
<xsl:element name="{translate($newName,'3','')}">
Morten, appreciate all your help. Thank you.

