[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

CSV to XML transformation XSLT to include special cha

Subject: CSV to XML transformation XSLT to include special characters?
From: "Marney Cotterill" <marney@xxxxxxxxxxxxxxxxxxxx>
Date: Thu, 03 Apr 2008 09:33:10 +1000
 CSV to XML transformation XSLT to include special  cha
Hi Listers,

I have modified an XSLT orgignally developed by Andrew Welch to transform a 
CSV to an XML for importing data into a dynamic website. I am brand new to 
XSL and XSLT and have hit a small snag with the transform.

I've done some testing and I think it comes down to the inclusion of special 
characters in my CSV, which essentially breaks the transform, giving a blank 
result - it does not even display the error messege.

Using XML 1.0 and processing with Kernow.

XSLT Below:

<?xml version="1.0"?>
<!--
	A CSV to XML transform
	Version 2
	Andrew Welch
	http://andrewjwelch.com
	
	Modify or supply the $pathToCSV parameter and run the transform
	using "main" as the initial template.
	
	For bug reports or modification requests contact me at 
andrew.j.welch@xxxxxxxxx
-->
  		
<xsl:stylesheet version="2.0"
	xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
	xmlns:xs="http://www.w3.org/2001/XMLSchema"
  xmlns:msxsl="urn:schemas-microsoft-com:xslt"
	xmlns:fn="fn"
	exclude-result-prefixes="xs fn">

<xsl:output indent="yes" encoding="US-ASCII"/>

<xsl:param name="pathToCSV" select="'file:///c:/fish.csv'"/>

<xsl:function name="fn:getTokens" as="xs:string+">
	<xsl:param name="str" as="xs:string"/>
		<xsl:analyze-string select="concat($str, ',')" 
regex='(("[^"]*")+|[^,]*),'>
			<xsl:matching-substring>
				<xsl:sequence 
select='replace(regex-group(1), "^""|""$|("")""", "$1")'/>
			</xsl:matching-substring>
		</xsl:analyze-string>
</xsl:function>

<xsl:template match="/" name="main">
  <xsl:variable name="csvconverted">
  <xsl:choose>
    <xsl:when test="unparsed-text-available($pathToCSV)">
      <xsl:variable name="csv" select="unparsed-text($pathToCSV)"/>
      <xsl:variable name="lines" select="tokenize($csv, '&#xD;&#xA;')" 
as="xs:string+"/>
      <xsl:variable name="elemNames" select="fn:getTokens($lines[1])" 
as="xs:string+"/>
        <root>
          <xsl:for-each select="$lines[position() > 1]">
            <row>
              <xsl:variable name="lineItems" select="fn:getTokens(.)" 
as="xs:string+"/>
              <xsl:for-each select="$elemNames">
                <xsl:variable name="pos" select="position()"/>
                <xsl:element name="{.}">
                  <xsl:value-of select="$lineItems[$pos]"/>
                </xsl:element>
              </xsl:for-each>
            </row>
          </xsl:for-each>
        </root>
      </xsl:when>
    <xsl:otherwise>
      <xsl:text>Cannot locate : </xsl:text>
      <xsl:value-of select="$pathToCSV"/>
    </xsl:otherwise>
  </xsl:choose>
  </xsl:variable>
  <xsl:apply-templates select="$csvconverted/root"/>
</xsl:template>

  <xsl:template match="/root">
    <xmodexport version="4.0" generationdate="2008-03-27 22:52:04.984" 
portalid="0">
      <records>
        <xsl:apply-templates select="row">
        </xsl:apply-templates>
      </records>
    </xmodexport>
  </xsl:template>
  <xsl:template match="row">
    <record id="-1" formid="22" portalid="0" adduser="1" updateuser="1" 
approved="true" dateadded="2008-04-18 00:00:00.000" datemodified="2006-04-
18 00:00:00.000" displaydate="2006-04-17 00:00:00.000" expirydate="9999-12-
31 23:59:59.000">
      <InstanceData>
        <xsl:text disable-output-escaping="yes">&lt;![CDATA[</xsl:text>
        <instance>
          <xsl:copy-of select="./*"></xsl:copy-of>
        </instance>
        <xsl:text disable-output-escaping="yes">]]&gt;</xsl:text>
      </InstanceData>
    </record>
  </xsl:template>
  </xsl:stylesheet>

An excerpt from my CSV:

classdate,classname,classtime,classcode,classcost,xmodid,guestlink,classdesc
ription
3/03/2008,Sydney eats presentsV,6.30pm - 8.30pm,ALI,$75,Ashley 
Hughes,Ashley Hughes from Alio ,"Ashleyms time at Londonms River CafH and his 
travels throughout Italy have given him a simple, flexible cooking style typical of 
Italian cuisine.  At this hands-on dinner class hemll show you how he combines 
premium produce with simple fresh flavours to produce delicious Italian food."

The "H" in CafH is a special character. When I delete and replace with a 
normal"e" the stylesheet runs perfectly.

Is there a way to include these special charters into the XSLT?

Thanks, 
Marney

Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.