Subject: Re: [xsl] parsing strings From: "Swen Thuemmler" <Swen.Thuemmler@xxxxxxxxxxxx> Date: Tue, 8 May 2001 10:00:44 +0200 (MET DST) |
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 |
---|
|
<- Previous | Index | Next -> |
---|---|---|
RE: [xsl] parsing strings, Paul Brown | Thread | RE: [xsl] parsing strings, Michael Kay |
RE: [xsl] How to avoid applying tem, Jarno Elovirta | Date | Re: [xsl] How to avoid applying tem, Jeni Tennison |
Month |