Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old March 26th, 2004, 12:21 PM
Registered User
 
Join Date: Mar 2004
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default 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?

 
Old December 30th, 2005, 04:13 AM
Authorized User
 
Join Date: Dec 2005
Posts: 36
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to ramesh.kumarm
Default

Can you please send the [u]java code</u> that generates the xml file?


 
Old February 8th, 2012, 11:32 AM
Registered User
 
Join Date: Feb 2012
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I see that it's a very old post but does this have a solution? I'm running in to a similar problem.
 
Old February 8th, 2012, 11:55 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

The #10 and #13 on the last line should have been

Code:
&_#10;
without the underscore. But since this forum isn't good at handling display of such code sequences, that may have been what the original poster wrote.

I suggest you start a new post showing the code that you actually wrote.
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference





Similar Threads
Thread Thread Starter Forum Replies Last Post
Handle Null or Blank values using xslt in csv data mums XSLT 1 April 3rd, 2008 07:30 PM
CSV and XML with PHP NEO1976 PHP How-To 1 July 17th, 2006 02:33 AM
XML to CSV reloader2 XML 2 September 7th, 2005 08:51 AM
Problem applying simple XSLT to XML to create CSV gregclark XSLT 2 August 25th, 2005 07:30 AM
XSLT and CSV jjonesatuk XSLT 3 September 10th, 2003 05:12 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.