I have an XML file that contains a list of nodes MOVIMENTO and I need to group these nodes by value of sub-tag SIGLA with XSLT 1.0.
<RESULT>
<MOVIMENTO> <!-- MOVIMENTO: nodes to cicle into -->
<INFO>
<MOV_AREA>10</MOV_AREA>
</INFO>
<ARTICOLI>
<ARTICOLO>
<SIGLA>80069</SIGLA> <!-- SIGLA: nodes to group by distinct value -->
<UM>NR</UM>
<QTA_CONS>1,00</QTA_CONS>
</ARTICOLO>
</ARTICOLI>
</MOVIMENTO>
<MOVIMENTO>
<INFO>
<MOV_AREA>13</MOV_AREA>
</INFO>
<ARTICOLI>
<ARTICOLO>
<SIGLA>80069</SIGLA>
<UM>NR</UM>
<QTA_CONS>1,00</QTA_CONS>
</ARTICOLO>
</ARTICOLI>
</MOVIMENTO>
<MOVIMENTO>
<INFO>
<MOV_AREA>14</MOV_AREA>
</INFO>
<ARTICOLI>
<ARTICOLO>
<SIGLA>77586</SIGLA>
<UM>NR</UM>
<QTA_CONS>1,00</QTA_CONS>
</ARTICOLO>
</ARTICOLI>
</MOVIMENTO>
</RESULT>
I tried a lot of solution based on examples find on web but nothing works correctly for me. Someone can help me? Below the code that I'm using:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl">
<xsl:key name="groups" match="ARTICOLO" use="SIGLA" />
<xsl:template match="RESULT">
<xsl:apply-templates select="MOVIMENTO/ARTICOLI/ARTICOLO[generate-id() = generate-id(key('groups', SIGLA)[1])]"/>
</xsl:template>
<xsl:template match="ARTICOLO">
<xsl:for-each select="key('groups', SIGLA)">
SIGLA: <xsl:value-of select="normalize-space(SIGLA)"/>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>