i have an XML generated in the below form from JSON to XML Conversion.
<entry>
<string>dataset_code</string>
<string>GDP</string>
</entry>
How to convert this into the below format in XML?
<entry>
<dataset_code>GDP</dataset_code>
</entry>
Note: Here the key-value pairs(dataset_code, GDP, entry) are dynamic.
Any help on this would be highly appreciated!
In XSLT, this would be done as:
<xsl:template match="/*">
<xsl:copy>
<xsl:element name="{string[1]}">
<xsl:value-of select="string[2]" />
</xsl:element>
</xsl:copy>
</xsl:template>
Note that this can easily fail if the first string is not a valid XML element name.
I'm not an expert but playing with http://www.freeformatter.com/xsl-transformer.html I've found this solution:
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/">
<entry>
<xsl:text disable-output-escaping="yes"><</xsl:text><xsl:value-of select="(entry/string)[1]"/><xsl:text disable-output-escaping="yes">></xsl:text>
<xsl:value-of select="(entry/string)[2]"/>
<xsl:text disable-output-escaping="yes"><</xsl:text>/<xsl:value-of select="(entry/string)[1]"/><xsl:text disable-output-escaping="yes">></xsl:text>
</entry>
</xsl:template>
</xsl:stylesheet>
Hope it helps