XSLT sorting and grouping by child element

`I have an XML file containing many" Entry "nodes. Each "Input" node usually has a child "Year" and "Title".

I am trying to generate HTML output that looks like this:

2012
    Title
    Title

2011
    Title

2010
    Title
    Title

      

This pattern must continue as long as the record with a unique year exists. I am having trouble printing the year only once. I can print it once every year, but it will only print the title of one (at the first) post. I can get it to print every entry with this year, but it will show the year for every entry. I used a combination of variables, generate-ids and keys, but ran into the problems described (I could be doing everything wrong).

  • There is no limit on the number of years, the number of entries, or the number of entries per year / group.
  • Each year should only be displayed once.

In HTML terms, I would like each subgroup (which separates the year) to be in its own ordered list, but if that's not possible, I wouldn't be surprised!

...

The general XML file template I'm working with looks 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 fine.

Here is the template matching for Entry (it just takes the elements and puts everything in one list element).

    <xsl:if test="Download">
  <xsl:text disable-output-escaping="yes">&lt;a href="http://thisisnot.real/publications/</xsl:text>
  <xsl:value-of select="Download"/>
  <xsl:text disable-output-escaping="yes">"&gt;</xsl:text>
    </xsl:if>
    <xsl:value-of select="Title"/>
    <xsl:if test="Download">
  <xsl:text disable-output-escaping="yes">&lt;/a&gt;</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">&gt;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>

      

+3


source to share


1 answer


Here option 2.0 ...

XML input

<root>
  <Entry>
    <Title>Title B</Title>
    <Year>2010</Year>
  </Entry>
  <Entry>
    <Title>Title A</Title>
    <Year>2010</Year>
  </Entry>
  <Entry>
    <Title>Title B</Title>
    <Year>2012</Year>
  </Entry>
  <Entry>
    <Title>Title A</Title>
    <Year>2011</Year>
  </Entry>
  <Entry>
    <Title>Title A</Title>
    <Year>2012</Year>
  </Entry>
</root>

      

XSLT 2.0

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output indent="yes"/>
  <xsl:strip-space elements="*"/>

  <xsl:template match="root">
    <html>
      <xsl:for-each-group select="Entry" group-by="Year">
        <xsl:sort select="Year" data-type="number" order="descending"/>
        <p><xsl:value-of select="Year"/></p>
        <ol>
          <xsl:apply-templates select="current-group()/Title">
            <xsl:sort select="." data-type="text" order="ascending"/>
          </xsl:apply-templates>
        </ol>
      </xsl:for-each-group>
    </html>
  </xsl:template>

  <xsl:template match="Title">
    <li><xsl:value-of select="."/></li>
  </xsl:template>

</xsl:stylesheet>

      

HTML output (code)

<html>
   <p>2012</p>
   <ol>
      <li>Title A</li>
      <li>Title B</li>
   </ol>
   <p>2011</p>
   <ol>
      <li>Title A</li>
   </ol>
   <p>2010</p>
   <ol>
      <li>Title A</li>
      <li>Title B</li>
   </ol>
</html>

      

HTML output (display)



 

2012

 
  •     
  • Heading A    
  • Heading B 
 

2011

 
  •     
  • Heading A 
 

2010

 
  •     
  • Heading A    
  • Heading B 
+3


source







All Articles