Html XML中的XSLT动态列

Html XML中的XSLT动态列,html,xml,xslt,key,grouping,Html,Xml,Xslt,Key,Grouping,我有一个示例xml,如下所示 <?xml version="1.0" encoding="UTF-8"?> <Root> <Row> <Group>Group A</Group> <Date>2013-09-02</Date> <Value>200</Value> </Row> <Row> <Group>Group A</Group>

我有一个示例xml,如下所示

    <?xml version="1.0" encoding="UTF-8"?>
<Root>
<Row>
<Group>Group A</Group>
<Date>2013-09-02</Date>
<Value>200</Value>
</Row>
<Row>
<Group>Group A</Group>
<Date>2013-09-02</Date>
<Value>500</Value>
</Row>
<Row>
<Group>Group A</Group>
<Date>2013-10-02</Date>
<Value>400</Value>
</Row>
<Row>
<Group>Group B</Group>
<Date>2013-09-02</Date>
<Value>250</Value>
</Row>
</Root>

A组
2013-09-02
200
A组
2013-09-02
500
A组
2013-10-02
400
B组
2013-09-02
250
我有一个xslt(版本1.0),它将用于总结数据,并带有动态列。我让代码创建HTML表,结果显示正确的列数和行数,但我无法让表主体中的单元格对上面XML的“Value”节点中的值求和

XSLT:

<?xml version="1.0" encoding="UTF-8"?> 
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:lookup="http://any.com/lookup" exclude-result-prefixes="lookup">
    <xsl:output method="html" indent="yes" encoding="UTF-8" />
    <xsl:decimal-format name="NN" NaN="-" />
    <lookup:Dates>
        <Date value="2013-09-02" />
        <Date value="2013-10-02" />
        <Date value="2013-11-04" />
        <Date value="2013-12-02" />
        <Date value="2014-01-02" />
        <Date value="2014-02-03" />
        <Date value="2014-03-03" />
    </lookup:Dates>
    <xsl:key name="Dates" match="/xsl:stylesheet/lookup:Dates" use="Date" />
    <xsl:key name="Group" match="Row" use="Group" />
    <xsl:template match="/">
        <html>
            <head>
<style type="text/css">
table {border:1px solid #000;border-collapse:collapse}
td {border:1px solid #000;border-collapse:collapse}
</style>
            </head>
            <body>
                <table cellpadding="4" cellspacing="0">
                    <tr>
                        <td>Group</td>
                        <xsl:for-each select="document('')/*/lookup:Dates/Date">
                            <td>
                                <xsl:value-of select="@value" />
                            </td>
                        </xsl:for-each>
                    </tr>
                    <xsl:for-each select="/Root/Row[count(. | key('Group', Group)[1])=1]">
                        <xsl:sort select="Group" data-type="text" order="ascending" />
                        <xsl:variable name="curGroup" select="key('Group', Group)" />
                        <tr>
                            <td>
                                <xsl:value-of select="$curGroup/Group" />
                            </td>
                            <xsl:for-each select="document('')/*/lookup:Dates/Date">
                                <td>
                                    <xsl:value-of select="sum($curGroup[/Root/Row/Date=current()/Dates/Date]/Value)" />
                                </td>
                            </xsl:for-each>
                        </tr>
                    </xsl:for-each>
                </table>
            </body>
        </html>
    </xsl:template>
</xsl:stylesheet>

表{边框:1px实心#000;边框折叠:折叠}
td{border:1px solid#000;border collapse:collapse}
团体
生成的HTML为:

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<style type="text/css">
table {border:1px solid #000;border-collapse:collapse}
td {border:1px solid #000;border-collapse:collapse}
</style>
</head>
<body><table cellpadding="4" cellspacing="0">
<tr>
<td>Group</td>
<td>2013-09-02</td>
<td>2013-10-02</td>
<td>2013-11-04</td>
<td>2013-12-02</td>
<td>2014-01-02</td>
<td>2014-02-03</td>
<td>2014-03-03</td>
</tr>
<tr>
<td>Group A</td>
<td>0</td>
<td>0</td>
<td>0</td>
<td>0</td>
<td>0</td>
<td>0</td>
<td>0</td>
</tr>
<tr>
<td>Group B</td>
<td>0</td>
<td>0</td>
<td>0</td>
<td>0</td>
<td>0</td>
<td>0</td>
<td>0</td>
</tr>
</table></body>
</html>

表{边框:1px实心#000;边框折叠:折叠}
td{border:1px solid#000;border collapse:collapse}
团体
2013-09-02
2013-10-02
2013-11-04
2013-12-02
2014-01-02
2014-02-03
2014-03-03
A组
0
0
0
0
0
0
0
B组
0
0
0
0
0
0
0
你知道我哪里出错了吗?我假设它在xpath中

<xsl:value-of select="sum($curGroup[/Root/Row/Date=current()/Dates/Date]/Value)" />

非常感谢


Graham

如果要添加总计行,那么这样做是行不通的

<xsl:for-each select="document('')/*/lookup:Dates/Date"> 
   <td class="right"> 
      <xsl:value-of select="sum(Root/Row[Date=current()/@value]/Value)" /> 
   </td> 
</xsl:for-each> 

这是因为在xsl:for each循环中,您的上下文是Date元素,因此执行根/行的xpath表达式将与之相关

解决方案只是定义一个包含所有元素的变量(在循环之前),然后引用该变量。试试这个:

<xsl:variable name="allGroups" select="//Row" />
<xsl:for-each select="//Dates/Date">
    <td>
         <xsl:value-of select="sum($allGroups[Date=current()/@value]/Value)" />
    </td>
</xsl:for-each>


我不确定我是否理解您的问题。您说的是“动态列”,但是您的列名被硬编码到样式表中。动态解决方案将是纯数据驱动的。已解决!sum语句中的xpath不正确,更正为:现在为组和总计提供了正确的摘要在转换xml之前加载xsl时,lookup:dates节点中的日期将动态更新现在正在处理最终总计行:
code
totals
code
仅显示每列的最后一行总计为零。太棒了!非常感谢你。现在效果很好