Search code examples
xslt-1.0xslt-2.0

How to do for-each-group group-adjacent in XSLT 1.0


Can someone help me convert the below XSLT 2.0 code to XSLT 1.0? Thanks.

<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">

<xsl:strip-space elements="*"/>
<xsl:output indent="yes"/>

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

<xsl:template match="*[E1EDL24]">
    <xsl:copy>
        <xsl:apply-templates select="@*"/>
        <xsl:for-each-group select="node()" group-adjacent="boolean(self::E1EDL24)">
            <xsl:choose>
                <xsl:when test="current-grouping-key()">
                     <xsl:apply-templates select="current-group()">
                        <xsl:sort select="MATNR" order="descending"/>
                    </xsl:apply-templates>                        
                </xsl:when>
                <xsl:otherwise>
                    <xsl:apply-templates select="current-group()"/>
                </xsl:otherwise>
            </xsl:choose>
        </xsl:for-each-group>        
    </xsl:copy>
</xsl:template>

</xsl:transform>


Solution

  • Try whether the following works:

    <?xml version="1.0" encoding="utf-8"?>
    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
      version="1.0">
    
    <xsl:strip-space elements="*"/>
    <xsl:output indent="yes"/>
    
    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>
    
    <xsl:template match="@*|node()" mode="sort">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>
    
    <xsl:key name="adjacent-group" match="E1EDL24[preceding-sibling::*[1][self::E1EDL24]]" use="generate-id(preceding-sibling::E1EDL24[not(preceding-sibling::*[1][self::E1EDL24])][1])"/>
    
    <xsl:template match="E1EDL24[not(preceding-sibling::*[1][self::E1EDL24])]">
      <xsl:apply-templates select=". | key('adjacent-group', generate-id())" mode="sort">
        <xsl:sort select="MATNR" order="descending"/>
      </xsl:apply-templates>
    </xsl:template>
    
    <xsl:template match="E1EDL24[preceding-sibling::*[1][self::E1EDL24]]"/>
    
    </xsl:stylesheet>