0

I want to extract unique values of element i tried for-each with following-sibling but it is not working Main problem is that I don't know how to get distinct values using XSLT 1.0 or 2.0 Here is Source XML example i am using

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

Required Output

<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>
Filburt
  • 17,626
  • 12
  • 64
  • 115
unknown09
  • 1
  • 3
  • Does this answer your question? [XSLT muenchian grouping by value in child node](https://stackoverflow.com/questions/64258044/xslt-muenchian-grouping-by-value-in-child-node) or other answers using [muenchian grouping](https://en.wikipedia.org/wiki/XSLT/Muenchian_grouping) There many more existing answers deliang with [tag:muenchian-grouping]. – Filburt Apr 28 '22 at 12:15

1 Answers1

2

In xslt 2.0 you can for grouping by Itemno use for-each-group. As example you can use this code

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

The result of it is xml that you need

iroli
  • 458
  • 2
  • 9