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

String comparison

Subject: String comparison
From: pankaj.c@xxxxxxxxxxxxxxxxxx
Date: Fri, 24 Sep 2010 11:49:50 +0530
 String comparison
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

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.