<?xml version="1.0"?>

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

<xsl:param name="months" select="document('months.xml')" />

<!-- set the output properties -->
<xsl:output method="html"/>

<xsl:template mode="copy" match="*">
  <xsl:call-template name="copy" />
</xsl:template>

<xsl:template name="copy">
  <xsl:copy>
    <xsl:for-each select="./@*">
      <xsl:copy/>
    </xsl:for-each>
    <xsl:apply-templates mode="copy" />
  </xsl:copy>
</xsl:template>

<!-- main rule for document element -->
<xsl:template match="tdif">
<html>
  <head>
    <title>My diary</title>
  </head>
  <body>
    <h1>My diary</h1>
    <xsl:apply-templates select="entry">
      <xsl:sort select="date" order="descending" />
    </xsl:apply-templates>
  </body>
</html>
</xsl:template>

<xsl:template match="contents/html/*">
  <xsl:call-template name="copy" />
</xsl:template>

<xsl:template match="date">
  <xsl:param name="year" select="substring (node(), 1, 4)" />
  <xsl:param name="month" select="substring (node(), 6, 2)" />
  <xsl:param name="day" select="substring (node(), 9, 2)" />

  <xsl:value-of select="$day" />
  <xsl:text> </xsl:text>
  <xsl:value-of select="$months/months/month[number($month)]/name" />
  <xsl:text> </xsl:text>
  <xsl:value-of select="$year" />
</xsl:template>

<xsl:template match="entry">
  <div style="margin: 0.5cm;">
  <b>
    <xsl:apply-templates select="date" />
  </b>
  <div style="margin: 0cm 2cm;">
    <xsl:apply-templates select="contents/html" />
  </div>
  </div>
</xsl:template>

</xsl:stylesheet>
