xslt count nodesets returned by key
In my xslt, I am trying to determine the number of distinct schedule_id
values in my <event/>
s so that I can output a table where each column is used for one schedule_id
. The following key should retrieve the nodesets grouped as I require. So how do I determine how many nodesets are returned?
<xsl:key name="events-by-schedule" match="event" use="@schedule_id" />
Example events with 3 (the number I'm trying to calculate) different schedule_id
s:
<event event_id="6" date="2009-05-27" schedule_id="4">
<ev_title>Ed's Bday</ev_title>
</event>
<event event_id="4" date="2009-11-11" schedule_id="0">
<ev_title>Remembrance Day</ev_title>
</event>
<event event_id="6" date="2009-08-17" schedule_id="23">
<ev_title>>Lunch with Bill</ev_title>
</event>
<event event_id="1" date="2009-12-25" schedule_id="0">
<ev_title>Christmas</ev_title>
</event>
<even开发者_开发问答t event_id="6" date="2009-05-02" schedule_id="4">
<ev_title>Beth's Bday</ev_title>
</event>
<event event_id="6" date="2009-10-01" schedule_id="23">
<ev_title>Performance Review</ev_title>
</event>
The XPath you are looking for is:
count(
event[
generate-id()
=
generate-id(key('events-by-schedule', @schedule_id)[1])
]
)
Basically, this is Muenchian grouping.
It counts all <event>
nodes that are first in their respective groups — in XPath terms this is: Their ID must match the ID of the first node of the group.
精彩评论