XSLT使用for-each选择唯一值



我想提取我尝试过的元素的唯一值,但它不工作主要问题是我不知道如何使用XSLT 1.0或2.0获得不同的值这里是源XML的例子,我使用

<Items>
<Item>
<Itemno>112</Itemno>
<itemname>abc</itemname>
<qun>5</qun>
<loc>US</loc>
</Item>
</Items>
<Items>
<Item>
<Itemno>112</Itemno>
<itemname>abc</itemname>
<qun>6</qun>
<loc>UK</loc>
</Item>
</Items>
<Items>
<Item>
<Itemno>112</Itemno>
<itemname>abc</itemname>
<qun>11</qun>
<loc>GER</loc>
</Item>
</Items>
<Items>
<Item>
<Itemno>114</Itemno>
<itemname>lkj</itemname>
<qun>1</qun>
<loc>IND</loc>
</Item>
</Items>

需要输出

<Order>
<Item>
<Itemno>112</Itemno>
<itemname>abc</itemname>
<Desc>
<qun>5</qun>
<loc>US</loc>
</Desc>
<Desc>
<qun>6</qun>
<loc>UK</loc>
</Desc>
<Desc>
<qun>11</qun>
<loc>GER</loc>
</Desc>
</Item>
<Item>
<Itemno>114</Itemno>
<itemname>lkj</itemname>
<Desc>
<qun>1</qun>
<loc>IND</loc>
</Desc>
</Item>
</Order>

在xslt 2.0中,您可以通过Itemno使用for-each-group进行分组。例如,您可以使用以下代码

<xsl:template match="Items">
<Order>
<xsl:for-each-group select="//Item" group-by="Itemno">
<Item>
<xsl:copy-of select="Itemno"/>
<xsl:copy-of select="itemname"/>
<xsl:for-each select="current-group()">
<Desc>
<xsl:copy-of select="qun"/>
<xsl:copy-of select="loc"/>
</Desc>
</xsl:for-each>
</Item>
</xsl:for-each-group>
</Order>
</xsl:template>

它的结果是您需要的xml

最新更新