使用 XSLT 进行求和和类别分组

发布于 2024-08-22 08:39:27 字数 1094 浏览 5 评论 0原文

使用 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>

而不显式编码每个州的名称,因为如果波多黎各成为一个州,我就会完蛋。

With XSLT, how can I change the following:

<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>

into:

<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>

without explicitly coding each state's name, because I would be screwed if Puerto Rico ever became a state.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

箹锭⒈辈孓 2024-08-29 08:39:27

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>

XSLT 1.0
Define a key "state", from which we can easily select all states given a state name. Than apply Muenchian grouping to find the unique states in the input.

Then it gets simple. The "element" template will be applied once per state name, and uses the key() to fetch all entries for that state.

<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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文