[英]XSLT Dynamic Columns in XML
我在下面有一個示例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>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.