Simple XSLT questions.

Discussion in 'XML' started by anitawa, May 9, 2007.

  1. anitawa

    anitawa Guest

    Hello,

    I am stumped and need some help.
    I want to know how you can count the number of certain character
    inside one element.

    For example, counting commas.

    <place>CA,USA</place> should return 1

    <place>Los Angeles,CA,USA</place> should return 2

    <place>90210,Los Angeles,CA,USA</place> should return 3
     
    anitawa, May 9, 2007
    #1
    1. Advertising

  2. Use XSLT's string-manipulation functions. You may also have to write a
    recursive template to iterate through the string.

    --
    Joe Kesselman / Beware the fury of a patient man. -- John Dryden
     
    Joseph Kesselman, May 9, 2007
    #2
    1. Advertising

  3. In article <>,
    anitawa <> wrote:

    >I want to know how you can count the number of certain character
    >inside one element.
    >
    >For example, counting commas.
    >
    ><place>CA,USA</place> should return 1
    >
    ><place>Los Angeles,CA,USA</place> should return 2
    >
    ><place>90210,Los Angeles,CA,USA</place> should return 3


    Ok, here's a disgusting hack:

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

    -- Richard



    --
    "Consideration shall be given to the need for as many as 32 characters
    in some alphabets" - X3.4, 1963.
     
    Richard Tobin, May 10, 2007
    #3
  4. anitawa

    Pavel Lepin Guest

    Richard Tobin <> wrote in
    <f1tkup$6r3$>:
    > In article
    > <>,
    > anitawa <> wrote:
    >>I want to know how you can count the number of certain
    >>character inside one element.

    >
    > Ok, here's a disgusting hack:
    >
    > string-length(.)-string-length(translate(., ',', ''))


    Looks elegant and not at all disgusting to me. Better than
    coding a D&C named template to do something this trivial,
    anyway.

    --
    Pavel Lepin
     
    Pavel Lepin, May 10, 2007
    #4
  5. Pavel Lepin wrote:
    > Richard Tobin <> wrote in
    >> Ok, here's a disgusting hack:
    >> string-length(.)-string-length(translate(., ',', ''))

    >
    > Looks elegant and not at all disgusting to me. Better than
    > coding a D&C named template to do something this trivial,
    > anyway.


    I agree; that's a good solution to the comma-counting problem.

    (Cue the music: "Comma, comma, comma, Camellia; you come and go..."
    Though I have to admit I spent years wondering what a "karma chameleon"
    was.)

    --
    () ASCII Ribbon Campaign | Joe Kesselman
    /\ Stamp out HTML e-mail! | System architexture and kinetic poetry
     
    Joe Kesselman, May 10, 2007
    #5
  6. anitawa

    Pavel Lepin Guest

    OT WAS: Re: Simple XSLT questions.

    Joe Kesselman <> wrote in
    <>:
    > Pavel Lepin wrote:
    >> Richard Tobin <> wrote in
    >>> Ok, here's a disgusting hack:
    >>> string-length(.)-string-length(translate(., ',', ''))

    >>
    >> Looks elegant and not at all disgusting to me. Better
    >> than coding a D&C named template to do something this
    >> trivial, anyway.

    >
    > I agree; that's a good solution to the comma-counting
    > problem.
    >
    > (Cue the music: "Comma, comma, comma, Camellia; you come
    > and go..." Though I have to admit I spent years wondering
    > what a "karma chameleon" was.)


    I always thought it was 'come a million', which usually made
    me, well, a bit uneasy just thinking about it. But then, my
    English listening comprehension ain't anything I'd
    call 'good' even now, and it was even worse those days.

    --
    Pavel Lepin
     
    Pavel Lepin, May 10, 2007
    #6
  7. anitawa

    Peter Flynn Guest

    Joe Kesselman wrote:
    > Pavel Lepin wrote:
    >> Richard Tobin <> wrote in
    >>> Ok, here's a disgusting hack:
    >>> string-length(.)-string-length(translate(., ',', ''))

    >>
    >> Looks elegant and not at all disgusting to me. Better than
    >> coding a D&C named template to do something this trivial,
    >> anyway.

    >
    > I agree; that's a good solution to the comma-counting problem.
    >
    > (Cue the music: "Comma, comma, comma, Camellia; you come and go..."
    > Though I have to admit I spent years wondering what a "karma chameleon"
    > was.)


    I must be very warped. I got it first time I heard it.

    ///Peter
     
    Peter Flynn, May 14, 2007
    #7
  8. anitawa

    Pavel Lepin Guest

    Peter Flynn <> wrote in
    <>:
    > Joe Kesselman wrote:
    >> Pavel Lepin wrote:
    >>> Richard Tobin <> wrote in
    >>>> Ok, here's a disgusting hack:
    >>>> string-length(.)-string-length(translate(., ',', ''))
    >>>
    >>> Looks elegant and not at all disgusting to me. Better
    >>> than coding a D&C named template to do something this
    >>> trivial, anyway.

    >>
    >> I agree; that's a good solution to the comma-counting
    >> problem.

    >
    > I must be very warped. I got it first time I heard it.


    By the way, just to let you know, the OP is quite grateful
    for the solution. It's just that for some reason instead of
    expressing his gratitude to you on the group, he expressed
    his gratitude to you in my mail:

    On Thursday 10 May 2007 22:15, you wrote:
    [...]
    > Thats exactly what i was looking for.
    >
    > Thanks a lot!


    --
    Pavel Lepin
     
    Pavel Lepin, May 14, 2007
    #8
    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. Stylus Studio
    Replies:
    0
    Views:
    767
    Stylus Studio
    Aug 3, 2004
  2. Benjamin Hillsley
    Replies:
    3
    Views:
    1,749
    Dimitre Novatchev
    Sep 25, 2003
  3. ted
    Replies:
    1
    Views:
    654
    Laurens
    Jan 26, 2004
  4. Replies:
    2
    Views:
    739
    Henry S. Thompson
    Oct 19, 2005
  5. Replies:
    4
    Views:
    715
Loading...

Share This Page