XML To CSV with XSLT and Java
I have a Java program that generates an XML file that looks like the following:
<resultset>
<row>
<column name="id">123</column>
<column name="lastName">Jones</column>
<column name="firstName">Joe</column>
.
.
.
</row>
<row>
<column name="id">124</column>
<column name="lastName">Jhonson</column>
<column name="firstName">Jake</column>
.
.
.
</row>
.
.
.
</resultset>
I use this xml file to try and trnasform it into a comma delimited file by using the following xslt sheet:
<?xml version='1.0'?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:param name="dateLabel" />
<xsl:template match="/">
Clock/Id #, Last Name, First Name, Middle Initial ,Loc #, Date, Department, Oracle Acct #, Title, Business Group
<xsl:apply-templates select="//row" />
</xsl:template>
<xsl:template match="row">
<xsl:value-of select="column[@name='id']"/>, <xsl:value-of select="column[@name='lastName']"/>, <xsl:value-of select="column[@name='firstName']"/>, <xsl:value-of select="column[@name='middleInitial']"/>, <xsl:value-of select="column[@name='location']"/>, <xsl:value-of select="column[@name='date']"/>, <xsl:value-of select="column[@name='department']"/>, <xsl:value-of select="column[@name='account']"/>, <xsl:value-of select="column[@name='title']"/> ,<xsl:value-of select="column[@name='businessGroup']"/>
<xsl:text>#13;#10;</xsl:text>
</xsl:template>
</xsl:stylesheet>
The following is an excerpt of my Java code to transform it:
TransformerFactory tfactory = TransformerFactory.newInstance();
Transformer t = null;
t = tfactory.newTransformer(
new StreamSource(new File(props.getString("dial.csv.xsl"))));
StringWriter sw = new StringWriter();
StreamResult result = new StreamResult(sw);
t.transform(new DOMSource(document), result);
StringBuffer csv = sw.getBuffer();
The output is coming out all in one line with the exception of the headers. I have seen posting that the above should work but it is not working form me. The select="column directives in the xsl are all in one line.
Any suggestions?
|