Re: [xsl] Different behaviour for the first item in a for-each-group

Subject: Re: [xsl] Different behaviour for the first item in a for-each-group
From: "G. Ken Holman" <gkholman@xxxxxxxxxxxxxxxxxxxx>
Date: Sat, 31 Oct 2009 14:38:41 -0400
At 2009-10-31 11:10 -0700, Mark Wilson wrote:
I have a working template that I need to modify. At the moment, as it iterates through the for-each-group, every group (Title) is treated identically. I need to provide a different behavior The first group will be wrapped by a row/cell with the "keep "property, all others in a non-"keep" property row/cell. The current template is in Listing 1, my attempt to modify it in listing 2. Before I bollix everything up, would someone look it over and tell me if I have done it right or if there is a better way?

There are better approaches to what you've done that I think will help. Your listing 2 is characterized by a lot of duplication that I think is not necessary.


Listing 2:
<xsl:template name="format-article">
       <xsl:for-each-group select="../Article" group-by="Title">
           <xsl:sort select="Title"/>
           <xsl:sort select="Year"/>
           <xsl:sort select="IssueNumber" data-type="number"/>
           <xsl:sort select="Page"/>
           <!-- Put the first citation in a keeping row-->
           <xsl:for-each select="current-group()">
               <xsl:choose>
                   <xsl:when test="current-group()[1]">

The above test will always be true because you are testing that the group exists (in that the first member of the group exists) which is going to be true because you just created the group.


<fo:table-row keep-with-previous="always">

The above attribute appears to be the only difference in the behaviour, and that correlates with your description above, so there is a better way to address this requirement by only selectively adding the attribute:


<xsl:template name="format-article">
<xsl:for-each-group select="../Article" group-by="Title">
<xsl:sort select="Title"/>
<xsl:sort select="Year"/>
<xsl:sort select="IssueNumber" data-type="number"/>
<xsl:sort select="Page"/>
<!-- Put the first citation in a keeping row-->
<xsl:for-each select="current-group()">
<fo:table-row>
<xsl:if test="position()=1">
<xsl:attribute name="keep-with-previous">always</xsl:attribute>
</xsl:if>
<fo:table-cell>
<fo:block xsl:use-attribute-sets="citation">
<fo:wrapper xsl:use-attribute-sets="title">
<xsl:apply-templates select="Title"/>
</fo:wrapper>
<xsl:apply-templates select="Person"/>
<xsl:apply-templates select="current-group()"
mode="format"/>
</fo:block>
</fo:table-cell>
</fo:table-row>
</xsl:for-each>
...


I hope this helps!

. . . . . . . . . . . . Ken


-- Upcoming: hands-on XSLT, XQuery and XSL-FO Washington DC Nov 2009 Interested in other classes? http://www.CraneSoftwrights.com/s/i/ Crane Softwrights Ltd. http://www.CraneSoftwrights.com/s/ Training tools: Comprehensive interactive XSLT/XPath 1.0/2.0 video Video lesson: http://www.youtube.com/watch?v=PrNjJCh7Ppg&fmt=18 Video overview: http://www.youtube.com/watch?v=VTiodiij6gE&fmt=18 G. Ken Holman mailto:gkholman@xxxxxxxxxxxxxxxxxxxx Male Cancer Awareness Nov'07 http://www.CraneSoftwrights.com/s/bc Legal business disclaimers: http://www.CraneSoftwrights.com/legal

Current Thread