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: "Mark Wilson" <mark@xxxxxxxxxxxx>
Date: Sat, 31 Oct 2009 15:51:35 -0700
Hi Ken,
I was not careful in the way I described this problem. Each group consists of a single title with one or more citations. What I must have said is that I wanted to set the Title in each group to "keep". What I *should* have said was, I want to set the only the first group to "keep". For the input data at the end of this email, that would be the title "Further observations". All the rest would not be marked "keep".


Clearly, I do not want (from listing 1):
<xsl:if test="position()=1">
<xsl:attribute name="keep-with-previous">always</xsl:attribute>
</xsl:if>
However, I do not know XPath well. How do I select only the first group for marking and let the other groups pass without marking them?


All help appreciated.
Mark

Listing 1:
<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:for-each select="current-group()">
<fo:table-row>
<!-- Put the first group in a keeping row-->
<xsl:if test="position()=1">
<xsl:attribute name="keep-with-previous">always</xsl:attribute>
</xsl:if>
<!-- And all the other groups in a non-"keeping" row -->
<fo:table-cell>
<fo:block>
<xsl:choose>
<xsl:when test="parent::Item">
<xsl:call-template name="article0"/>
</xsl:when>
<xsl:when test="parent::Level1">
<xsl:call-template name="article1"/>
</xsl:when>
<xsl:when test="parent::Level2">
<xsl:call-template name="article2"/>
</xsl:when>
<xsl:when test="parent::Level3">
<xsl:call-template name="article3"/>
</xsl:when>
</xsl:choose>
<fo:block xsl:use-attribute-sets="citation">
<fo:wrapper xsl:use-attribute-sets="title">
<!--for a given 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:block>
</fo:table-cell>
</fo:table-row>
</xsl:for-each>
</xsl:for-each-group>
</xsl:template>


Input data:
<Item>
<Article>
<Title>Further observations</Title>
<IssueName>Mar/Apr</IssueName>
<Year>1999</Year>
<Page>34</Page>
<IssueNumber>2</IssueNumber>
</Article>
<Article>
<Title>Letters to the editor</Title>
<IssueName>Nov/Dec</IssueName>
<Year>2001</Year>
<Page>27</Page>
<IssueNumber>6</IssueNumber>
</Article>
<Article>
<Title>Philatelic news and views</Title>
<IssueName>Sep/Oct</IssueName>
<Year>2001</Year>
<Page>35</Page>
<IssueNumber>5</IssueNumber>
</Article>
<Article>
<Title>Philatelic news and views</Title>
<IssueName>Sep/Oct</IssueName>
<Year>2002</Year>
<Page>11</Page>
<IssueNumber>5</IssueNumber>
</Article>
<Article>
<Title>Philatelic news and views</Title>
<IssueName>Nov/Dec</IssueName>
<Year>2002</Year>
<Page>28</Page>
<IssueNumber>6</IssueNumber>
</Article>
<Article>
<Title>Philatelic news and views</Title>
<IssueName>Jan/Feb</IssueName>
<Year>2003</Year>
<Page>24</Page>
<IssueNumber>1</IssueNumber>
</Article>
<Article>
<Title>Philatelic news and views</Title>
<IssueName>Nov/Dec</IssueName>
<Year>2006</Year>
<Page>18</Page>
<IssueNumber>6</IssueNumber>
</Article>
</Item>


Current Thread