Re: [xsl] Tokenizing and transforming a CSV file

Subject: Re: [xsl] Tokenizing and transforming a CSV file
From: Mukul Gandhi <gandhi.mukul@xxxxxxxxx>
Date: Thu, 26 Feb 2009 12:45:38 +0530
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