0
votes

I have been struggling with this problem for hours....help

First I must use XSLT 1.0 and I cannot use xsl:key. I have to sort and group the following xml data

<?xml version="1.0" encoding="UTF-8" ?>
 <?xml-stylesheet href="class2.xsl" type="text/xsl" ?>
 <université>
     <étudiant>
         <nom>Réjean Tremblay</nom>
         <cours sigle="INF8430" note="89" />
         <cours sigle="INF1030" note="69" />
         <cours sigle="INF1230" note="75" />
    </étudiant>
    <étudiant>
         <nom>Martin Lambert</nom>
         <cours sigle="INF8430" note="75" />
         <cours sigle="INF1030" note="72" />
         <cours sigle="INF1230" note="73" />
    </étudiant>
     <étudiant>
         <nom>Luc Alain</nom>
         <cours sigle="INF9430" note="39" />
         <cours sigle="INF1030" note="89" />
         <cours sigle="INF1230" note="79" />
    </étudiant>
     <étudiant>
         <nom>Olive Saint-Amant</nom>
         <cours sigle="INF8430" note="91" />
         <cours sigle="INF1230" note="99" />
    </étudiant>
 </université>

The expected result should be

enter image description here

1
"I cannot use xsl:key." Why is that?michael.hor257k
I cannot use the key element to group the courses w3schools.com/xsl/el_key.aspuser1082748
Are you using XT processor? Otherwise this limitation is ridiculous.Rudolf Yurgenson
@user1082748, please explain why you cannot use xsl:key.Martin Honnen
You might feel obliged to work for a client who tells you to write code with one hand tied behind your back, but most people answering questions here are likely to tell your client to get lost.Michael Kay

1 Answers

2
votes

Without xsl:key (not optimal, of course)

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output indent="yes"/>

    <xsl:template match="/*">
        <table>
            <tr>
                <td>Sigle</td>
                <td>Nombre d'etuidants</td>
                <td>Moyenne du cours</td>
            </tr>
            <xsl:apply-templates select="etudiant/cours"/>
        </table>
    </xsl:template>

    <xsl:template match="cours[not(@sigle = preceding::cours/@sigle)]">
        <tr>
            <td>
                <xsl:value-of select="@sigle"/>
            </td>
            <xsl:variable name="cnt" select="count(//cours[@sigle = current()/@sigle])"/>
            <td>
                <xsl:value-of select="$cnt"/>
            </td>
            <td>
                <xsl:value-of select="format-number(sum(//cours[@sigle = current()/@sigle]/@note) div $cnt, '#.0')"/>
            </td>
        </tr>
    </xsl:template>
</xsl:stylesheet>