[XSL-LIST Mailing List Archive Home] [By Thread] [By Date]

Re: [xsl] Split the content of a variable (xpath 1.0)


Subject: Re: [xsl] Split the content of a variable (xpath 1.0)
From: Joern Nettingsmeier <nettings@xxxxxxxxxxxxxxxxxxxxxx>
Date: Fri, 03 Mar 2006 14:41:38 +0100

Silvia Liberto wrote:
Hi all,

I have a variable "Keywords" with several keywords inside.
They are seperated with ;.
so i want per xslt to split this content into single keywords,
like this:

<keywordset>
  <keyword>Keyword1</keyword>
  <keyword>Keyword2</keyword>
  <keyword>Keyword3</keyword>
  <keyword>Keyword4</keyword>
  <keyword>Keyword5</keyword>
</keywordset>

xml:

<Keywords>Keyword1; Keyword2; Keyword3; Keyword4; Keyword5;
Keyword6</Keywords>		

here's an xslt1.0 version without extension functions:


<xsl:template name="tokenize">
        <xsl:param name="inputString"/>
        <xsl:param name="separator" select="' '"/>
        <xsl:param name="resultElement" select="'item'"/>
        <xsl:variable
                name="token"
                select="substring-before($inputString, $separator)"
        />
        <xsl:variable
                name="nextToken"
                select="substring-after($inputString, $separator)"
        />
        <xsl:if test="$token">
                <xsl:element name="{$resultElement}">
                        <xsl:value-of select="$token"/>
                </xsl:element>
        </xsl:if>
        <xsl:if test="$nextToken">
                <xsl:call-template name="tokenize">
                        <xsl:with-param
                                name="inputString"
                                select="$nextToken"/>
                        <xsl:with-param
                                name="separator"
                                select="$separator"/>
                        <xsl:with-param
                                name="resultElement"
                                select="$resultElement"/>
                </xsl:call-template>
        </xsl:if>
</xsl:template>

and here's how you would use it to get what you want:


<xsl:template match="Keywords"> <keywords> <xsl:call-template name="tokenize"> <xsl:with-param name="inputString" select="."/> <xsl:with-param name="separator" select="';'"/> <xsl:with-param name="resultElement" select="'keyword'"/> </xsl:call-template> </keywords> </xsl:template>


-- jvrn nettingsmeier

home://germany/45128 essen/lortzingstr. 11/
http://spunk.dnsalias.org
phone://+49/201/491621

if you are a free (as in "free speech") software developer
and you happen to be travelling near my home, drop me a line
and come round for a free (as in "free beer") beer. :-D


Current Thread
Keywords