Re: [xsl] match string

Subject: Re: [xsl] match string
From: Anton Triest <anton@xxxxxxxx>
Date: Tue, 19 Oct 2004 15:39:58 +0200
Zsolt Szabs wrote:

Maybe I have found a solution. It works, at least with libxslt.
is this correct? I think is is rather a hack.

Didn't my solution work? I think your templates are correct but I'm not sure if they will produce correct results for paragraphs with 3 words or less. Also, I have the impression that they will only display the first 3 words and skip the rest?



<!--Select the first paragraph of all chapters--> <xsl:template match="//chapter/section[1]/para[1]" priority="1"> <xsl:call-template name="first_line"> <xsl:with-param name="string" select="."/> </xsl:call-template> </xsl:template>

<!--Select all other paragraphs-->
<xsl:template match="//para">
    <xsl:call-template name="para.template">
    </xsl:call-template>
</xsl:template>

<xsl:template name="first_line">
<xsl:param name="string"/>
<xsl:param name="string2"/>
<xsl:param name="string3"/>
<xsl:choose>
<!--are there any spaces???-->
<xsl:when test="contains($string, ' ')">
<div>
<b>
<!--1st word-->
<xsl:value-of select="substring-before($string,' ')"/>
<!--give back the space-->
<xsl:text> </xsl:text>
<!--2nd word-->
<xsl:value-of select="substring-before( substring-after ($string, ' '), ' ')"/>
<!--give back the space-->
<xsl:text> </xsl:text>
<!--3rd word-->
<xsl:value-of select="substring-before( substring-after ( substring-after ($string, ' '), ' '),' ')"/>
<!--give back the space-->
<xsl:text> </xsl:text>
</b>
</div>
</xsl:when>


            <xsl:otherwise>
                <xsl:call-template name="para.template">
                </xsl:call-template>
            </xsl:otherwise>

  </xsl:choose>
</xsl:template>

I don't know what your template name="para.template" does, but if you only want to split the words on the first paragraph of every chapter, and just copy all the others, you could do with an identity template and a match like David suggested:


<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>


   <xsl:output method="xml" version="1.0" encoding="utf-8" indent="yes"/>
   <xsl:strip-space elements="*"/>

<xsl:param name="split" select="3"/>

   <!-- identity template: copy all elements -->
   <xsl:template match="*">
       <xsl:copy>
           <xsl:copy-of select="@*"/>
           <xsl:apply-templates/>
       </xsl:copy>
   </xsl:template>

   <!-- override on first paragraph of each chapter -->
   <xsl:template match="chapter/section[1]/para[1]//text()[1]">
       <xsl:call-template name="splitwords"/>
   </xsl:template>

<xsl:template name="splitwords">
<xsl:param name="i" select="0"/>
<xsl:param name="str1" select="''"/>
<xsl:param name="str2" select="normalize-space(.)"/>
<xsl:choose>
<xsl:when test="$i = $split">
<b><xsl:value-of select="$str1"/></b>
<xsl:value-of select="$str2"/>
</xsl:when>
<xsl:otherwise>
<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:otherwise>
</xsl:choose>
</xsl:template>


</xsl:stylesheet>


HTH, Anton

Current Thread