RE: [xsl] tokenizing comma separated string with quotes

Subject: RE: [xsl] tokenizing comma separated string with quotes
From: "Houghton,Andrew" <houghtoa@xxxxxxxx>
Date: Wed, 21 Feb 2007 15:35:22 -0500
> From: Andrew Welch [mailto:andrew.j.welch@xxxxxxxxx]
> Sent: 21 February, 2007 14:07
> To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
> Subject: [xsl] tokenizing comma separated string with quotes
>
> Given the input
>
> <elem>"foo, bar", baz, bom</elem>
>
> Is there a nice one liner  / technique to return the three
> tokens "foo, bar"  "baz" "bom"
>
> eg:
>
> <root>
>   <token>foo, bar</token>
>   <token>baz</token>
>   <token>bom</token>
> </root>
>
> I can't see the answer for all the apparent quote escaping required...

If you are using XSL 2.0, this should work:


Input file:
<elem>"foo, bar", baz, bom</elem>


Transform file:
<xsl:transform version="2.0"
  exclude-result-prefixes="xsd xsi xsl"

  xmlns:xsd="http://www.w3.org/2001/XMLSchema";
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform";

>

  <xsl:output method="xml" version="1.0"
    media-type="text/xml" encoding="utf-8"
    omit-xml-declaration="yes" indent="yes"
  />

  <xsl:variable name="regex">
    <xsl:text>\s*("[^"]*"|[^,]+)\s*</xsl:text>
  </xsl:variable>

  <xsl:template match="/">
    <root>
      <xsl:analyze-string regex="{$regex}" select="/elem">
        <xsl:matching-substring>
          <token><xsl:value-of select="regex-group(1)"/></token>
        </xsl:matching-substring>
      </xsl:analyze-string>
    </root>
  </xsl:template>

</xsl:transform>


Output file:
<root>
   <token>"foo, bar"</token>
   <token>baz</token>
   <token>bom</token>
</root>

Current Thread