【问题标题】:XSLT version 2.0- Footer for each group with count of recordsXSLT 2.0 版 - 每个组的页脚以及记录数
【发布时间】:2017-11-26 17:24:45
【问题描述】:

有人可以按照以下要求引导我朝着正确的方向前进吗?我正在尝试为每个组添加页脚。页脚只需要计算每个组中具有相同 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

非常感谢任何帮助!

【问题讨论】:

    标签: xml xslt-2.0


    【解决方案1】:

    如果您只对状态进行分组,然后计算 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

    【讨论】:

    • 这太完美了!谢谢!!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多