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>

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

4

1 回答 1

4

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>
于 2010-02-19T15:10:51.853 回答