xslt count nodesets returned by key

2019-09-06 03:44发布

问题:

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_ids:

<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>
<event 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>

回答1:

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.



标签: xslt count key