I have input xml with three fields (title, artist, country), I want an ouput xml with two fields(title and artist(concatenated), address(exchnaged with country) after transformation, Can any one help me to write a transformation xslt.
Input xml:
<?xml version="1.0" encoding="UTF-8"?>
<catalog>
<cd>
<title>Empire Burlesque</title>
<artist>Bob Dylan</artist>
<country>USA</country>
</cd>
<cd>
<title>Hide your heart</title>
<artist>Bonnie Tyler</artist>
<country>UK</country>
</cd>
<cd>
<title>Greatest Hits</title>
<artist>Dolly Parton</artist>
<country>USA</country>
</cd>
<cd>
<title>Still got the blues</title>
<artist>Gary Moore</artist>
<country>UK</country>
</cd>
<cd>
<title>Eros</title>
<artist>Eros Ramazzotti</artist>
<country>EU</country>
</cd>
</catalog>
Required output:
<?xml version="1.0" encoding="UTF-8"?>
<catalog>
<cd>
<title_and_artist>Empire Burlesque by Bob Dylan</title_and_artist>
<address>USA</address>
</cd>
<cd>
<title_and_artist>Hide your heart by Bonnie Tyler</title_and_artist>
<address>UK</address>
</cd>
<cd>
<title_and_artist>Greatest Hits by Dolly Parton</title_and_artist>
<address>USA</address>
</cd>
</catalog>
The xslt I used:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements=""/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="catalog/cd">
<xsl:copy>
<xsl:value-of select="concat(title, ' ' ,'by',' ', artist) "/>
</xsl:copy>
</xsl:template>
<xsl:template match="address/node()"/>
</xsl:stylesheet>
CodePudding user response:
Try this:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="cd">
<xsl:copy>
<title_and_artist>
<xsl:value-of select="concat(title, ' ' ,'by',' ', artist) "/>
</title_and_artist>
<address>
<xsl:value-of select="country"/>
</address>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>