0
votes

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

1 Answers

1
votes

Some tweaking is required in the XSLT code. The key needs to be changed to

<xsl:key name="groups" match="MOVIMENTO" use="ARTICOLI/ARTICOLO/SIGLA" />

You can use an identity transform template to copy the elements as is to output and then apply the grouping.

<xsl:template match="@* | node()">
    <xsl:copy>
        <xsl:apply-templates select="@* | node()" />
    </xsl:copy>
</xsl:template>

Match the <MOVIMENTO> templates for the grouped elements and then loop through the key for the distinct elements.

<xsl:template match="MOVIMENTO[generate-id() = generate-id(key('groups', ARTICOLI/ARTICOLO/SIGLA)[1])]">
    <xsl:copy>
        <xsl:for-each select="key('groups', ARTICOLI/ARTICOLO/SIGLA)">
            <xsl:apply-templates select="INFO" />
        </xsl:for-each>
        <xsl:apply-templates select="key('groups', ARTICOLI/ARTICOLO/SIGLA)[1]/ARTICOLI" />
    </xsl:copy>
</xsl:template>

Lastly for any other <MOVIMENTO> nodes, do nothing.

<xsl:template match="MOVIMENTO" />

The output is like below

<RESULT>
    <MOVIMENTO>
        <INFO>
            <MOV_AREA>10</MOV_AREA>
        </INFO>
        <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>