Xml 使用XSL仅对数值列求和

Xml 使用XSL仅对数值列求和,xml,xslt,sum,Xml,Xslt,Sum,我有这样一些XML: <?xml version="1.0" encoding="UTF-8"?> <?xml-stylesheet type="text/xsl" href="test.xsl"?> <root> <line> <ResourceName>Ren</ResourceName> <Amount>20</Amount> <Ot

我有这样一些XML:

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="test.xsl"?>
<root>
    <line>
        <ResourceName>Ren</ResourceName>
        <Amount>20</Amount>
        <OtherAmount>5</OtherAmount>
        <SomeText>Nopls</SomeText>
    </line>
    <line>
        <ResourceName>Stimpy</ResourceName>
        <Amount>30</Amount>
        <OtherAmount>10</OtherAmount>
        <SomeText>Do_not_sum</SomeText>
    </line>
</root>

任
20
5.
没有
刺激的
30
10
不要求和
但重要的是,行节点下面的“列”的数量可以或多或少地和任何名称(动态变化)

我想生成一个HTML表,其中节点名作为页眉,页脚中任何数字列的总数

到目前为止,我有一个XSLT,如下所示:

<?xml version="1.0" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >

<xsl:template match="root">
<html>
<body>
    <table border="1">
        <thead>
            <xsl:apply-templates select="line[1]" mode="thead"/>
        </thead>
        <tbody>
            <xsl:apply-templates select="line" />
        </tbody>
        <tfoot>
            <tr>
                <td>Totals:</td>
                <td>
                    <xsl:variable name="amount1Lines" select="line/Amount"/>
                    <xsl:value-of select="format-number(sum($amount1Lines), '0.00')" />
                </td>
                <td>
                </td>
                <td>
                </td>
                <td>
                </td>
            </tr>
        </tfoot>
    </table>
</body>
</html>

</xsl:template>

<xsl:template match="line" mode="thead">
    <tr>
        <xsl:apply-templates select="*" mode="thead"/>
    </tr>
</xsl:template>

<xsl:template match="*" mode="thead">
    <th>
        <xsl:value-of select="local-name()" />
    </th>
</xsl:template>

<xsl:template match="line">
    <tr>
        <xsl:apply-templates select="*" />
    </tr>
</xsl:template>

<xsl:template match="line/*">
    <td>
        <xsl:value-of select="." />
    </td>
</xsl:template>

</xsl:stylesheet>
<tfoot><tr>
    <xsl:apply-templates select="line[1]/*" mode="tfoot"/>
</tr></tfoot>

<xsl:template match="*" mode="tfoot">
    <td>
        <xsl:variable name="columnName" select="local-name()"></xsl:variable>
        <xsl:if test="number(//line[1]/*[local-name()=$columnName]) = 
                      number(//line[1]/*[local-name()=$columnName])" >
            <xsl:value-of select="sum(//line/*[local-name() = $columnName])" />
        </xsl:if>
    </td>
</xsl:template>

总数:
但是很明显,页脚部分是为每一列硬编码的


任何人都可以确定一些xsl,这些xsl只对数值列求和,而在页脚中保留其他列为空。例如,在本例中,它将“Amount”和“OtherAmount”相加,但不包括resourcename或sometext列。

通过重复与
tfoot的
tfoot
相同的模式,然后使用确定样本列是否为数字,可以按如下方式合计列:

<?xml version="1.0" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >

<xsl:template match="root">
<html>
<body>
    <table border="1">
        <thead>
            <xsl:apply-templates select="line[1]" mode="thead"/>
        </thead>
        <tbody>
            <xsl:apply-templates select="line" />
        </tbody>
        <tfoot>
            <tr>
                <td>Totals:</td>
                <td>
                    <xsl:variable name="amount1Lines" select="line/Amount"/>
                    <xsl:value-of select="format-number(sum($amount1Lines), '0.00')" />
                </td>
                <td>
                </td>
                <td>
                </td>
                <td>
                </td>
            </tr>
        </tfoot>
    </table>
</body>
</html>

</xsl:template>

<xsl:template match="line" mode="thead">
    <tr>
        <xsl:apply-templates select="*" mode="thead"/>
    </tr>
</xsl:template>

<xsl:template match="*" mode="thead">
    <th>
        <xsl:value-of select="local-name()" />
    </th>
</xsl:template>

<xsl:template match="line">
    <tr>
        <xsl:apply-templates select="*" />
    </tr>
</xsl:template>

<xsl:template match="line/*">
    <td>
        <xsl:value-of select="." />
    </td>
</xsl:template>

</xsl:stylesheet>
<tfoot><tr>
    <xsl:apply-templates select="line[1]/*" mode="tfoot"/>
</tr></tfoot>

<xsl:template match="*" mode="tfoot">
    <td>
        <xsl:variable name="columnName" select="local-name()"></xsl:variable>
        <xsl:if test="number(//line[1]/*[local-name()=$columnName]) = 
                      number(//line[1]/*[local-name()=$columnName])" >
            <xsl:value-of select="sum(//line/*[local-name() = $columnName])" />
        </xsl:if>
    </td>
</xsl:template>
您可以将求和更改为仅限于当前的
数据,方法是沿着
祖先
轴查找“此”根的行:

<xsl:if test="number(ancestor::root/line[1]/*[local-name()=$nodeName]) 
              = number(ancestor::root/line[1]/*[local-name()=$nodeName])" >
    <xsl:value-of select="sum(ancestor::root/line/*[local-name() = $nodeName])" />
</xsl:if>


是否要对所有数值节点求和?e、 例如,从上面的示例代码中,您可能期望值为65?否。我想要一个表,其中每列(例如金额)的页脚都有一个和。所以Amount的页脚有50个,OtherAmount的页脚有15个,这很有效。如果我有另一个带有“”的块,我如何避免从另一个块中拾取行,并且仍然有一个用于页脚和的通用模板?即:根行中只有“root”的子项总数,根2行中只有“root2”的子项总数?已更新-尽管如果您下次提出新问题,您可能会得到更快的响应:-)