`I have an XML file that contains a lot of 'Entry' nodes. Within each 'Entry' node there is usually a 'Year' and 'Title' child.
I'm trying to generate an HTML output that looks like this:
2012
Title
Title
2011
Title
2010
Title
Title
This pattern should continue as long as an Entry with a unique year exists. I am having trouble getting it to only print the year once. I can get it to print each year once, but it will only print the title of one (assuming the first) entry. I can get it to print each entry with it's year, but it will show the year for each entry. I've used a combination of variables, generate-ids and keys but have ran into the problems described (I could have been doing each wrong).
- There is no limit to the number of years, number of entries, or number of entries per year/group.
- Each year should only be displayed once.
In terms of HTML, I'd like each sub-group (that shares a year) to be in its own Ordered List, but if that's not possible I wouldn't be surprised!
.
The general pattern of the XML file I am working with is like this:
root
Entry
Title
Year
<root>
<Entry>
<Title>Num 1</Title>
<Year>1991</Year>
</Entry>
<Entry>
<Title>Num 2</Title>
<Year>2011</Year>
</Entry>
<Entry>
<Title>Num 3</Title>
<Year>2012</Year>
</Entry>
<Entry>
<Title>Num 4</Title>
<Year>2012</Year>
</Entry>
</root>
XSL version 1.0 or 2.0 is okay.
Here is the template match for Entry (It just takes the elements and puts everything in a single line list-item).
<xsl:if test="Download">
<xsl:text disable-output-escaping="yes"><a href="http://thisisnot.real/publications/</xsl:text>
<xsl:value-of select="Download"/>
<xsl:text disable-output-escaping="yes">"></xsl:text>
</xsl:if>
<xsl:value-of select="Title"/>
<xsl:if test="Download">
<xsl:text disable-output-escaping="yes"></a></xsl:text>
</xsl:if>
<xsl:text>, </xsl:text>
<!-- Add the handling for (possibly multiple) Authors -->
<xsl:for-each select="Author">
<xsl:value-of select="."/>,
</xsl:for-each>
<xsl:text>In </xsl:text>
<EM>
<xsl:value-of select="Booktitle"/>
</EM>
<xsl:text>, </xsl:text>
<!-- Add the handling for Page -->
<xsl:if test="Page">
page <xsl:value-of select="Page"/>,
</xsl:if>
<!-- Add the handling for Address -->
<xsl:if test="Address">
<xsl:value-of select="Address"/>,
</xsl:if>
<!-- Add the handling for Year-Convert numeric Month to letters: eg, from 1 to January -->
<xsl:for-each select="Month">
<xsl:choose>
<xsl:when test=".=1">January <xsl:value-of select="../Year"/></xsl:when>
<xsl:when test=".=2">February <xsl:value-of select="../Year"/></xsl:when>
<xsl:when test=".=3">March <xsl:value-of select="../Year"/></xsl:when>
<xsl:when test=".=4">April <xsl:value-of select="../Year"/></xsl:when>
<xsl:when test=".=5">>May <xsl:value-of select="../Year"/></xsl:when>
<xsl:when test=".=6">June <xsl:value-of select="../Year"/></xsl:when>
<xsl:when test=".=7">July <xsl:value-of select="../Year"/></xsl:when>
<xsl:when test=".=8">August <xsl:value-of select="../Year"/></xsl:when>
<xsl:when test=".=9">September <xsl:value-of select="../Year"/></xsl:when>
<xsl:when test=".=10">October <xsl:value-of select="../Year"/></xsl:when>
<xsl:when test=".=11">November <xsl:value-of select="../Year"/></xsl:when>
<xsl:when test=".=12">December <xsl:value-of select="../Year"/></xsl:when>
</xsl:choose>
</xsl:for-each>
<!-- Add the handling for Note -->
<xsl:if test="Note">
<em>(<xsl:value-of select="Note"/>)</em>
</xsl:if>
<!-- Add the handling for AcceptRate in the grey color: #333333 -->
<xsl:if test="AcceptRate">
<font color="#333333"><small> Acceptance Rate: <xsl:value-of select="AcceptRate"/></small></font>
</xsl:if>
<!-- Add the handling for Award in the red color: #ff0000 -->
<xsl:if test="Award">
<font color="#ff0000"><strong> (<xsl:value-of select="Award"/>)</strong></font>
</xsl:if>
</LI>
Here's a 2.0 option...
XML Input
XSLT 2.0
HTML Output (code)
HTML Output (display)
2012
2011
2010