XSLT版本2.0-每个组的页脚,包含记录数

时间:2017-11-26 17:24:46

标签: xml xslt-2.0

有人可以按照以下要求引导我朝着正确的方向前进吗?我试图为每个组添加一个页脚。页脚只需要具有相同Student_Status的每个组中的记录数。

下面是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>

以下是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>

以下是当前输出

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

我试图让以下输出为每个Student_Status设置一个页脚,并在每个组中记录一个记录。

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

非常感谢任何帮助!

1 个答案:

答案 0 :(得分:2)

如果您只对状态进行分组,然后计算current-group()项,则应获得您描述的输出:

            <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