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

Re: match string

Subject: Re: match string
From: Anton Triest <anton@xxxxxxxx>
Date: Tue, 19 Oct 2004 11:56:35 +0200
xsl match
Zsolt Szabs wrote:

I am trying to mach the first 3 words of a node

I can mach the first word,
<xsl:value-of select="substring-before($string,' ')"/>

but how to match the other two?

The only thing sure is, that I have to mach the three spaces after the words.

Here's a solution with a recursive template:


<xsl:param name="split" select="3"/>
<xsl:template match="para">
<para><xsl:call-template name="splitwords"/></para>
</xsl:template>
<xsl:template name="splitwords">
<xsl:param name="i" select="0"/>
<xsl:param name="str1" select="''"/>
<xsl:param name="str2" select="."/>
<xsl:choose>
<xsl:when test="$i &lt; $split">
<xsl:choose>
<xsl:when test="contains($str2,' ')">
<xsl:call-template name="splitwords">
<xsl:with-param name="i" select="$i+1"/>
<xsl:with-param name="str1" select="concat($str1,substring-before($str2,' '),' ')"/>
<xsl:with-param name="str2" select="substring-after($str2,' ')"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:call-template name="splitwords">
<xsl:with-param name="i" select="$split"/>
<xsl:with-param name="str1" select="concat($str1,$str2)"/>
<xsl:with-param name="str2" select="''"/>
</xsl:call-template>
</xsl:otherwise>
</xsl:choose>
</xsl:when>
<xsl:otherwise>
<b><xsl:value-of select="$str1"/></b>
<xsl:value-of select="$str2"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>


It's a bit cumbersome, but it works:

<input>
   <para>A sentence begins with a number of words.</para>
   <para>Here's a paragraph with 6 words.</para>
   <para>Short paragraph</para>
</input>

<output>
  <para><b>A sentence begins </b>with a number of words.</para>
  <para><b>Here's a paragraph </b>with 6 words.</para>
  <para><b>Short paragraph</b></para>
</output>

This too is much easier in XSLT2 I guess?

Cheers,
Anton

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.