Shwetank Sharma
Shwetank Sharma

Reputation: 33

string comparison between two strings in xslt

I am using below code for checking whether two strings content are equal or not but it is not working as i am using watson explorer id

<xsl:variable name="str1" select="./td[4]"/>
<xsl:variable name="str2" select="./td[2]"/>

<xsl:if test="$str1 = $str2">
  true 
</xsl:if>

please tell me how to find using xslt whether the content of two strings are equal or not as i was trying by using compare(str1,str2) but it was also not working for me as i am using xslt 1.0 verson

Upvotes: 2

Views: 17589

Answers (1)

sreevathsa a
sreevathsa a

Reputation: 149

XML:

<Sample>`<AccountName1>SREE</AccountName1>`<AccountName2>SREE</AccountName2>`<SortCode>789-88-8907</SortCode>`<CardNumber>4545-6767-9876-8764</CardNumber>`<address>j-89-8999</address>`</Sample>

XSL:

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:regexp="http://exslt.org/regular-expressions" xmlns:dp="http://www.datapower.com/extensions" >
<xsl:output method="xml" indent="yes"/>
<xsl:template match="@*|node()"> 
<xsl:copy>
    <xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match = "*[local-name()='AccountName1' or local-name()='AccountName2']">
    <xsl:variable name = "var1">
 <xsl:value-of select = "/*[local-name()='Sample']/*[local-name()='AccountName1']/text()"/>
  </xsl:variable>
<xsl:variable name = "var2">
 <xsl:value-of select = "/*[local-name()='Sample']/*[local-name()='AccountName2']/text()"/>
</xsl:variable>
 <xsl:message dp:priority="debug"> First: <xsl:value-of select = "$var1"/> </xsl:message>
         <xsl:message dp:priority="debug"> Second: <xsl:value-of select = "$var2"/> </xsl:message>
<xsl:choose>
        <xsl:when test = "$var1=$var2">
<xsl:message dp:priority = "debug">Both are same </xsl:message>
                <xsl:copy>
                <xsl:value-of select = "."/>
            </xsl:copy>
</xsl:when>
        <xsl:otherwise>
            <xsl:message dp:priority = "debug">Both are different </xsl:message>
                <xsl:copy>
<xsl:value-of select="regexp:replace(*[local-name()='AccountName2'],'','',$var1)"/>
            </xsl:copy>
        </xsl:otherwise>
    </xsl:choose>
</xsl:template> 


</xsl:stylesheet> 

Upvotes: 1

Related Questions