【问题标题】:Sums and Category Grouping with XSLT使用 XSLT 进行总和和类别分组
【发布时间】:2010-02-19 14:50:12
【问题描述】:

使用 XSLT,如何更改以下内容:

<root>
  <element id="1" State="Texas" County="Dallas" Population="2412827" />
  <element id="2" State="Texas" County="Harris" Population="3984349" />
  <element id="3" state="Georgia" County="Fulton" Population="1014932" />
  <element id="4" state="Georgia" County="Richmond" Population="212775" />
</root>

进入:

<body>
  <h2>Texas</h2>
  <table>
    <tr><td>Dallas</td><td>2412827</td></tr>
    <tr><td>Harris</td><td>3984349</td></tr>
    <tr><td>Total</td><td>6397176</td></tr>
  <h2>Georgia</h2>
  <table>
    <tr><td>Fulton</td><td>1014932</td></tr>
    <tr><td>Richmond</td><td>212775</td></tr>
    <tr><td>Total</td><td>1227707</td></tr>
  </table>
</body>

没有明确编码每个州的名称,因为如果波多黎各成为一个州,我会被搞砸的。

【问题讨论】:

    标签: xml xslt xpath muenchian-grouping


    【解决方案1】:

    XSLT 1.0
    定义一个键“状态”,我们可以从中轻松选择给定状态名称的所有状态。而不是应用 Muenchian 分组来查找输入中的唯一状态。

    然后它就变得简单了。 “元素”模板将为每个状态名称应用一次,并使用 key() 获取该状态的所有条目。

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    
        <xsl:output method="xml" indent="yes" />
    
        <xsl:key name="state" match="element" use="@State" />
    
        <xsl:template match="root">
            <body>
                <xsl:apply-templates select="element[generate-id(.)=generate-id(key('state',@State)[1])]"/>
            </body>
        </xsl:template>
    
        <xsl:template match="element">
            <h2><xsl:value-of select="@State" /></h2>
            <table>
                <xsl:for-each select="key('state',@State)">
                    <tr>
                        <td>
                            <xsl:value-of select="@County" />
                        </td>
                        <td>
                            <xsl:value-of select="@Population" />
                        </td>
                    </tr>
                </xsl:for-each>
    
                <tr>
                    <td>
                        <xsl:text>Total</xsl:text>
                    </td>
                    <td>
                        <xsl:value-of select="sum(key('state',@State)/@Population)"/>
                    </td>
                </tr>
    
            </table>
        </xsl:template>
    
    </xsl:stylesheet>
    

    XSLT 2.0

    <xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    
        <xsl:output method="xml" indent="yes" />
    
        <xsl:template match="root">
            <body>
                <xsl:for-each-group select="element" group-by="@State">
                    <h2><xsl:value-of select="@State" /></h2>
                    <table>
                        <xsl:for-each select="current-group()">
                            <tr>
                                <td>
                                    <xsl:value-of select="@County" />
                                </td>
                                <td>
                                    <xsl:value-of select="@Population" />
                                </td>
                            </tr>
                        </xsl:for-each>
                        <tr>
                            <td>
                                <xsl:text>Total</xsl:text>
                            </td>
                            <td>
                                <xsl:value-of select="format-number(sum(current-group()/@Population), '#########')"/>
                            </td>
                        </tr>
                    </table>
                </xsl:for-each-group>
            </body>
        </xsl:template>
    
    </xsl:stylesheet>
    

    【讨论】:

    • +1 领先我几秒钟。 :) 我怀疑这是一个家庭作业问题,至少感觉是这样。
    • 不是家庭作业。我只是不太熟悉 XSLT。还有其他人告诉我这是不可能的,在我看来并非如此。我知道 SO 能够帮助我。
    猜你喜欢
    • 1970-01-01
    • 2012-09-04
    • 2013-02-13
    • 1970-01-01
    • 2017-10-26
    • 1970-01-01
    • 2017-05-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多