我有這樣滯留在XSLT 2.0分組
<items>
<item>
<id>5</id>
<name>Harry</name>
<age>18</age>
<color>blue</color>
</item>
<item>
<id>5</id>
<name>Harry</name>
<age>18</age>
<fav_food>pizza</fav_food>
</item>
<item>
<id>5</id>
<name>Harry</name>
<age>18</age>
<is_single>true</is_single>
</item>
</items>
我想組最多的元素,使文件看起來像這樣
<items>
<item>
<id>5</id>
<name>Harry</name>
<age>18</age>
<color>blue</color>
<fav_food>pizza</fav_food>
<is_single>true</is_single>
</item>
</items>
編輯輸入文件 - 一個製造按照此鏈接進行XSLT轉換(XSLT Consolidating data when ID is the same),但這只是不打印任何東西。
這裏是我的變換(使用XSLT 2.0) -
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes" omit-xml-declaration="yes" />
<xsl:strip-space elements="*" />
<xsl:template match="items">
<xsl:apply-templates select="@*|node()"/>
</xsl:template>
<xsl:template match="item">
<xsl:apply-templates select="@*" />
<xsl:for-each-group select="item" group-by="id">
<item>
<id><xsl:value-of select="current-grouping-key()"/></id>
<xsl:apply-templates select="current-group()" />
</item>
</xsl:for-each-group>
</xsl:template>
<xsl:template match="@*|node()[not(self::*)]">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="*">
<xsl:element name="{local-name()}">
<xsl:apply-templates select="@*|node()"/>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
如果使用XSLT 2.0,您可以用'的for-each-group' - 退房[這個問題]的答案(http://stackoverflow.com/q/11247326/1395993),這是一個類似的問題。 –
XSLT 1.0和2.0中的分組非常不同,你真的需要說出你正在使用的是哪一個。 –
對不起,我忘記說,我使用XSLT 2.0,並更新了我現在有XSLT轉換我的問題。 – Narabhut