user2959477
user2959477

Reputation: 31

xslt 1.0 - how to have nested grouping with element of same level

I have an xml of the following form

<operation>
    <update>
        <wogroup>id1</wogroup>
        <woid>SL0001</woid>
        <status>NEW</status>
    </update>
    <update>
        <wogroup>id1</wogroup>
        <woid>SL0001</woid>
        <status>OPEN</status>
    </update>
    <update>
        <wogroup>id1</wogroup>
        <woid>SL0001</woid>
        <status>CLOSED</status>
    </update>
    <update>
        <wogroup>id1</wogroup>
        <woid>SL0002</woid>
        <status>NEW</status>
    </update>
    <update>
        <wogroup>id2</wogroup>
        <woid>SL00011</woid>
        <status>OVERRIDE</status>
    </update>
    <update>
        <wogroup>id2</wogroup>
        <woid>SL00011</woid>
        <status>CLOSED</status>
    </update>
    <update>
        <wogroup>id2</wogroup>
        <woid>SL00021</woid>
        <status>NEW</status>
    </update>
</operation>

I have no saying on how the xml looks like, but I need to make a report in html. I would like to see an html like

Group : id1
    WO : SL001
        NEW
        OPEN
        CLOSED
    WO : SL002
        NEW
Group : id2
    WO : SL0011
       OVERRIDE
       CLOSED
    WO : SL0021
       NEW

I succeed in grouping on wogroup or woid, but not in a nested way ... anyone a suggestion?

Upvotes: 3

Views: 953

Answers (1)

Fernando
Fernando

Reputation: 1278

I don't know if this is the best way but it groups elements as you need:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
   <xsl:output method="xml" encoding="UTF-8" />

   <xsl:key name="updates-by-wogroup" match="/operation/update" use="wogroup" />
   <xsl:key name="updates-by-wogroup-and-woid" match="/operation/update" use="concat(wogroup,woid)" />

   <xsl:template match="/">
      <xsl:for-each select="operation/update[not(wogroup = preceding-sibling::update/wogroup)]">
        Group: <xsl:value-of select="wogroup" />
        <xsl:for-each select="key('updates-by-wogroup',current()/wogroup)[not(woid = preceding-sibling::update/woid)]">
          WO: <xsl:value-of select="woid" />
          <xsl:for-each select="key('updates-by-wogroup-and-woid',concat(current()/wogroup,current()/woid))">
            <xsl:text>
              <xsl:value-of select="status" /></xsl:text>
            </xsl:for-each>
          </xsl:for-each>
      </xsl:for-each>
   </xsl:template>
</xsl:stylesheet>

If you succeeded in grouping by wogroup or woid you will understand the code, anyway ask any question if you need.

Upvotes: 2

Related Questions