この入力ファイルが必要です
<items>
<item>
<id>1</id>
<group_id>1</group_id>
<color>blue</color>
<city>Chicago</city>
</item>
<item>
<id>2</id>
<group_id>1</group_id>
<color>red</color>
<city>Chicago</city>
</item>
<item>
<id>3</id>
<group_id>1</group_id>
<color>yellow</color>
<city>Detroit</city>
</item>
<item>
<id>4</id>
<group_id>2</group_id>
<color>cyan</color>
<city>Washington</city>
</item>
<item>
<id>5</id>
<group_id>2</group_id>
<color>gray</color>
<city>Colorado</city>
</item>
</items>
この xslt は、必要な出力を作成できます
<xsl:template match="/">
<xsl:apply-templates select="items" />
</xsl:template>
<xsl:template match="items">
<xsl:for-each-group select="item" group-by="group_id">
<xsl:element name="item">
<xsl:element name="group_id">
<xsl:value-of select="current-grouping-key()" />
</xsl:element>
<xsl:for-each select="current-group()">
<xsl:element name="id">
<xsl:value-of select="id" />
</xsl:element>
</xsl:for-each>
</xsl:element>
</xsl:for-each-group>
</xsl:template>
出力
<item>
<group_id>1</group_id>
<id>1</id>
<id>2</id>
<id>3</id>
</item>
<item>
<group_id>2</group_id>
<id>4</id>
<id>5</id>
</item>
編集:すべてのアイテムを含む1つの入力ファイルではなく、単一のアイテムを含む複数のファイルがある場合、次のようにすることができます:
<xsl:variable name="files" select="collection('file:///path/to/files/item*.xml')"/>
<xsl:template match="/">
<xsl:element name="items">
<xsl:for-each-group select="$files/item" group-by="group_id">
<xsl:element name="item">
<xsl:element name="group_id">
<xsl:value-of select="current-grouping-key()" />
</xsl:element>
<xsl:for-each select="current-group()">
<xsl:element name="id">
<xsl:value-of select="id" />
</xsl:element>
</xsl:for-each>
</xsl:element>
</xsl:for-each-group>
</xsl:element>
</xsl:template>