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: Martin Honnen <Martin.Honnen@xxxxxx>
Date: Sat, 31 Oct 2009 19:31:11 +0100
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?

<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]">
<fo:table-row keep-with-previous="always">
<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"/>
<!--and for each citation-->
<xsl:apply-templates select="current-group()" mode="format"/>
</fo:block>
</fo:table-cell>
</fo:table-row>
</xsl:when>
<xsl:otherwise>
<fo:table-row>
<fo:table-cell>
<!--and put all the other citations in one a non-keeping row-->
<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:otherwise>
</xsl:choose>
</xsl:for-each>
</xsl:for-each-group>
</xsl:template>

Inside of the for-each-group position() will give you the "number" of the group being processed thus if you want to treat the first group differently then I think you want


<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"/>


<xsl:choose>
<xsl:when test="position() eq 1">
<fo:table-row keep-with-previous="always">
<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"/>
<!--and for each citation-->
<xsl:apply-templates select="current-group()" mode="format"/>
</fo:block>
</fo:table-cell>
</fo:table-row>
</xsl:when>
<xsl:otherwise>
<fo:table-row>
<fo:table-cell>
<!--and put all the other citations in one a non-keeping row-->
<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:otherwise>
</xsl:choose>


       </xsl:for-each-group>
   </xsl:template>

--

	Martin Honnen
	http://msmvps.com/blogs/martin_honnen/

Current Thread