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 August 27th, 2013, 10:31 AM
Registered User
 
Join Date: Aug 2013
Posts: 6
Thanks: 1
Thanked 0 Times in 0 Posts
Default Csv to xml with variable number of columns

I was asked to come up with a solution to take a .csv file to xml for data transfer from one program to another. I am running Widows 7 but when finished it will run on Server 2003 and Server 2008. I am using Saxon v9.5 for the xslt. I'm having trouble getting the file to parse.

The .csv file looks like:

AA_00002,EUTANKID2,08/20/2013,91626020,,08/20/2013,43
AA_00003,EUTANKID3,08/20/2013,91625870,,08/20/2013,88
AA_00004,EUTANKID4,08/20/2013,91626011,,08/20/2013,50
AA_00005,EUTANKID5,08/20/2013,1625911,,08/20/2013,63
AA_00006,EUTANKID6,08/20/2013,0,,08/20/2013,76
AA_00007,EUTANKID7,08/20/2013,91625890,,08/20/2013,120

I run it with the following stylesheet:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"
>

<xsl:param name="identifier"/>
<xsl:param name="uri"/>

<xsl:template match="/">
<xsl:variable name="filename" select="'file:///C:/Users/rjacobs/Desktop/DJBASIN_EUTANK_exportout.csv'"/>
<xsl:if test="unparsed-text-available($filename)">
<xsl:value-of select="unparsed-text($filename)"/>
</xsl:if> "
<xsl:variable name="CsvExport" select="unparsed-text($filename)"/>
<xsl:variable name="data">
<list>
<xsl:analyze-string select="$CsvExport" regex="\n">
<xsl:non-matching-substring>
<row>
<xsl:analyze-string select="." regex="," flags="x">
<xsl:non-matching-substring>
<col>
<xsl:value-of select="normalize-space(.)"/>
</col>
</xsl:non-matching-substring>
</xsl:analyze-string>
</row>
</xsl:non-matching-substring>
</xsl:analyze-string>
</list>
</xsl:variable>
</xsl:template>
</xsl:stylesheet>

and get this as an output:

<?xml version="1.0" encoding="UTF-8"?>AA_00002,EUTANKID2,08/20/2013,91626020,,08/20/2013,43
AA_00003,EUTANKID3,08/20/2013,91625870,,08/20/2013,88
AA_00004,EUTANKID4,08/20/2013,91626011,,08/20/2013,50
AA_00005,EUTANKID5,08/20/2013,1625911,,08/20/2013,63
AA_00006,EUTANKID6,08/20/2013,0,,08/20/2013,76
AA_00007,EUTANKID7,08/20/2013,91625890,,08/20/2013,120

Then I run a second pass with this stylesheet:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"
>

<xsl:param name="identifier"/>
<xsl:param name="uri"/>
<xsl:variable name="data"/>
<xsl:template match="/">
<xsl:variable name="filename" select="'file:///E:/Saxon/final_test.xml'"/>
<xsl:for-each select="$filename//row">
<EUExport>
<FacId>
<EUTankId>
<xsl:value-of select="col[1]"/>
</EUTankId>
<FacilityId>
<xsl:value-of select="col[0]"/>
</FacilityId>
<Date>
<xsl:value-of select="col[5]"/>
</Date>
<TankId>
<xsl:value-of select="col[3]"/>
</TankId>
<TankLevel>
<xsl:value-of select="col[6]"/>
</TankLevel>
<TankInterface>
<xsl:text></xsl:text>
</TankInterface>
</FacId>
</EUExport>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>

The output is the same as the input.
 
Old August 27th, 2013, 11:37 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

Well you appear to be storing some XML (if indeed your XSLT works, I haven't checked) into a variable called "data". But you never output the data variable.

The output you are seeing is from inside the xsl:if statement.
__________________
/- Sam Judson : Wrox Technical Editor -/

Think before you post: What have you tried?
The Following User Says Thank You to samjudson For This Useful Post:
rodjacobs (August 27th, 2013)
 
Old August 27th, 2013, 12:41 PM
Registered User
 
Join Date: Aug 2013
Posts: 6
Thanks: 1
Thanked 0 Times in 0 Posts
Default

Thanks samjudson. It is erroring out. I tried to run the csv to xml example from chapter 8 in Beginning XSLT and Xpath Transforming XML Documents and Data from the code downloads and it error's out also.

I get the following error:

SXXP0003: Error reported by XML parser: Content is not allowed in prolog.

I am using parser org.apache.xerces.jaxp.SAXParserImpl$HAXPSAXParser
 
Old August 28th, 2013, 09:55 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

Sounds like you are trying to load the CSV file as an XML file.

I think if you are using JAXP to call Saxon then you'll need to pass a dummy XML file to the transformation. If you use Saxon directly then you can call it with a named main template.

And you can get rid of the msxml namespace - that is the Microsoft namespace, and you wonuldn't be able to use that in Java XSLT even if you wanted to.
__________________
/- Sam Judson : Wrox Technical Editor -/

Think before you post: What have you tried?
 
Old August 28th, 2013, 05:43 PM
Registered User
 
Join Date: Aug 2013
Posts: 6
Thanks: 1
Thanked 0 Times in 0 Posts
Default

You were right samjudson. I passed in a dummy xml file and used the code below and it's working great.
Code:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    exclude-result-prefixes="xs"
    version="2.0">
    
    <xsl:param name="identifier"/>
    <xsl:param name="uri" select="'file:///E:/XSLT/CsvXmlCsv_XSLT/filename.csv'"/>
    
    <xsl:template match="/">
        <xsl:variable name="csv" select="unparsed-text($uri)"/>
        <xsl:variable name="data">
            <data>
                <xsl:analyze-string select="$csv" regex="\n">
                    <xsl:non-matching-substring>
                        <row>
                            <xsl:analyze-string select="." regex="," flags="x">
                                <xsl:non-matching-substring>
                                    <col>
                                        <xsl:value-of select="normalize-space(.)"/>
                                    </col>
                                </xsl:non-matching-substring>
                            </xsl:analyze-string>
                        </row>
                    </xsl:non-matching-substring>
                </xsl:analyze-string>
            </data>
        </xsl:variable>
        
        <xsl:result-document href="CsvToXml.xml">
            <Export xml:id="{$identifier}">
                <FacID>
                    <xsl:for-each select="$data//row">
                        <ID>
                            <xsl:attribute name="FacilityId">
                                <xsl:value-of select="col[1]"/>
                            </xsl:attribute>
                            <xsl:attribute name="TankId">
                                <xsl:value-of select="col[4]"/>
                            </xsl:attribute>
                            <xsl:attribute name="Date">
                                <xsl:value-of select="col[6]"/>
                            </xsl:attribute>
                            <xsl:attribute name="TankLevel">
                                <xsl:value-of select="col[7]"/>
                            </xsl:attribute>
                            <xsl:attribute name="Interface">
                                <xsl:text></xsl:text>
                            </xsl:attribute>>
                            <xsl:value-of select="col[2]"/>
                        </ID>
                    </xsl:for-each>
                </FacID>
            </Export>
        </xsl:result-document>
    </xsl:template>
</xsl:stylesheet>





Similar Threads
Thread Thread Starter Forum Replies Last Post
Convert XML to CSV with XML field names as column headers? swheeler XSLT 10 February 10th, 2012 11:18 AM
Load VARIABLE Number of columns cosmokramer Oracle 1 November 2nd, 2008 05:51 PM
SELECT VARIABLE Number of columns drani Oracle 4 September 21st, 2008 01:12 AM
Read CSV file - Save Columns into Rows pintoo BOOK: Professional SQL Server 2005 Integration Services ISBN: 0-7645-8435-9 1 August 20th, 2008 09:01 AM
Dynamic number of columns returned from sp dreadjr BOOK: Professional SQL Server 2005 Reporting Services ISBN: 0-7645-8497-9 1 July 5th, 2006 03:42 PM





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