Re: [xsl] Grouping with second choice

Subject: Re: [xsl] Grouping with second choice
From: Thorsten Liebig <liebig@xxxxxxxxxxxxxxxxxxxxx>
Date: Fri, 17 Aug 2001 12:02:30 +0200
Jeni Tennison wrote:


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"
         match="Student"
         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:


Thanks for the hint. But what I intend to do, is to divide the students
according to there choices *and* additional constraints like maximum group
size. So, whenever a first choice cannot be fulfilled due to group size
reasons the second choice should be taken into account. My problem is
(I think), that all those solutions do not apply in which the group tags
are created on the fly (like in the solution below) because it may happen
that a student has to be divided in a group which hasn't been created, yet.
Concrete, having a maximal group size of 3, the students of my example file
should be classified as follows:

<group1>
   <student> ... A ... </student>
   <student> ... B ... </student>
   <student> ... C ... </student>
</group1>
<group2>
   <student> ... D ... </student>
   <student> ... E ... </student>
   <student> ... F ... </student>
</group2>

- Thorsten


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

(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

---
Jeni Tennison
http://www.jenitennison.com/


XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list


--
_____________________________________________________________
Thorsten Liebig              Abteilung Künstliche Intelligenz
Tel.: +49 731 502 4207                Fakultät für Informatik
Fax.: +49 731 502 4119                        Universität Ulm


XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list



Current Thread