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

Re: tokenizing comma separated string with quotes

Subject: Re: tokenizing comma separated string with quotes
From: "Andrew Welch" <andrew.j.welch@xxxxxxxxx>
Date: Thu, 22 Feb 2007 10:04:16 +0000
Re:  tokenizing comma separated string with quotes
On 2/21/07, Houghton,Andrew <houghtoa@xxxxxxxx> wrote:
> From: Andrew Welch [mailto:andrew.j.welch@xxxxxxxxx]
> Sent: 21 February, 2007 14:07
> To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
> Subject:  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>


Thanks Andrew - is there a modification to the regex to not include
the surrounding quotes?

eg

<token>"foo, bar"</token>

should be

<token>foo, bar</token>

I can translate away the quotes, but it would be nice if the regex
could be modified to do the same thing.

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.