 |
| 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
|
|
|
|

April 6th, 2006, 10:19 AM
|
|
Registered User
|
|
Join Date: Feb 2006
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
transform csv file to xml
Given a comma separated and one heading (fixed number of columns) and no quotation csv sample file as following
Heading1,Heading2,Heading3,Heading4
value11,value12,value13,value14
value21,value22,value23,value24
How can I transform it to following or equivalent XML format. Do I need to use coding, if so? Can you show me the C++ code?
<?xml version="1.0"?>
<CSVData>
<Heading>
<h1>Heading1</h1>
<h2>Heading2</h2>
<h3>Heading3</h3>
<h4>Heading4</h4>
</Heading>
<Dataset>
<data>
<h1>value11</h1>
<h2>value12</h2>
<h3>value13</h3>
<h4>value14</h4>
</data>
<data>
<h1>value21</h1>
<h2>value22</h2>
<h3>value23</h3>
<h4>value24</h4>
</data>
</Dataset>
</CSVData>
|
|

April 6th, 2006, 10:28 AM
|
 |
Wrox Author
|
|
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
|
|
You can do this kind of up-conversion quite easily in XSLT 2.0:
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="2.0">
<xsl:template name="main">
<xsl:variable name="input" select="unparsed-text('data.csv')"/>
<CSVData>
<xsl:variable name="rows" select="tokenize($input, '\r?\n')"/>
<Heading>
<xsl:for-each select="tokenize($rows[1], ',')">
<xsl:element name="h{position()}">
<xsl:value-of select="."/>
</xsl:element>
</xsl:for-each>
</Heading>
<Dataset>
<xsl:for-each select="remove($rows, 1)">
<data>
<xsl:for-each select="tokenize(., ',')">
<xsl:element name="h{position()}">
<xsl:value-of select="."/>
</xsl:element>
</xsl:for-each>
</data>
</xsl:for-each>
</Dataset>
</CSVData>
</xsl:template>
</xsl:transform>
Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
|
|

April 10th, 2006, 07:43 AM
|
|
Registered User
|
|
Join Date: Feb 2006
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
I am limited to use the libxslt which only support XSLT 1.0. Any more idea about how to achieve the effort with XSLT transform and/or C++ coding?
|
|

April 10th, 2006, 09:38 AM
|
 |
Wrox Author
|
|
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
|
|
If you choose to limit yourself to XSLT 1.0, that's your choice.
Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
|
|

April 10th, 2006, 04:08 PM
|
|
Registered User
|
|
Join Date: Apr 2006
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Here's the basis of an XSLT 1.0 solution. Simply enclose your CSV data within an XML tag using CDATA.
XML CSV data:
<data><![CDATA[ ...your CSV data here... ]]></data>
Stylesheet:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
exclude-result-prefixes="xsl">
<xsl:output method="xml" encoding="utf-8" />
<xsl:variable name="newline" select="'#xa;'" />
<xsl:variable name="comma" select="','" />
<xsl:template match="/">
<data>
<xsl:apply-templates/>
</data>
</xsl:template>
<xsl:template match="text()">
<originalCSV>
<xsl:value-of select="." />
</originalCSV>
<xsl:call-template name="write-line" />
</xsl:template>
<xsl:template name="write-line">
<xsl:param name="text" select="." />
<xsl:variable name="this-row" select="substring-before( concat( $text, $newline ), $newline )" />
<xsl:variable name="remaining-rows" select="substring-after( $text, $newline )" />
<xsl:if test="string-length($this-row) > 1">
<row>
<xsl:call-template name="write-item">
<xsl:with-param name="line" select="$this-row" />
</xsl:call-template>
</row>
</xsl:if>
<xsl:if test="string-length( $remaining-rows ) > 0">
<xsl:call-template name="write-line">
<xsl:with-param name="text" select="$remaining-rows" />
</xsl:call-template>
</xsl:if>
</xsl:template>
<xsl:template name="write-item">
<xsl:param name="line"/>
<xsl:variable name="this-item" select="substring-before( concat( $line, $comma ), $comma)" />
<xsl:variable name="remaining-items" select="substring-after( $line, $comma )" />
<item>
<xsl:value-of select="$this-item" />
</item>
<xsl:if test="string-length( $remaining-items ) > 0">
<xsl:call-template name="write-item">
<xsl:with-param name="line" select="$remaining-items" />
</xsl:call-template>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
Antonio Collins
|
|

December 10th, 2012, 04:12 AM
|
|
Registered User
|
|
Join Date: Oct 2012
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
UTF-8 Problem
Hi
I used .xslt file to convert from csv to xml.
http://andrewjwelch.com/code/xslt/cs...to-xml_v2.html
But i have problem that in .csv file contain Unicode text, then xml cannot to export. It alway show "Cannot locale:..."
Could anyone help me?
Thanks
|
|

December 10th, 2012, 04:52 AM
|
 |
Wrox Author
|
|
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
|
|
Please start a new thread for a new question.
Andrew's script contains the lines:
<xsl:when test="unparsed-text-available($pathToCSV)">
<xsl:variable name="csv" select="unparsed-text($pathToCSV)" />
the functions unparsed-text() and unparsed-text-available() accept a second argument, which is the encoding of the file. If your file is encoded in UTF-8 (which is probably what you mean by saying it's Unicode), then try supplying the string "utf-8" as the second argument.
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
|
|

December 10th, 2012, 09:53 PM
|
|
Registered User
|
|
Join Date: Oct 2012
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
UTF-8 Problem
I am a newbie at this forum, i do not know how to post new thread, i will do it later.
<xsl:when test="unparsed-text-available($pathToCSV, 'utf-8')">
<xsl:variable name="csv" select="unparsed-text($pathToCSV, 'utf-8')"/>
I added 'utf-8' as second argument, but it still does not work.
The csv has content this line
23.11.2011;Mi;18:00;18:00;00:00;Safety;Jürgen 1131 User Lib;;
when i remove "Jürgen", then it works
Thanks for your help
|
|

December 11th, 2012, 04:57 AM
|
 |
Wrox Author
|
|
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
|
|
You need to know what encoding the file is in, and specify this encoding in your second argument.
If you're using some old-fashioned editor like Notepad on Windows to enter the text, and your machine is configured to a western European locale, then it's likely the encoding is windows cp1252. Try specifying "iso-8859-1".
Take a look inside the file with a hex editor to see what the actual bytes are. The unparsed-text function needs to decode bytes to characters, and it can't do that without knowing what the encoding is.
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
|
|
 |