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

Re: Create word elements from a string

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

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

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

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

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

</xsl:stylesheet>

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



--
Regards,
Mukul Gandhi

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.