XSL count specific characters in string

J

Jerry

Hi -

Using XSL I need to count the number of times character appears in a
string. My guess is I need to recurse through the value of x, but I'm
having trouble getting my head around the solution. In the example
below I'd like to count the number of '-' in x and write the sum to y.


<x>10-10-10</x>
<x>10-10-100</x>
<x>10-100-10</x>
<x>10-10</x>
<x>10-10-10-100</x>

<y>2</y>
<y>2</y>
<y>2</y>
<y>1</y>
<y>3</y>
Can anyone point me in the right direction?

Thanks,

Jerry
 
J

Joris Gillis

Using XSL I need to count the number of times character appears in a
string. My guess is I need to recurse through the value of x, but I'm
having trouble getting my head around the solution. In the example
below I'd like to count the number of '-' in x and write the sum to y.

If the string to search for contains only one character, like in your example, you might use this:

<xsl:template match="x">
<y><xsl:value-of select="string-length(.)-string-length(translate(.,'-',''))"/></y>
</xsl:template>

regards,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,999
Messages
2,570,243
Members
46,836
Latest member
login dogas

Latest Threads

Top