html - XML 中的 XSLT 动态列

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

我有一个 xslt(1.0 版),可用于汇总数据,其中包含动态列。我用代码创建了 HTML 表格,结果显示了正确的列数和正确的行数,我无法获取表格主体中的单元格以对上面 XML 的“值”节点中的值求和.

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>

生成的 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>

有什么地方出错了吗?我假设它在行中的 xpath 中

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

非常感谢

格雷厄姆

最佳答案

如果你想添加一个总计行,那么这样做是行不通的......

<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 元素,所以执行 Root/Row 的 xpath 表达式> 将与此相关。

解决方案是简单地定义一个变量(在循环之前)保存所有 Row 元素,然后引用它。试试这个:

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

关于html - XML 中的 XSLT 动态列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22175401/

相关文章:

javascript - 如何从 Angular 中的另一个组件滚动到一个组件?

html - 从上到下 float div

jquery - 根据具有绝对位置的子 div 的高度更改父 div 的高度

xml - 无法为签名 ‘saveXML’ 字符找到函数 ‘"的继承方法”’

windows - 是否有任何 XSLT 处理命令行工具?

jquery - 以百分比分配宽度,但想以像素为单位获取它

xml - XML 中的组元素

wpf - WPF 应用程序的最佳本地数据库解决方案是什么?

xml - 使用 XSLT 将分隔符转换为包装器

c# - 使用 xslt 从 rss 文件创建 html 列表