xslt counting

H

Herr Herrner

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
 
J

Joris Gillis

Tempore 15:23:43 said:
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:
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)

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


regards,
 
H

Herr Herrner

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 said:
Tempore 15:23:43, die Wednesday 09 March 2005 AD, hinc in foro
{comp.text.xml} scripsit Herr Herrner said:
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:
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,
 
J

Joris Gillis

Tempore 16:12:31 said:
<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?
 
H

Herr Herrner

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
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top