0

I have a requirement to convert the following XML:

<?xml version="1.0" encoding="UTF-8"?>

 <cd>
    <title>Empire Burlesque</title>
    <artist>Bob Dylan</artist>
    <country>USA</country>
    <company>Columbia</company>
    <price>10.90</price>
    <year>1985</year>
  </cd>

The target XML that I require is something like this:

<params>
<param>
<key>title</key>
<value>Empire Burlesque</value>
</param>
<param>
<key>artist</key>
<value>Bob Dylan<</value>
</param>
<param>
<key>country</key>
<value>USA</value>
</param>
<param>
<key>company</key>
<value>Columbia</value>
</param>
<param>
<key>price</key>
<value>10.90</value>
</param>
<param>
<key>year</key>
<value>1985</value>
</param>
</params>

What should the XSL file look like for me to be able to achieve this?

1 Answer 1

1

I. XSLT 1.0

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="1.0">

    <xsl:output omit-xml-declaration="yes"/>

    <xsl:template match="/">
        <params>
            <xsl:apply-templates select="cd"/>
        </params>
    </xsl:template>

    <xsl:template match="cd/*">
        <param>
            <key><xsl:value-of select="name()"/></key>
            <value><xsl:value-of select="."/></value>
        </param>
    </xsl:template>

</xsl:stylesheet>

II. Output

<params>
    <param><key>title</key><value>Empire Burlesque</value></param>
    <param><key>artist</key><value>Bob Dylan</value></param>
    <param><key>country</key><value>USA</value></param>
    <param><key>company</key><value>Columbia</value></param>
    <param><key>price</key><value>10.90</value></param>
    <param><key>year</key><value>1985</value></param>
</params>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.