xml - Output the difference in character XSLT -
i have 2 variables $word1 , $word2 , values are:
$word1 = 'america' $word2 = 'american'
now using xslt
have compare both variables , output difference in character.
for example output has 'n'. how in xslt 1.0
??
i found function called index-of-string
in xslt 2.0!!
depends on mean 'difference'. check if $word2
starts $word1
, return remaining part can do:
substring-after($word2,$word1)
that returns 'n' in example.
if need check if $word1
appears anywhere within $word2
- , return parts of $word2
before/after $word1
have use recursive template:
<xsl:template name="substring-before-after"> <xsl:param name="prefix"/> <xsl:param name="str1"/> <xsl:param name="str2"/> <xsl:choose> <xsl:when test="string-length($str1)>=string-length($str2)"> <xsl:choose> <xsl:when test="substring($str1,1,string-length($str2))=$str2"> <xsl:value-of select="concat($prefix,substring($str1,string-length($str2)+1))"/> </xsl:when> <xsl:otherwise> <xsl:call-template name="substring-before-after"> <xsl:with-param name="prefix" select="concat($prefix,substring($str1,1,1))"/> <xsl:with-param name="str1" select="substring($str1,2)"/> <xsl:with-param name="str2" select="$str2"/> </xsl:call-template> </xsl:otherwise> </xsl:choose> </xsl:when> <xsl:otherwise> <xsl:text></xsl:text> </xsl:otherwise> </xsl:choose> </xsl:template>
that call this:
<xsl:call-template name="substring-before-after"> <xsl:with-param name="prefix" select="''"/> <xsl:with-param name="str1" select="$word2"/> <xsl:with-param name="str2" select="$word1"/> </xsl:call-template>
this return still 'n' in example, , returns 'an' if `$word1 = 'merica' etc..
note approach returns empty string if 2 strings identical , if second string not contained in first one. can modify returning kind of 'special' in second case modifying last otherwise
:
<xsl:otherwise> <xsl:text>[special string]</xsl:text> </xsl:otherwise>
Comments
Post a Comment