xsl-list
[Top] [All Lists]

Re: [xsl] Tokenizing and transforming a CSV file

2009-02-26 02:16:07
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(_dot_)Honnen(_at_)gmx(_dot_)de> wrote:
Check whether http://andrewjwelch.com/code/xslt/csv/csv-to-xml_v2.html can
deal with your CSV.


-- 
Regards,
Mukul Gandhi

--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--