[xsl] String comparison

Subject: [xsl] String comparison
From: pankaj.c@xxxxxxxxxxxxxxxxxx
Date: Fri, 24 Sep 2010 11:49:50 +0530
Hello all,

I have a requirement, where in I am comparing the first and last page 
string and manipulating the last page value in output. Though there is 
nothing wrong in below stylesheet in terms of output but I am sure there 
must be smart way of doing this. May be with use of <xsl:variable>.

Any thoughts will be appreciated.

TIA,
Pankaj

Input
====
<pages><first-page>10635<first-page><last-page>10637<last-page></pages>

Output
=====
<pages><first-page>10635<first-page>-<last-page>7<last-page></pages>


Stylesheet
=======

    <xsl:template match="pages">
          <xsl:copy> 
            <xsl:apply-templates select="./first-page"/>
            <xsl:choose>
                <xsl:when test="./last-page">
                    <MIT><xsl:text>&#x2014;</xsl:text></MIT>
                    <xsl:apply-templates select="./last-page"/>
                </xsl:when>
            </xsl:choose>
        </xsl:copy> 
    </xsl:template>


<xsl:template match="last-page">
<xsl:copy>
 <xsl:choose>
     <xsl:when 
test="substring(.,1,1)=substring(preceding-sibling::first-page,1,1)">
        <xsl:if 
test="not(substring(.,2,1)=substring(preceding-sibling::first-page,2,1))">
                <xsl:value-of select="substring(.,2,string-length(.))"/>
        </xsl:if>
        <xsl:if 
test="not(substring(.,3,1)=substring(preceding-sibling::first-page,3,1))">
                <xsl:value-of select="substring(.,3,string-length(.))"/>
        </xsl:if>
        <xsl:if 
test="not(substring(.,4,1)=substring(preceding-sibling::first-page,4,1))">
                <xsl:value-of select="substring(.,4,string-length(.))"/>
        </xsl:if>
        <xsl:if 
test="not(substring(.,5,1)=substring(preceding-sibling::first-page,5,1))">
                <xsl:value-of select="substring(.,5,string-length(.))"/>
        </xsl:if>
        <xsl:if 
test="not(substring(.,6,1)=substring(preceding-sibling::first-page,6,1))">
                <xsl:value-of select="substring(.,6,string-length(.))"/>
        </xsl:if>
     </xsl:when>
 <xsl:otherwise>
     <xsl:apply-templates/>
 </xsl:otherwise>
 </xsl:choose>
 </xsl:copy>
 </xsl:template>

Current Thread