Re: [xsl] Create word elements from a string

Subject: Re: [xsl] Create word elements from a string
From: Mukul Gandhi <gandhi.mukul@xxxxxxxxx>
Date: Thu, 29 Oct 2009 10:09:55 +0530
Here's another 1.0 stylesheet (Dimitre, gave you one):

<xsl:stylesheet xmlns:xsl="";

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

    <xsl:template match="/">
	   <xsl:call-template name="tokenize">
	      <xsl:with-param name="str" select="text" />

   <xsl:template name="tokenize">
      <xsl:param name="str" />
	    <xsl:when test="contains($str, ' ')">
		  <xsl:value-of select="substring-before($str, ' ')" />
	       <xsl:call-template name="tokenize">
		   <xsl:with-param name="str" select="substring-after($str, ' ')" />
		   <xsl:value-of select="$str" />


This stylesheet implements tokenizing algorithm from scratch (and also
with a limitation, that tokenizing delimiter can be a single ' '
character). If you can use XSLT 2.0, you might prefer Ken's solution,
as XPath 2.0 has native tokenizing function. XPath 2.0 tokenizing
function allows you to, use say a regular expression specifier '\s+'
as a tokenizing delimiter.

On Thu, Oct 29, 2009 at 2:14 AM, Larry Hayashi <lhtrees@xxxxxxxxx> wrote:
> Is there a function in XSLT 1.1 that will extract words from a string?
> I'd like to be able to take <text>Jill ran up the hill.</text> and get
> the following:
> <words>
> B <word>Jill</word>
> B <word>ran</word>
> B <word>up</word>
> B <word>the</word>
> B <word>hill.</word>
> </words>
> Thanks,
> Larry

Mukul Gandhi

Current Thread