You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用XSLT 1.0按ID分组并合并condition属性值?

How to Group by ID and Merge condition Values in XSLT 1.0 (For Large Payloads)

Ah, I see the issue right away: you're using <xsl:for-each-group>, which is an XSLT 2.0 feature, but you're working in XSLT 1.0. That's why your current stylesheet is throwing those messy errors. For XSLT 1.0—especially when dealing with large payloads full of duplicate records—the go-to method for grouping is Muenchian Grouping. It's efficient, well-established, and perfect for your use case. Let's fix this step by step.

What is Muenchian Grouping?

Muenchian Grouping works by defining an index (using <xsl:key>) for your nodes based on the grouping value (in your case, the id element). This creates a fast lookup table, so you don't have to repeatedly scan the entire document when processing groups—critical for keeping performance snappy with big datasets.

Full Working XSLT 1.0 Stylesheet

Here's the adjusted stylesheet that groups by id, merges condition values, and outputs exactly the structure you want:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <!-- Define a key to index all <result> nodes by their <id> value -->
    <xsl:key name="result-by-id" match="result" use="id"/>

    <xsl:template match="/Items">
        <xsl:copy>
            <!-- Loop through only the first node of each unique <id> group -->
            <xsl:for-each select="result[generate-id() = generate-id(key('result-by-id', id)[1])]">
                <result>
                    <!-- Copy <id> and <name> (they're identical across the entire group) -->
                    <xsl:copy-of select="id | name"/>
                    <!-- Merge all <condition> values from the group, separated by commas -->
                    <condition>
                        <xsl:for-each select="key('result-by-id', id)">
                            <!-- Add a comma separator only if it's not the first item -->
                            <xsl:if test="position() > 1">, </xsl:if>
                            <xsl:value-of select="condition"/>
                        </xsl:for-each>
                    </condition>
                </result>
            </xsl:for-each>
        </xsl:copy>
    </xsl:template>

    <!-- Identity template (copies all other nodes/attributes if needed) -->
    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>
</xsl:stylesheet>

Let's Break Down the Code

  1. Key Definition: The <xsl:key> creates an index where every <result> node is linked to its <id> value. This is the backbone of Muenchian Grouping—it lets the processor quickly fetch all nodes in a group without scanning the whole document.
  2. Select Unique Groups: The <xsl:for-each> line picks only the first node of each unique <id> group. The generate-id() check ensures we process each group exactly once, no duplicates.
  3. Copy Shared Data: Since all nodes in a group have the same <id> and <name>, we just copy those elements once from the first node in the group.
  4. Merge condition Values: We loop through all nodes in the current group, appending each <condition> value. The <xsl:if> adds a comma separator only after the first item, so we don't get a leading comma.

Expected Output

When you run this stylesheet against your input XML, you'll get the grouped result you requested:

<Items>
    <result>
        <id>11</id>
        <name>ABC</name>
        <condition>new, used</condition>
    </result>
    <result>
        <id>22</id>
        <name>XYZ</name>
        <condition>new</condition>
    </result>
    <result>
        <id>33</id>
        <name>PQR</name>
        <condition>used</condition>
    </result>
</Items>

Why This Works for Large Payloads

Muenchian Grouping is optimized for performance. The key index lets the processor retrieve all nodes in a group in constant time, unlike naive grouping methods (like using preceding-sibling checks) which can get slow with thousands of records. This makes it ideal for your large payload with lots of duplicates.

内容的提问来源于stack exchange,提问作者Rooster

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.07 17:57:47