Search code examples
xmlxslt-2.0

XSLT version 2.0- Footer for each group with count of records


can someone lead me in the right direction with the below requirement? I'm trying to add a footer for each group. The footer just needs to have a count of records in each group that has the same Student_Status.

Below is the XML

<Record>    
<Student>
    <Student_ID>2345</Student_ID>
    <Student_Data>
        <Student_Hours>
            <Student_Status>Full</Student_Status>
        </Student_Hours>
    </Student_Data>
</Student>
<Student>
    <Student_ID>7891</Student_ID>
    <Student_Data>
        <Student_Hours>
            <Student_Status>Half</Student_Status>
        </Student_Hours>
    </Student_Data>
</Student>
<Student>
    <Student_ID>4568</Student_ID>
    <Student_Data>
        <Student_Hours>
            <Student_Status>Full</Student_Status>
        </Student_Hours>
    </Student_Data>
</Student>
<Student>
    <Student_ID>9897</Student_ID>
    <Student_Data>
        <Student_Hours>
            <Student_Status>Full</Student_Status>
        </Student_Hours>
    </Student_Data>
</Student>

Below is the XSLT

        <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" version="2.0">

            <xsl:output method='text' indent="no"/>
            <xsl:variable name="separator"><xsl:text>,</xsl:text></xsl:variable>
            <xsl:variable name="newLine"><xsl:text>&#10;</xsl:text></xsl:variable> 

                <xsl:template match="Record">
                    <xsl:for-each-group select="Student" group-by="concat(Student_ID, Student_Status)">  
                        <xsl:for-each select="current-group()">
                            <Student_ID><xsl:value-of select="Student_ID"/></Student_ID><xsl:value-of select="$separator"/>
                            <Student_Status><xsl:value-of select="Student_Data/Student_Hours/Student_Status"/></Student_Status>
                            <xsl:value-of select="$newLine"/>      
                        </xsl:for-each>
                    </xsl:for-each-group>
                    <!--Footer-->
                    <Count>Count </Count>  <xsl:value-of select="count(Student/Student_ID)"/>
                    <!--Footer-->
                </xsl:template>
        </xsl:stylesheet>

Below is the current output

  2345,Full
  7891,Half
  4568,Full
  9897,Full
  Count 4

I'm trying to get the below output to have a footer for each Student_Status, and have a count of records in each group.

  2345,Full
  4568,Full
  9897,Full
  Count 3
  7891,Half
  Count 1

Any help is much appreciated!


Solution

  • If you only group on the status and then count the current-group() items you should get the output you describe:

                <xsl:template match="Record">
                    <xsl:for-each-group select="Student" group-by="Student_Data/Student_Hours/Student_Status">  
                        <xsl:for-each select="current-group()">
                            <Student_ID><xsl:value-of select="Student_ID"/></Student_ID><xsl:value-of select="$separator"/>
                            <Student_Status><xsl:value-of select="current-grouping-key()"/></Student_Status>
                            <xsl:value-of select="$newLine"/>      
                        </xsl:for-each>
                        <xsl:value-of select="'Count:', count(current-group()), $newLine"/>
                    </xsl:for-each-group>
                </xsl:template>
    

    http://xsltransform.net/pNmBy2d/1