XML-XSLT-CSV transformation -
i trying transform xml document csv through xslt. not able achieve desired result far.
xml below:
<projects> <project> <name>shockwave</name> <language>ruby</language> <owner>brian may</owner> <state>new</state> <startdate>31/10/2008 0:00:00</startdate> </project> <project> <name>other</name> <language>erlang</language> <owner>takashi miike</owner> <state> canceled </state> <startdate>07/11/2008 0:00:00</startdate> </project> </projects>
xslt below:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/xsl/transform"> <xsl:output method="text" encoding="iso-8859-1"/> <xsl:strip-space elements="*" /> <xsl:template match="/*/child::*"> <xsl:for-each select="child::*"> <xsl:if test="position() != last()"> <xsl:value-of select="normalize-space(.)"/>, </xsl:if> <xsl:if test="position() = last()"> <xsl:value-of select="normalize-space(.)"/><xsl:text>
</xsl:text> </xsl:if> </xsl:for-each> </xsl:template> </xsl:stylesheet>
while requirement transform csv below: header first row. , newline after startdate.
name,language,owner,state,startdate
shockwave, ruby, brian may,new,31/10/2008 0:00:00
other,erlang,takashi miike,canceled,07/11/2008 0:00:00
the problem statement
<xsl:if test="position() != last()"> <xsl:value-of select="normalize-space(.)"/>, </xsl:if>
you outputting comma, line break after comma output. xslt ignore white-space if whole text node whitespace. include non-whitespace character, output indentation too!
so, need change this...
<xsl:if test="position() != last()"> <xsl:value-of select="normalize-space(.)"/><xsl:text>,</xsl:text> </xsl:if>
having said that, can simplify xslt little bit more. try instead
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/xsl/transform"> <xsl:output method="text" encoding="iso-8859-1"/> <xsl:strip-space elements="*" /> <xsl:template match="/*/child::*"> <xsl:for-each select="child::*"> <xsl:if test="position() > 1"> <xsl:text>,</xsl:text> </xsl:if> <xsl:value-of select="normalize-space(.)"/> </xsl:for-each> <xsl:text>
</xsl:text> </xsl:template> </xsl:stylesheet>
edit: if wanted output header row, add template xslt
<xsl:template match="/*"> <xsl:for-each select="*[1]/*"> <xsl:if test="position() > 1"> <xsl:text>,</xsl:text> </xsl:if> <xsl:value-of select="local-name()"/> </xsl:for-each> <xsl:text>
</xsl:text> <xsl:apply-templates /> </xsl:template>
Comments
Post a Comment