RE: [xsl] grouping problem

Subject: RE: [xsl] grouping problem
From: "Michael Kay" <mike@xxxxxxxxxxxx>
Date: Fri, 16 Apr 2010 00:04:03 +0100
> Here is the basic grouping 2.0 template (xsl:attribute 
> elements have been removed) in which I am trying to group the 
> @class='bodytext- question and @class=bodytext-choice elements:
> 
>   <xsl:template match="testbody">
>            <testbody>
>                 <xsl:for-each-group select="*" 
> group-starting-with="p 
> [@class=('bodytext-question','bodytext-questionfirst')]">
> 
> This template works for bodytext-questions that are followed 
> by other bodytext-questions. But it also groups everything 
> from the start of <testbody> to the first bodytext-question 
> as well as everything that follows the last of a series of 
> bodytext-questions.

Every node selected by select="*" will end up in exactly one group. 

For the nodes preceding the first "matching" node, the usual strategy is to
ungroup them again (or to do some other special processing on them)
typically by testing within xsl:for-each-group whether the first item in the
group does indeed match the group-starting-with pattern. As for "everything
that follows the last of a series of bodytext-questions", it's not clear to
me from your example what this means, but the last group is going to take in
everything from the last bodytext-question to the end, and you're going to
have to include logic to break it up if necessary.

I don't know whether this works in your case, but one technique of course is
to replace select="*" by a more specific select that only contains the nodes
you want in the output.

Regards,

Michael Kay
http://www.saxonica.com/
http://twitter.com/michaelhkay 

Current Thread