Re: [xsl] grouping problem

Subject: Re: [xsl] grouping problem
From: "Imsieke, Gerrit, le-tex" <gerrit.imsieke@xxxxxxxxx>
Date: Fri, 16 Apr 2010 01:32:26 +0200
On 16.04.2010 00:50, Terry Ofner wrote:

I suspect that I should be using group-adjacent but am I unsure how to go ahead with such grouping in this case.


Spring time is grouping time on this list, so it seems.


You are right, group-adjacent is your friend, but not alone: consider the case that two question/choice blocks are immediately adjacent. Then group-adjacent won't seperate them at each block's question line, but instead will put them all together. So I suggest to use group-starting-with with a group-adjacent applied to each primary group (in order to filter out the non-question paras). Or you could do it the other way round: perform a group-adjacent on the boolean condition that there is question/choice content, then group the groups where current-grouping-key() is true, starting at each question.

Here's a sample stylesheet for the first solution:

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

<xsl:output method="xml" indent="yes" />

  <xsl:template match="testbody">
    <testbody>
      <xsl:for-each-group select="*"
        group-starting-with="p[
                               @class=(
                                 'bodytext-question',
                                 'bodytext-questionfirst'
                               )
                             ]">
        <xsl:choose>
          <!-- Don't consider the first group if it starts with
               something else: -->
          <xsl:when test="self::p[
                            @class=(
                              'bodytext-question',
                              'bodytext-questionfirst'
                            )
                          ]">
            <xsl:for-each-group select="current-group()"
              group-adjacent="boolean(
                                self::p[
                                  @class=(
                                    'bodytext-question',
                                    'bodytext-questionfirst',
                                    'bodytext-choice'
                                  )
                                ]
                              )">
              <xsl:choose>
                <xsl:when test="current-grouping-key()">
                  <xsl:element name="question">
                    <xsl:apply-templates select="current-group()"/>
                  </xsl:element>
                </xsl:when>
                <xsl:otherwise>
                  <xsl:apply-templates select="current-group()"/>
                </xsl:otherwise>
              </xsl:choose>
            </xsl:for-each-group>
          </xsl:when>
          <xsl:otherwise>
            <xsl:apply-templates select="current-group()"/>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:for-each-group>
    </testbody>
  </xsl:template>

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

</xsl:stylesheet>
======8<-----------------------------

Gerrit

Current Thread