Re: [xsl] xsl:for-each-group help needed !

Subject: Re: [xsl] xsl:for-each-group help needed !
From: "Imsieke, Gerrit, le-tex gerrit.imsieke@xxxxxxxxx" <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx>
Date: Wed, 16 Sep 2020 22:09:06 -0000
With a single for-each-group:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
  xmlns:xs="http://www.w3.org/2001/XMLSchema";
  exclude-result-prefixes="xs" version="3.0">

  <xsl:mode default-mode="#unnamed"
            use-accumulators="width"
            on-no-match="shallow-copy"/>

  <xsl:accumulator name="width" as="xs:integer" initial-value="0">
    <xsl:accumulator-rule match="blocks" select="0"/>
    <xsl:accumulator-rule match="block[not(@width)]" select="100"/>
    <xsl:accumulator-rule match="block[@width]"
      select="if ($value eq 100)
              then xs:integer(@width)
              else $value + xs:integer(@width)"/>
  </xsl:accumulator>

  <xsl:template match="blocks">
    <xsl:copy>
      <xsl:for-each-group select="block"
        group-ending-with="block[accumulator-before('width') eq 100]">
        <xsl:choose>
          <xsl:when test="count(current-group()) eq 1">
            <xsl:apply-templates select="current-group()"/>
          </xsl:when>
          <xsl:otherwise>
            <block type="composite">
              <xsl:apply-templates select="current-group()"/>
            </block>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:for-each-group>
    </xsl:copy>
  </xsl:template>

</xsl:stylesheet>

On 16.09.2020 23:48, Martin Honnen martin.honnen@xxxxxx wrote:
On 16.09.2020 23:19, Christophe Marchand cmarchand@xxxxxxxxxx wrote:
Hello all !

A slide is divided as blocks. Usually, there is one bloc per line, but
sometimes, two or more blocks can be on one line, if their width sum is
100.

My input has non-grouped blocks, and I want my input group blocks per line.

Here is a sample input :

<blocks>
B B  <block id="i1">content</block>
B B  <block id="i2" width="33">content</block>
B B  <block id="i3" width="67">content</block>
B B  <block id="i4" width="50">content</block>
B B  <block id="i5" width="50">content</block>
B B  <block id="i6" width="25">content</block>
B B  <block id="i7" width="55">content</block>
B B  <block id="i8" width="20">content</block>
B B  <block id="i9">content</block>
</blocks>

Here is the expected output :

<blocks>
B B  <block id="i1">content</block>
B B  <block type="composite">
B B  B  <block id="i2" width="33">content</block>
B B B B  <block id="i3" width="67">content</block>
B B  </block>
B B  <block type="composite">
B B  B  <block id="i4" width="50">content</block>
B B B B  <block id="i5" width="50">content</block>
B B  </block>
B B  <block type="composite">
B B  B  <block id="i6" width="25">content</block>
B B B B  <block id="i7" width="55">content</block>
B B  B  <block id="i8" width="20">content</block>
B B  </block>
B B  <block id="i9">content</block>
</blocks>

I have no idea of which form of for-each-group I have to use...

Is a solution with two-pass where I first calculate cumulated width with
an accumulator, followed a group-end-with="@cumulative-width eq 100"
could be a correct solution ?

You can use that accumulator in your group-ending-with, without two passes I think, but I only could get it to work by wrapping it into an outer group-adjacent="not(@width)"

 B  <xsl:accumulator name="width" as="xs:integer" initial-value="0">
 B B B  <xsl:accumulator-rule match="blocks" select="0"/>
 B B B  <xsl:accumulator-rule match="block[not(@width)]" select="0"/>
 B B B  <xsl:accumulator-rule match="block[@width]" select="if ($value eq
100) then xs:integer(@width) else + $value + xs:integer(@width)"/>
 B  </xsl:accumulator>

 B  <xsl:template match="blocks">
 B B B  <xsl:copy>
 B B B B B  <xsl:for-each-group select="block" group-adjacent="not(@width)">
 B B B B B B B  <xsl:choose>
 B B B B B B B B B  <xsl:when test="current-grouping-key()">
 B B B B B B B B B B B  <xsl:apply-templates select="current-group()"/>
 B B B B B B B B B  </xsl:when>
 B B B B B B B B B  <xsl:otherwise>
 B B B B B B B B B B B  <xsl:for-each-group select="current-group()"
group-ending-with="block[accumulator-before('width') eq 100]">
 B B B B B B B B B B B B B  <block>
 B B B B B B B B B B B B B B B  <xsl:apply-templates select="current-group()"/>
 B B B B B B B B B B B B B  </block>
 B B B B B B B B B B B  </xsl:for-each-group>
 B B B B B B B B B  </xsl:otherwise>
 B B B B B B B  </xsl:choose>
 B B B B B  </xsl:for-each-group>
 B B B  </xsl:copy>
 B  </xsl:template>


-- Gerrit Imsieke GeschC$ftsfC<hrer / Managing Director le-tex publishing services GmbH Weissenfelser Str. 84, 04229 Leipzig, Germany Phone +49 341 355356 110, Fax +49 341 355356 510 gerrit.imsieke@xxxxxxxxx, http://www.le-tex.de

Registergericht / Commercial Register: Amtsgericht Leipzig
Registernummer / Registration Number: HRB 24930

GeschC$ftsfC<hrer / Managing Directors:
Gerrit Imsieke, Svea Jelonek, Thomas Schmidt

Current Thread