我一直在努力学习使用其他帖子来了解muenchian分组,但我正在努力将正确的分组变成表格格式。分组需要分为两个级别,首先是运动赛季'年'然后在每个赛季内显示按“组”分组的匹配项。我成功地将“小组”分组,但他们都在最近一个赛季展示,而不是分成各自的赛季。以下是xml:
的示例<DocumentElement>
<QueryResults>
<Years>2013/2014</Years>
<Group>1</Group>
<TeamNameShort>TeamA</TeamNameShort>
</QueryResults>
<QueryResults>
<Years>2013/2014</Years>
<Group>1</Group>
<TeamNameShort>TeamB</TeamNameShort>
</QueryResults>
<QueryResults>
<Years>2013/2014</Years>
<Group>2</Group>
<TeamNameShort>TeamC</TeamNameShort>
</QueryResults>
<QueryResults>
<Years>2013/2014</Years>
<Group>2</Group>
<TeamNameShort>TeamD</TeamNameShort>
</QueryResults>
<QueryResults>
<Years>2012/2013</Years>
<Group>1</Group>
<TeamNameShort>TeamA</TeamNameShort>
</QueryResults>
<QueryResults>
<Years>2012/2013</Years>
<Group>1</Group>
<TeamNameShort>TeamB</TeamNameShort>
</QueryResults>
<QueryResults>
<Years>2012/2013</Years>
<Group>2</Group>
<TeamNameShort>TeamC</TeamNameShort>
</QueryResults>
<QueryResults>
<Years>2012/2013</Years>
<Group>2</Group>
<TeamNameShort>TeamD</TeamNameShort>
</QueryResults>
</DocumentElement>
并且xsl目前看起来像这样
<xsl:stylesheet
version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:cfg="http://tempuri.org/config"
exclude-result-prefixes="cfg"
>
<xsl:output method="html" indent="yes"/>
<!-- index by season -->
<xsl:key
name="Season"
match="QueryResults"
use="Years"
/>
<!-- index by "Pool" -->
<xsl:key
name="Pool"
match="QueryResults"
use="Group"
/>
<xsl:template match="DocumentElement">
<xsl:copy>
<!-- group by season -->
<xsl:apply-templates mode="season" select="
QueryResults[
generate-id()
=
generate-id(key('Season', Years)[1])
]
">
<xsl:sort select="Years" order="descending" />
</xsl:apply-templates>
</xsl:copy>
</xsl:template>
<!-- Season -->
<xsl:template match="QueryResults" mode="season">
<xsl:variable name="y" select="Years" />
<table>
<tbody>
<tr>
<td colspan="3">Season <xsl:value-of select="$y"/></td>
</tr>
<tr>
<th>Pos</th>
<th>Group/Year</th>
<th>Team</th>
</tr>
<!-- group Season by Pool -->
<xsl:apply-templates mode="pool" select="
key('Season', $y)[
generate-id()
=
generate-id(key('Pool',Group)[1])
]
">
</xsl:apply-templates>
</tbody>
</table>
</xsl:template>
<!-- Pool -->
<xsl:template match="QueryResults" mode="pool">
<xsl:variable name="g" select="Group" />
<tr>
<td colspan="3">Pool <xsl:value-of select="Group"/></td>
</tr>
<xsl:for-each select="key('Pool',$g)">
<tr>
<td><xsl:value-of select="Group"/></td>
<td><xsl:value-of select="Years"/></td>
<td><xsl:value-of select="TeamNameShort"/></td>
</tr>
</xsl:for-each>
</xsl:template>
<xsl:template match="QueryResults">
<xsl:copy-of select="." />
</xsl:template>
</xsl:stylesheet>
答案 0 :(得分:3)
问题在于,您不应仅仅通过组元素进行分组,而应该通过组对给定的年进行分组。实际上,您需要使用连接键
<xsl:key name="Pool" match="QueryResults" use="concat(Years, '|', Group)"/>
然后,只需在您引用密钥的任何位置使用此连接值即可。例如
<xsl:apply-templates mode="pool" select="
key('Season', $y)
[
generate-id() = generate-id(key('Pool',concat(Years, '|', Group))[1])
]"/>
请注意,此处的|
字符可以是任何内容,只要它不会出现在您连接的两个值中的任何一个中。
试试这个XSLT
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:cfg="http://tempuri.org/config" exclude-result-prefixes="cfg">
<xsl:output method="html" indent="yes"/><!-- index by season -->
<xsl:key name="Season" match="QueryResults" use="Years"/><!-- index by "Pool" -->
<xsl:key name="Pool" match="QueryResults" use="concat(Years, '|', Group)"/>
<xsl:template match="DocumentElement">
<xsl:copy><!-- group by season -->
<xsl:apply-templates mode="season" select="QueryResults[generate-id() = generate-id(key('Season', Years)[1])]">
<xsl:sort select="Years" order="descending"/>
</xsl:apply-templates>
</xsl:copy>
</xsl:template><!-- Season -->
<xsl:template match="QueryResults" mode="season">
<xsl:variable name="y" select="Years"/>
<table>
<tbody>
<tr>
<td colspan="3">Season
<xsl:value-of select="$y"/></td>
</tr>
<tr>
<th>Pos</th>
<th>Group/Year</th>
<th>Team</th>
</tr><!-- group Season by Pool -->
<xsl:apply-templates mode="pool" select="key('Season', $y)[generate-id() = generate-id(key('Pool',concat(Years, '|', Group))[1])]"/>
</tbody>
</table>
</xsl:template>
<!-- Pool -->
<xsl:template match="QueryResults" mode="pool">
<xsl:variable name="g" select="concat(Years, '|', Group)"/>
<tr>
<td colspan="3">Pool
<xsl:value-of select="Group"/></td>
</tr>
<xsl:for-each select="key('Pool',$g)">
<tr>
<td>
<xsl:value-of select="Group"/>
</td>
<td>
<xsl:value-of select="Years"/>
</td>
<td>
<xsl:value-of select="TeamNameShort"/>
</td>
</tr>
</xsl:for-each>
</xsl:template>
<xsl:template match="QueryResults">
<xsl:copy-of select="."/>
</xsl:template>
</xsl:stylesheet>