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

Re: Tokenizing and transforming a CSV file

Subject: Re: Tokenizing and transforming a CSV file
From: Mukul Gandhi <gandhi.mukul@xxxxxxxxx>
Date: Thu, 26 Feb 2009 12:45:38 +0530
Re:  Tokenizing and transforming a CSV file
Thanks, Martin for the reference.

Andrew's solution is quite close to my requirement.

The stylesheet I am using is (I am using the fn:getTokens function
written by Andrew):

<?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"
                xmlns:fn="http://fn"
                version="2.0" exclude-result-prefixes="xs fn">

  <xsl:output method="xml" indent="yes" omit-xml-declaration="yes" />

  <xsl:template match="/">
    <xsl:for-each select="fn:getTokens(x)">
       <field>
          <xsl:value-of select="." />
       </field>
    </xsl:for-each>
  </xsl:template>

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

with the input:

<x>hi,"this is a long string, please tokenize me",hello,world</x>

I get the correct output:

<field>hi</field>
<field>this is a long string, please tokenize me</field>
<field>hello</field>
<field>world</field>

but with following input:

<x>hi,abc "this is a long string, please tokenize me",hello,world</x>

I am getting output:

<field>hi</field>
<field>abc "this is a long string</field>
<field> please tokenize me</field>
<field>hello</field>
<field>world</field>

but the output should be:

<field>hi</field>
<field>abc "this is a long string, please tokenize me"</field>
<field>hello</field>
<field>world</field>

Any further help regarding this would be much appreciated.

On Wed, Feb 25, 2009 at 10:24 PM, Martin Honnen <Martin.Honnen@xxxxxx> wrote:
> Check whether http://andrewjwelch.com/code/xslt/csv/csv-to-xml_v2.html can
> deal with your CSV.


-- 
Regards,
Mukul Gandhi

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-2011 All Rights Reserved.