xsl-list
[Top] [All Lists]

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

2020-09-16 17:16:04
On 17.09.2020 00:09, Imsieke, Gerrit, le-tex 
gerrit(_dot_)imsieke(_at_)le-tex(_dot_)de wrote:
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>

I was wondering what output is wanted for

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

Neither my suggestion nor the above seem to only wrap the adjacent
block[@width] where the accumulator gives 100.

I complicated or strengthened my attempt as

  <xsl:template match="blocks">
    <xsl:copy>
      <xsl:for-each-group select="block" group-adjacent="not(@width)">
        <xsl:choose>
          <xsl:when test="current-grouping-key()">
            <xsl:apply-templates select="current-group()"/>
          </xsl:when>
          <xsl:otherwise>
            <xsl:for-each-group select="current-group()"
group-ending-with="block[accumulator-before('width') eq 100]">
              <xsl:choose>
                <xsl:when
test="current-group()[last()][accumulator-before('width') eq 100]">
                  <block type="composite">
                    <xsl:apply-templates select="current-group()"/>
                  </block>
                </xsl:when>
                <xsl:otherwise>
                  <xsl:apply-templates select="current-group()"/>
                </xsl:otherwise>
              </xsl:choose>
            </xsl:for-each-group>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:for-each-group>
    </xsl:copy>
  </xsl:template>
--~----------------------------------------------------------------
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
EasyUnsubscribe: http://lists.mulberrytech.com/unsub/xsl-list/1167547
or by email: xsl-list-unsub(_at_)lists(_dot_)mulberrytech(_dot_)com
--~--

<Prev in Thread] Current Thread [Next in Thread>