We are trying to convert XML to CSV using XSLT. I tried this link XML to CSV Using XSLT and Converting XML to CSV (using XSLT) .
- It is converting normal xml to csv but in my Case I am unable to get the desired output.
- In my case I have multiple records which are separated by space in a single tag as mentioned in below xml input.
Eg:- Project and Rating tag contains the multiple records separated by space.
Input XML:
<?xml version = "1.0"?>
<?xml-stylesheet type = "text/xsl" version="2.0" href = "csvconverted.xsl"?>
<TestData>
<project>Project-1 Project-2 Project-3</project>
<rating>2 3 5</rating>
<date>21-12-2018 21-06-2020 21-12-20</date>
</TestData>
XSL:-
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" encoding="utf-8" />
<xsl:param name="delim" select="','" />
<xsl:param name="quote" select="'"'" />
<xsl:param name="break" select="'
'" />
<xsl:template match="/">
<xsl:apply-templates select="TestData" />
</xsl:template>
<xsl:template match="TestData">
<xsl:apply-templates />
<xsl:if test="following-sibling::*">
<xsl:value-of select="$break" />
</xsl:if>
</xsl:template>
<xsl:template match="*">
<!-- remove normalize-space() if you want keep white-space at it is -->
<xsl:value-of select="concat($quote, normalize-space(), $quote)" />
<xsl:if test="following-sibling::*">
<xsl:value-of select="$delim" />
</xsl:if>
</xsl:template>
<xsl:template match="text()" />
</xsl:stylesheet>
Expected Output:
project,rating,date
Project-1,2,21-12-2018
Project-2,3,21-06-2020
Project-3,5,21-12-20
Actual Output using the above code:-
"Project-1 Project-2 Project-3","2 3 5","21-12-2018 21-06-2020 21-12-20"
Will be grateful if you could guide me further.