如何在没有键/Muenchian 分组的 XSLT 1.0 中执行 for-each-group [英] How to do a for-each-group in XSLT 1.0 without keys/Muenchian grouping

查看:16
本文介绍了如何在没有键/Muenchian 分组的 XSLT 1.0 中执行 for-each-group的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用 XSLT 并尝试按属性的子字符串对节点进行分组.唯一的问题是我在无法使用 xsl:key 的环境中工作.我想知道对诸如以下内容进行分组的最佳方法:

<代码><结果><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"/></结果>

这样它就可以格式化,其中有一个表格,该表格按 ID 中的最后一位数字对结果进行分组并显示它们的值.例如,它会将 Result:1-1、Test:2-1 和 Result:3-1 分组在第一个表中,并在其下方列出它们的值.

某种预期的结果是:

<代码>|表 1 ||---------||32 ||6362 ||3243 ||表 2 ||---------||3225 ||第352话|2332 ||表 3 ||---------||第372话|第325话|第342话|表 4 ||---||64732 ||第2134章

任何有关使用某些分组方法或某些功能的建议将不胜感激!感谢您的阅读以及您提供的任何帮助!

解决方案

Muenchian 分组的替代方案在相同的

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

I am currently working with XSLT and trying to group nodes by a sub-string of an attribute. The only thing is I'm working in an environment where I can't use an xsl:key. I was wondering the best way to go about grouping something such as:

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

So that it could be formatted where there is a table that groups the Results by the last digit in the ID and displays their values. For example, it would group Result:1-1, Test:2-1, and Result:3-1 in the first table and list their values below it.

Some sort of expected result would be:

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

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

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

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

Any suggestions as to certain methods of grouping to use or certain fuctions would be much appreciated! Thanks for reading and any help you have to offer!

解决方案

The alternative to Muenchian grouping is described in the same article that explains it; it is also labeled there as "very inefficient".

The following stylesheet:

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

when applied to your example input, will return (rendered):

Note that this is different from the result you have posted - but I believe it is correct nevertheless.

这篇关于如何在没有键/Muenchian 分组的 XSLT 1.0 中执行 for-each-group的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆