XSL count specific characters in string

Discussion in 'XML' started by Jerry, Jan 13, 2005.

  1. Jerry

    Jerry Guest

    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
    Jerry, Jan 13, 2005
    #1
    1. Advertising

  2. Jerry

    Joris Gillis Guest

    Tempore 03:42:58, die Thursday 13 January 2005 AD, hinc in foro {comp.text.xml} scripsit Jerry <>:


    > 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,
    --
    Joris Gillis (http://www.ticalc.org/cgi-bin/acct-view.cgi?userid=38041)
    Vincit omnia simplicitas
    Keep it simple
    Joris Gillis, Jan 13, 2005
    #2
    1. Advertising

  3. Jerry

    Jerry Guest

    Joris -

    This is perfect and very clever.

    Much thanks,

    Jerry Orabona
    Jerry, Jan 13, 2005
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    1
    Views:
    3,600
    A. Bolmarcich
    May 27, 2005
  2. Behrang Dadsetan

    Deleting specific characters from a string

    Behrang Dadsetan, Jul 9, 2003, in forum: Python
    Replies:
    17
    Views:
    731
    Bengt Richter
    Jul 11, 2003
  3. Michael Chermside

    RE: Deleting specific characters from a string

    Michael Chermside, Jul 10, 2003, in forum: Python
    Replies:
    1
    Views:
    322
    Paul Rudin
    Jul 10, 2003
  4. Martin Nadoll

    count a specific character in a string

    Martin Nadoll, Feb 24, 2004, in forum: Javascript
    Replies:
    9
    Views:
    196
    Dr John Stockton
    Apr 1, 2004
  5. Researcher
    Replies:
    5
    Views:
    110
    Anno Siegel
    Sep 8, 2003
Loading...

Share This Page