****Note: title for this question should be...
********************************************
How to escape the commas in an element node in a CSV?
********************************************
I was wondering what the logic for encapsulating element nodes in an XSL is. Specifically, the XSL i wrote creates a CSV file and i'm trying to encapsulate all of the data elements in to double-quotes.
Using: XSLT 1.0, XPath 1.0
Source XML:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<xml>
<main>
<date year="2010" month="3" date="31" day="3"/>
<time hour="10" minute="38" second="55" timezone="Central"/>
<person first_name = "Bob" last_name = "Jackson">
<contents>
<content text="A very long sentence that will continue going for a very long time, possibly, it could be over a hundred words long and include multiple commas.../>
</contents>
</person>
<person first_name = "David" last_name = "Lee">
<contents>
<content text="A very long sentence that will continue going for a very long time, possibly, it could be over a hundred words long and include multiple commas.../>
</contents>
</person>
</main>
</xml>
XSL
Code:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml" indent="no" omit-xml-declaration="yes"/>
<xsl:template match="/">
<xsl:variable name="delim" select="','"/>
<outputfile>TEST_FILE.CSV</outputfile>&newline;
<xsl:for-each select="//person">
<xPerson= concat('@first_name, ' ',@last_name')"/>
<xsl:value-of select="concat($xPerson, $delim)"/>
<xsl:for-each select="contents/content">
<xsl:value-of select="concat(@text,"$delim")"/>
</xsl:for-each>
&newline;
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
The " above doesn't seem to be doing the trick.
Additionally, currently the XSL escapes from escaping ampersands (eg. when printing out a some thing like TEST&TEST... it outputs TEST&TEST). Is there a proper encoding that will allow this to happen automatically?