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

Re: parsing strings

Subject: Re: parsing strings
From: "Swen Thuemmler" <Swen.Thuemmler@xxxxxxxxxxxx>
Date: Tue, 8 May 2001 10:00:44 +0200 (MET DST)
parsing strings in c
On Mon, 7 May 2001, William Lam wrote:

> Hi,
> 
> I'm still relatively new to XSL.
> 
> I am looking for a string tokenizer, similar to the
> one available in the java library.

Well, you can do this with XSL, no need for a tokenizer, see below...

> I have a source node with delimited key/value pairs
> 
> <text>A:a;B:b;C:c;D:d</text>
> 
> I need to extract the key value pairs to look similar
> to
> 
> <items>
> <key>A</key>
> <value>a</value>
> <key>B</key>
> <value>b</value>
> <key>C</key>
> <value>c</value>
> <key>D</key>
> <value>d</value>
> </items>

Try the following stylesheet:

--------------------------8< snip >8-------------------------
<?xml version="1.0" encoding="iso-8859-1"?>

<xsl:stylesheet xmlns:xsl='http://www.w3.org/1999/XSL/Transform' version="1.0">

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

<xsl:template match="text">
  <items>
    <xsl:call-template name="process-tokens">
      <xsl:with-param name="tokenstring" select="."/>
      <xsl:with-param name="separator" select="';'"/>
    </xsl:call-template>
  </items>
</xsl:template>


<xsl:template name="process-tokens">
  <xsl:param name="tokenstring"/>
  <xsl:param name="separator"/>

  <xsl:variable name="pre">
    <xsl:choose>
      <xsl:when test="contains($tokenstring, $separator)">
        <xsl:value-of select="substring-before($tokenstring,$separator)"/>
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="$tokenstring"/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:variable>
  <xsl:variable name="post" select="substring-after($tokenstring,$separator)"/>

  <xsl:message>pre: <xsl:value-of select="$pre"/></xsl:message>
  <xsl:message>post: <xsl:value-of select="$post"/></xsl:message>
  <xsl:call-template name="process-single-token">
    <xsl:with-param name="token" select="$pre"/>
  </xsl:call-template>
  <xsl:if test="$post">
    <xsl:call-template name="process-tokens">
      <xsl:with-param name="tokenstring" select="$post"/>
      <xsl:with-param name="separator" select="$separator"/>
    </xsl:call-template>
  </xsl:if>
</xsl:template>

<xsl:template name="process-single-token">
  <xsl:param name="token"/>

  <xsl:if test="$token">
    <xsl:variable name="key" select="substring-before($token,':')"/>
    <xsl:variable name="value" select="substring-after($token,':')"/>

    <key>
      <xsl:value-of select="$key"/>
    </key>
    <value>
      <xsl:value-of select="$value"/>
    </value>
  </xsl:if>

</xsl:template>

</xsl:stylesheet>

--------------------------8< snip >8-------------------------

Hope this helps.

--Swen



 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


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.