xslt counting

Discussion in 'XML' started by Herr Herrner, Mar 9, 2005.

  1. Herr Herrner

    Herr Herrner Guest

    hi,

    i'm trying to count something in xml via xsl and don't quite get it. i
    have something like (don't mind it's not logical)

    <doc>
    <s>
    <w type="a">how</w>
    <w type="b">are</w>
    <w type="a">you</w>
    </s>
    <s>
    <w type="c">i</w>
    <w type="a">am</w>
    <w type="b">fine</w>
    </s>
    <s>
    <w type="a">i</w>
    <w type="a">am</w>
    <w type="b">fine</w>
    </s>
    <s>
    <w type="a">i</w>
    <w type="a">am</w>
    <w type="b">fine</w>
    </s>
    </doc>

    i'm counting all <s>-elements with a <w type="a">-element as first
    element by doing:

    <xsl:value-of select="count(//s/w[position()=1 and contains(@type,'a')])"/>

    (results in "3" for the above example; i need to use "contains" since it
    can also look like type="a ff ee")

    but i'm stuck finding a proper query for (all <s> elements having a <w
    type="a"> element as first element and a <w type="b"> element as second)
    (should result in "1" for the above example) and for (all <s> elements
    having a <w type="a"> element as first element and a <w type="b">
    element as greater-than-second) (would resoult in "2" in the above example)

    Any help?

    Thanks,

    Herr Herrner
     
    Herr Herrner, Mar 9, 2005
    #1
    1. Advertising

  2. Herr Herrner

    Joris Gillis Guest

    Tempore 15:23:43, die Wednesday 09 March 2005 AD, hinc in foro {comp.text.xml} scripsit Herr Herrner <>:

    > i'm counting all <s>-elements with a <w type="a">-element as first
    > element


    Hi,

    > <xsl:value-of select="count(//s/w[position()=1 and contains(@type,'a')])"/>
    >
    > (results in "3" for the above example; i need to use "contains" since it
    > can also look like type="a ff ee")

    The abbreviated syntax would be:
    <xsl:value-of select="count(//s[w[1][contains(@type,'a')]])"/>

    > but i'm stuck finding a proper query for (all <s> elements having a <w
    > type="a"> element as first element and a <w type="b"> element as second)

    <xsl:value-of select="count(//s[w[1][contains(@type,'a')] and w[2][contains(@type,'b')]])"/>

    > (should result in "1" for the above example) and for (all <s> elements
    > having a <w type="a"> element as first element and a <w type="b">
    > element as greater-than-second) (would resoult in "2" in the above example)


    <xsl:value-of select="count(//s[w[1][contains(@type,'a')] and count(w[2] | w[contains(@type,'b')][1]) = 2 ])"/>


    regards,
    --
    Joris Gillis (http://www.ticalc.org/cgi-bin/acct-view.cgi?userid=38041)
    Fiat W3C in tenebris
     
    Joris Gillis, Mar 9, 2005
    #2
    1. Advertising

  3. Herr Herrner

    Herr Herrner Guest

    Thanks a lot! That did the trick. My second "wish" was simply
    accomplished by:

    <xsl:value-of select="count(//s[w[1][contains(@type,'a')] and
    w[position()>2][contains(@type,'b')]])"/>

    so what does your last example do?!

    Thanks again,

    Herr Herrner


    Joris Gillis wrote:
    > Tempore 15:23:43, die Wednesday 09 March 2005 AD, hinc in foro
    > {comp.text.xml} scripsit Herr Herrner <>:
    >
    >> i'm counting all <s>-elements with a <w type="a">-element as first
    >> element

    >
    >
    > Hi,
    >
    >> <xsl:value-of select="count(//s/w[position()=1 and
    >> contains(@type,'a')])"/>
    >>
    >> (results in "3" for the above example; i need to use "contains" since it
    >> can also look like type="a ff ee")

    >
    > The abbreviated syntax would be:
    > <xsl:value-of select="count(//s[w[1][contains(@type,'a')]])"/>
    >
    >> but i'm stuck finding a proper query for (all <s> elements having a <w
    >> type="a"> element as first element and a <w type="b"> element as second)

    >
    > <xsl:value-of select="count(//s[w[1][contains(@type,'a')] and
    > w[2][contains(@type,'b')]])"/>
    >
    >> (should result in "1" for the above example) and for (all <s> elements
    >> having a <w type="a"> element as first element and a <w type="b">
    >> element as greater-than-second) (would resoult in "2" in the above
    >> example)

    >
    >
    > <xsl:value-of select="count(//s[w[1][contains(@type,'a')] and count(w[2]
    > | w[contains(@type,'b')][1]) = 2 ])"/>
    >
    >
    > regards,
     
    Herr Herrner, Mar 9, 2005
    #3
  4. Herr Herrner

    Joris Gillis Guest

    Tempore 16:12:31, die Wednesday 09 March 2005 AD, hinc in foro {comp.text.xml} scripsit Herr Herrner <>:

    > <xsl:value-of select="count(//s[w[1][contains(@type,'a')] and
    > w[position()>2][contains(@type,'b')]])"/>
    >
    > so what does your last example do?!
    >

    The difference is that my last example would NOT count:

    <s>
    <w type="a">how</w>
    <w type="b">are</w>
    <w type="b">you</w>
    </s>

    while your XPath would include it in counting.

    I could not figure out what the wished behaviour was, so which is it?

    --
    Joris Gillis (http://www.ticalc.org/cgi-bin/acct-view.cgi?userid=38041)
    Fiat W3C in tenebris
     
    Joris Gillis, Mar 9, 2005
    #4
  5. Herr Herrner

    Herr Herrner Guest

    > I could not figure out what the wished behaviour was, so which is it?
    I can't say really, since something like

    <s>
    <w type="a">how</w>
    <w type="b">are</w>
    <w type="b">you</w>
    </s>

    won't happen in the "real thing", but if it was to happen, your example
    would be the better one. Thanks again,

    Herr Herrner
     
    Herr Herrner, Mar 9, 2005
    #5
    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:
    773
    Stylus Studio
    Aug 3, 2004
  2. Lizard
    Replies:
    3
    Views:
    4,480
    Lizard
    Oct 7, 2003
  3. Graham

    Help Grouping/Counting XSLT

    Graham, Sep 17, 2004, in forum: XML
    Replies:
    3
    Views:
    590
    =?ISO-8859-1?Q?J=FCrgen_Kahrs?=
    Sep 17, 2004
  4. John Galenski

    XSLT Grouping and Counting Question

    John Galenski, Feb 6, 2009, in forum: XML
    Replies:
    1
    Views:
    1,980
    Martin Honnen
    Feb 6, 2009
  5. edwardfredriks

    counting up instead of counting down

    edwardfredriks, Sep 6, 2005, in forum: Javascript
    Replies:
    6
    Views:
    241
    Dr John Stockton
    Sep 7, 2005
Loading...

Share This Page