Re: [xsl] Grouping with second choice

Subject: Re: [xsl] Grouping with second choice
From: Jeni Tennison <mail@xxxxxxxxxxxxxxxx>
Date: Tue, 31 Jul 2001 14:12:15 +0100
Hi Thorsten,

> I want to group students according to their online registration
> which is exported to xml similar to the file attached below
> (students.xml). Registration allows students to express a first and
> a second choice for their exercise. Using the Muenchian Method is
> appropriate for sorting with respect to a single key, but how do I
> realize grouping with constraints, e.g. use the second choice if the
> first cannot be fulfilled (group is full)? Any hints?

With a second level group, you need a key that groups all the Student
element by FirstChoice *and* SecondChoice. You can concatenate the two
values together, using an appropriate separator (e.g. a semi-colon):

<xsl:key name="students-by-first-and-second-choice"
         use="concat(FirstChoice, ';' SecondChoice)" />

You need to access the first Students in each of these subgroups in
a similar way as you do for the first group, so you would have
something like:

<xsl:template match="Course">
        select="Student[count(.|key('students-by-firstchoice', FirstChoice)[1]) = 1]">
      <xsl:sort select="FirstChoice" />
      <xsl:variable name="FirstChoice" select="FirstChoice" />
      <xsl:element name="{$FirstChoice}">
            select="key('students-by-firstchoice', $FirstChoice)
                                   concat($FirstChoice, ';',
                                          SecondChoice))[1]) = 1]">
          <xsl:sort select="SecondChoice" />
          <xsl:element name="{SecondChoice}">
                            concat($FirstChoice, ';', SecondChoice))">
              <xsl:sort select="RegistrationDate" />
              <xsl:copy-of select="." />

(You might want to use moded templates rather than lots of nested
xsl:for-each elements to make things clearer.)

I hope that helps,


Jeni Tennison

 XSL-List info and archive:

Current Thread