<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html [
 <!ENTITY Egrave "&#200;">
 <!ENTITY egrave "&#232;">
 <!ENTITY eacute "&#233;">
 <!ENTITY igrave "&#236;">
 <!ENTITY agrave "&#224;">
 <!ENTITY ugrave "&#249;">
 <!ENTITY nbsp "&#160;">
 <!ENTITY copy "&#0169;">
 <!ENTITY laquo "&#0171;">
 <!ENTITY raquo "&#0187;">
]>

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" encoding="utf-8" indent="yes" />

  <xsl:param name="id" />
  <xsl:param name="source" />
  <xsl:param name="language" />
  <xsl:param name="date" />

  <xsl:param name="sortField">id</xsl:param>

  <xsl:param name="sortOrder">
    <xsl:choose>
      <xsl:when test="$sortField='id'">descending</xsl:when>
      <xsl:otherwise>ascending</xsl:otherwise>
    </xsl:choose>
  </xsl:param>
 
  <xsl:param name="limit">5</xsl:param>

  <xsl:param name="withpic">false</xsl:param>
  <xsl:param name="withlink">false</xsl:param>
  <xsl:param name="showattach">true</xsl:param>
  <xsl:param name="showlink">true</xsl:param>

  <xsl:param name="preview">external</xsl:param>

  <xsl:variable name="dataType">
   <xsl:choose>
    <xsl:when test="$sortField='id'">number</xsl:when>
    <xsl:otherwise>text</xsl:otherwise>
   </xsl:choose>
  </xsl:variable>


<xsl:template match="/">
  <xsl:apply-templates select="blog" />
</xsl:template>

<!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -->

<xsl:template match="blog">
  <xsl:for-each select="post">
    <xsl:sort select="./*[local-name()=$sortField]" order="{$sortOrder}" data-type="{$dataType}" />
    <xsl:if test="($id = ./id) or ($id = '') ">
      <xsl:if test="( ($withpic = 'false') or (count(./attachments/*) &gt; 0 ) ) and ( ($withlink = 'false') or (count(./links/*) &gt; 0 ) )">
        <xsl:apply-templates select="." />
      </xsl:if>
    </xsl:if>
  </xsl:for-each>
</xsl:template>

<!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -->

<xsl:template match="post">
        <div class="post">
          <h2 class="title"> <xsl:value-of select="subject"/> <a id="blog_{./id}" name="blog_{./id}"><xsl:text> </xsl:text></a></h2>
          <div class="entry"><xsl:text> </xsl:text>
             <xsl:apply-templates select="message"/>
          </div>
         <xsl:if test="count(tags/*) &gt; 0">
           <xsl:apply-templates select="tags"/>
         </xsl:if>
         <xsl:if test="count(attachments/*) &gt; 0">
           <xsl:apply-templates select="attachments"/>
         </xsl:if>
         <xsl:if test="count(links/*) &gt; 0">
           <xsl:apply-templates select="links"/>
         </xsl:if>
          <div class="meta">
            <p class="byline">Posted on <xsl:call-template name="render-date"><xsl:with-param name="date" select="postdate"/></xsl:call-template>, Alessandro Polo</p>
            <p class="links">
              <a href="/blog/blog.html?id={./id}" class="more">printer friendly</a> <b>|</b> 
              <xsl:choose>
                <xsl:when test="count(comments/comment) &gt; 0">
                  <a href="#" class="comments">Comments (<xsl:value-of select="count(comments/comment)"/>)</a>
                </xsl:when>
                <xsl:otherwise>
                  <span class="comments">No Comments</span>
                </xsl:otherwise>
              </xsl:choose>
            </p>
          </div>
        </div>
</xsl:template>

<!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -->

<xsl:template match="message | text">
  <xsl:apply-templates />
</xsl:template>

<xsl:template match="image">
  <a href="{../../attachments/picture[../../attachments/picture/@name = @name]}" title="{../../attachments/picture[@name = @name]/@description}"><img src="{../../attachments/picture[@name = @name]}" alt="{../../attachments/picture[@name = @name]/@description}" class="resized" /></a>
</xsl:template>

<xsl:template match="picture">
  <a href="{.}" title="{@description}"><img src="{.}" alt="{@description}" class="resized" /></a>
</xsl:template>

<!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -->

<xsl:template match="tags">
<div class="extra">
  <xsl:choose>
    <xsl:when test="count(./*) &gt; 1">
      <p><strong>Tags</strong>: <xsl:for-each select="tag">
         <a href="/tags/{.}"><xsl:value-of select="."/></a>
         <xsl:if test="position() &lt; last()">
           <xsl:text>, </xsl:text>
         </xsl:if>
        </xsl:for-each></p>
    </xsl:when>
    <xsl:otherwise>
      <p><strong>Tag</strong>: <a href="/tags/{./tag/.}"><xsl:value-of select="./tag/."/></a></p>
    </xsl:otherwise>
   </xsl:choose>
</div>
</xsl:template>

<!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -->

<xsl:template match="attachments">
<div class="extra">
  <xsl:choose>
    <xsl:when test="count(./*) &gt; 1">
      <p><strong>Attachments</strong> (<xsl:value-of select="count(./*)"/>)</p>
      <xsl:if test="$showattach = 'true'">
        <ul>
          <xsl:for-each select="picture">
            <li><a href="{.}" title="{@description}"><xsl:value-of select="."/></a></li>
          </xsl:for-each>
        </ul>
      </xsl:if>
    </xsl:when>
    <xsl:otherwise>
      <p><strong>Attachment</strong>: <a href="{./*/.}" title="{@./*/description}"><xsl:value-of select="./*/."/></a></p>
    </xsl:otherwise>
   </xsl:choose>
</div>
</xsl:template>

<!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -->

<xsl:template match="links">
<div class="extra">
  <xsl:choose>
    <xsl:when test="count(./*) &gt; 1">
      <p><strong>Links</strong> (<xsl:value-of select="count(./*)"/>)</p>
      <xsl:if test="$showlink = 'true'">
        <ul>
          <xsl:for-each select="post">
            <li><a href="/blog/{.}" title="{@description}">post://<xsl:value-of select="."/></a></li>
          </xsl:for-each>
          <xsl:for-each select="url">
            <li><a href="{.}" title="{@description}"><xsl:value-of select="."/></a></li>
          </xsl:for-each>
        </ul>
      </xsl:if>
    </xsl:when>
    <xsl:otherwise>
      <p><strong>Links</strong>: 
        <xsl:for-each select="post">
            <a href="/blog/{.}" title="{@description}">post://<xsl:value-of select="."/></a>
        </xsl:for-each>
        <xsl:for-each select="url">
            <a href="{.}" title="{@description}"><xsl:value-of select="."/></a>
        </xsl:for-each>
      </p>
    </xsl:otherwise>
   </xsl:choose>
</div>

</xsl:template>

<!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -->


<!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -->

<xsl:template name="render-date">
   <xsl:param name="date" />

   <xsl:if test="not (substring($date, 9, 1) = '0')">
    <xsl:value-of select="substring($date, 9, 1)" />
   </xsl:if>
   <xsl:value-of select="substring($date, 10, 1)" />
   <xsl:text> </xsl:text>

  <xsl:choose>
    <xsl:when test="$language = 'it-IT'">
      <xsl:choose>
        <xsl:when test="substring($date, 6, 2) = '01'">Gennaio</xsl:when>
        <xsl:when test="substring($date, 6, 2) = '02'">Febbraio</xsl:when>
        <xsl:when test="substring($date, 6, 2) = '03'">Marzo</xsl:when>
        <xsl:when test="substring($date, 6, 2) = '04'">Aprile</xsl:when>
        <xsl:when test="substring($date, 6, 2) = '05'">Maggio</xsl:when>
        <xsl:when test="substring($date, 6, 2) = '06'">Giugno</xsl:when>
        <xsl:when test="substring($date, 6, 2) = '07'">Luglio</xsl:when>
        <xsl:when test="substring($date, 6, 2) = '08'">Agosto</xsl:when>
        <xsl:when test="substring($date, 6, 2) = '09'">Settembre</xsl:when>
        <xsl:when test="substring($date, 6, 2) = '10'">Ottobre</xsl:when>
        <xsl:when test="substring($date, 6, 2) = '11'">Novembre</xsl:when>
        <xsl:when test="substring($date, 6, 2) = '12'">Dicembre</xsl:when>
      </xsl:choose>
    </xsl:when>
    <xsl:otherwise>
      <xsl:choose>
        <xsl:when test="substring($date, 6, 2) = '01'">January</xsl:when>
        <xsl:when test="substring($date, 6, 2) = '02'">February</xsl:when>
        <xsl:when test="substring($date, 6, 2) = '03'">March</xsl:when>
        <xsl:when test="substring($date, 6, 2) = '04'">April</xsl:when>
        <xsl:when test="substring($date, 6, 2) = '05'">May</xsl:when>
        <xsl:when test="substring($date, 6, 2) = '06'">June</xsl:when>
        <xsl:when test="substring($date, 6, 2) = '07'">July</xsl:when>
        <xsl:when test="substring($date, 6, 2) = '08'">August</xsl:when>
        <xsl:when test="substring($date, 6, 2) = '09'">September</xsl:when>
        <xsl:when test="substring($date, 6, 2) = '10'">October</xsl:when>
        <xsl:when test="substring($date, 6, 2) = '11'">November</xsl:when>
        <xsl:when test="substring($date, 6, 2) = '12'">December</xsl:when>
      </xsl:choose>
    </xsl:otherwise>
   </xsl:choose>

   <xsl:text> </xsl:text>
   <xsl:value-of select="substring($date, 1, 4)" />

   <xsl:text>, </xsl:text>
   <xsl:value-of select="substring($date, 12, 2)" />
   <xsl:text>:</xsl:text>
   <xsl:value-of select="substring($date, 15, 2)" />

</xsl:template>
<!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -->
<!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -->

<xsl:template name="add-line-breaks">
   <xsl:param name="string" />
   <xsl:choose>
      <xsl:when test="contains($string, '&#xD;')">
         <xsl:value-of select="substring-before($string, '&#xD;')" />
         <br />
         <xsl:call-template name="add-line-breaks">
            <xsl:with-param name="string" select="substring-after($string, '&#xD;')" />
         </xsl:call-template>
      </xsl:when>
      <xsl:otherwise>
         <xsl:value-of select="$string" />
      </xsl:otherwise>
   </xsl:choose>
</xsl:template>

<!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -->
<!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -->
<!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -->

<xsl:template match="img">
  <xsl:choose>
    <xsl:when test="$preview = 'internal'">
      <a href="javascript:showImage('{./@src}');" title="{./@alt}"><img src="{./@src}" alt="{./@alt}" style="{./@style}" class="resized" /></a>
    </xsl:when>
    <xsl:otherwise>
      <a onclick="return showPic(this);" href="{./@src}" title="{./@alt}"><img src="{./@src}" alt="{./@alt}" style="{./@style}" class="resized" /></a>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>

<xsl:template match="anchor">
  <a name="{.}">&nbsp;<xsl:text> </xsl:text></a>
</xsl:template>


<xsl:template match="para">
  <xsl:element name="p">
    <xsl:for-each select="@*">
      <xsl:attribute name="{name()}"><xsl:value-of select="."/></xsl:attribute>
    </xsl:for-each>
    <xsl:apply-templates/>
  </xsl:element>
</xsl:template>


<xsl:template match="ul">
  <ul>
    <xsl:for-each select="li">
       <li><xsl:apply-templates /></li>
    </xsl:for-each>
  </ul>
</xsl:template>

<xsl:template match="ol">
  <ol>
    <xsl:for-each select="li">
       <li><xsl:apply-templates /></li>
    </xsl:for-each>
  </ol>
</xsl:template>

<xsl:template match="li">
   <xsl:apply-templates />
</xsl:template>


<xsl:template match="@*|a|br|i|strong" priority="-1">
  <xsl:copy><xsl:apply-templates select="@*|node()"/></xsl:copy>
</xsl:template>


<xsl:template match="text()">
  <xsl:value-of select="."/>
<!--      <xsl:value-of select="normalize-space()"/>    -->
</xsl:template>

<!--

<xsl:template match="@*|*|text()|processing-instruction()" priority="-1">
  <xsl:apply-templates select="@*|*|text()|processing-instruction()"/>
</xsl:template>

<xsl:template match="@*|node()" priority="-1">
  <xsl:copy><xsl:apply-templates select="@*|node()"/></xsl:copy>
</xsl:template>
-->


</xsl:stylesheet>

