如何在没有键/ Muenchian分组的情况下在XSLT 1.0中执行for-each-group

时间:2016-06-28 21:25:53

标签: xml xslt xslt-1.0

我目前正在使用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中的最后一位分组并显示它们的值。例如,它会将Result:1-1,Test:2-1和Result:3-1分组到第一个表中,并在其下面列出它们的值。

某种预期结果将是:

| Table 1 |
|---------|
| 32      |
| 6362    |
| 3243    |

| Table 2 |
|---------|
| 3225    |
| 352     |
| 2332    |

| Table 3 |
|---------|
| 372     |
| 325     |
| 342     |

| Table 4 |
|---------|
| 64732   |
| 2134    |

任何关于某些分组使用方法或某些功能的建议都将非常感谢! 感谢阅读和您提供的任何帮助!

1 个答案:

答案 0 :(得分:2)

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>

应用于您的示例输入时,将返回(呈现):

enter image description here

请注意,这与您发布的结果不同 - 但我相信它是正确的。