Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Count number of character in a variable in XSLT 2.0

Tags:

xml

xslt

I have an XML which is describe below. This xml contains for example address node(address1). I just want to count the number of occurrence of character(,) in the address text. I am using template so just to mention I an storing address text into xslt variable.

Sample XML:

<xml>
    <address1>123 Fake Street, Alberta, Alberta, 234567</address1> 
    <address1>123 Fake Street, Alberta, Alberta</address1>
</xml> 

My XSLT:

<xsl:variable name="address" select="//*[local-name()='address1')]"/>
<xsl:variable name="totalComma" select="count(contains($address, ','))"/>
<xsl:choose>
<xsl:when test="$totalComma = 3">
    <!--Do something-->
</xsl:when>
<xsl:when test="$totalComma = 2">
    <!--Do something-->
</xsl:when>
</xsl:choose>

I don't know whether this is correct solution or not but didn't get expected result. A little help would be highly appreciable.

like image 483
Jyotish Singh Avatar asked Mar 13 '23 16:03

Jyotish Singh


1 Answers

Use string-length(foo) - string-length(translate(foo, ',', '')).

like image 78
Martin Honnen Avatar answered Mar 16 '23 00:03

Martin Honnen