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

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

Subject: Re: Split the content of a variable (xpath 1.0)
From: Joern Nettingsmeier <nettings@xxxxxxxxxxxxxxxxxxxxxx>
Date: Fri, 03 Mar 2006 14:41:38 +0100
tokenize xsl 1.0
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

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-2011 All Rights Reserved.