简体   繁体   中英

how to generate an xml with name value pair from key value paired xml?

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!

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">&lt;</xsl:text><xsl:value-of select="(entry/string)[1]"/><xsl:text disable-output-escaping="yes">&gt;</xsl:text>
    <xsl:value-of select="(entry/string)[2]"/>
    <xsl:text disable-output-escaping="yes">&lt;</xsl:text>/<xsl:value-of select="(entry/string)[1]"/><xsl:text disable-output-escaping="yes">&gt;</xsl:text>
  </entry>
</xsl:template>
</xsl:stylesheet>

Hope it helps

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM