我目前正在使用 XSLT 并尝试按属性的子字符串对节点进行分组。唯一的问题是我在无法使用 xsl:key 的环境中工作。我想知道对诸如以下内容进行分组的最佳方法:
<RESULTS>
<RESULT ID="Result:1-1" Value="32" />
<RESULT ID="Result:1-2" Value="3225" />
<RESULT ID="Result:1-3" Value="372" />
<RESULT ID="Result:1-4" Value="64732" />
<RESULT ID="Test:2-1" Value="6362" />
<RESULT ID="Test:2-2" Value="352" />
<RESULT ID="Test:2-3" Value="325" />
<RESULT ID="Result:3-1" Value="3243" />
<RESULT ID="Result:3-2" Value="2332" />
<RESULT ID="Result:3-3" Value="342" />
<RESULT ID="Result:3-4" Value="2134" />
</RESULTS>
这样就可以在有一个表格的情况下对其进行格式化,该表格按 ID 中的最后一位数字对结果进行分组并显示它们的值。例如,它将第一个表中的结果:1-1、测试:2-1 和结果:3-1 分组,并在其下方列出它们的值。
某种预期结果是:
| Table 1 |
|---------|
| 32 |
| 6362 |
| 3243 |
| Table 2 |
|---------|
| 3225 |
| 352 |
| 2332 |
| Table 3 |
|---------|
| 372 |
| 325 |
| 342 |
| Table 4 |
|---------|
| 64732 |
| 2134 |
任何有关某些分组使用方法或某些功能的建议将不胜感激!
感谢您的阅读以及您提供的任何帮助!
Muenchian 分组的替代方案在同一篇中进行了描述article这解释了它;它也被标记为“非常低效”。
以下样式表:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="/RESULTS">
<html>
<xsl:for-each select="RESULT">
<xsl:variable name="key" select="substring(@ID, string-length(@ID))" />
<xsl:if test="not(preceding-sibling::RESULT[substring(@ID, string-length(@ID))=$key])">
<table border="1">
<tr>
<th>
<xsl:text>Table </xsl:text>
<xsl:value-of select="$key"/>
</th>
</tr>
<xsl:for-each select="../RESULT[substring(@ID, string-length(@ID))=$key]">
<tr>
<td>
<xsl:value-of select="@Value"/>
</td>
</tr>
</xsl:for-each>
</table>
</xsl:if>
</xsl:for-each>
</html>
</xsl:template>
</xsl:stylesheet>
当应用于您的示例输入时,将返回(渲染):
请注意,这与您发布的结果不同 - 但我相信它是正确的。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)