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

<!--

  This transformation converts the ccfraud.xml XML file
  to a HTML table.

-->

<xsl:output method="xhtml" omit-xml-declaration = "yes" encoding="utf-8" indent="yes"/>

<xsl:param name="sort" select="''"/>
<xsl:param name="script" select="'index.php'"/>

<xsl:variable name="fields">
  <field>retailer</field>
  <field>comment</field>
  <field>number</field>
  <field>from</field>
  <field>expires</field>
  <field>name</field>
  <field>signature</field>
</xsl:variable>

<!-- Main template -->
<xsl:template match="/receipts">

   <table>

      <!-- Output the table headings -->
      <tr>
        <th><a href="{$script}#TABLE" title="Sort by retailer">Retailer</a></th>
        <th>Comment</th>
        <th class="number">Digits</th>
        <th>From</th>
        <th>Expiry</th>
        <th>Name</th>
        <th>Sig</th>
        <th>Score <a href="{$script}?sort=up#TABLE" title="Sort by score, lowest first"><img src="up.gif" alt="up"/></a> <a href="{$script}?sort=down#TABLE" title="Sort by score, highest first."><img src="down.gif" alt="down"/></a></th>
      </tr>

      <!-- Generate the intermediate output including the scores -->
      <xsl:variable name="tmp">
        <xsl:apply-templates select="receipt"/>
      </xsl:variable>

      <!-- Sort the intermediate output ($tmp) with the chosen sort order -->
      <xsl:choose>
        <xsl:when test="$sort='down'">
          <xsl:apply-templates select="$tmp/tr">
            <xsl:sort select="td[@score]" data-type="number" order="descending" lang="C" />
          </xsl:apply-templates>
        </xsl:when>
        <xsl:when test="$sort='up'">
          <xsl:apply-templates select="$tmp/tr">
            <xsl:sort select="td[@score]" data-type="number" order="ascending" lang="C"/>
          </xsl:apply-templates>
        </xsl:when>
        <xsl:otherwise>
          <xsl:apply-templates select="$tmp/tr">
            <xsl:sort select="td[@class='retailer']" data-type="text" lang="C"/>
          </xsl:apply-templates>
        </xsl:otherwise>
      </xsl:choose>
   </table>

</xsl:template>

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

<xsl:template match="td[@score]">
  <td class="s{@score}"><xsl:value-of select="@score"/></td>
</xsl:template>

<xsl:template match="td">
  <xsl:copy-of select="."/>
</xsl:template>

<!-- Transform a receipt element to a table row and calculate the score -->
<xsl:template match="receipt">

  <!-- $count is the count of digits in the CC number -->
  <xsl:variable name="count">
    <xsl:value-of select="string-length(translate(number,'x.','X'))"/>
  </xsl:variable>

  <!-- $score is the remaining part of the score -->
  <xsl:variable name="score">
    <xsl:call-template name="score"/>
  </xsl:variable>

  <!-- Hmm.  This is ugly, but we need it when the for-each
       changes the context -->
  <xsl:variable name="row" select="."/>

  <tr>
    <!-- Process each field in turn -->
    <xsl:for-each select="$fields/field">
      <xsl:choose>
        <xsl:when test="$row/*[name()=current()/.]">
          <xsl:apply-templates select="$row/*[name()=current()/.]">
            <xsl:with-param name="count" select="$count"/>
          </xsl:apply-templates>
        </xsl:when>
        <xsl:otherwise>
          <td class="{.}"><br/></td>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:for-each>

    <!-- And now do the score -->
    <td score="{$score+$count}"><xsl:value-of select="$score + $count"/></td>
  </tr>
</xsl:template>

<!-- Each of the row items in turn -->

<xsl:template match="retailer|comment">
  <td class="{name()}"><xsl:value-of select="."/></td>
</xsl:template>

<xsl:template match="number">
  <xsl:param name="count"/>
  <td>
    <xsl:attribute name="class">
      <xsl:choose>
        <xsl:when test="$count > 12">
          <xsl:text>num12</xsl:text>
        </xsl:when>
        <xsl:when test="$count > 8">
          <xsl:text>num8</xsl:text>
        </xsl:when>
        <xsl:when test="$count > 4">
          <xsl:text>num4</xsl:text>
        </xsl:when>
        <xsl:otherwise>
          <xsl:text>num0</xsl:text>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:attribute>
    <xsl:value-of select="."/>
  </td>
</xsl:template>

<xsl:template match="from|expires|name|signature">
  <td class="{name()}-x">x</td>
</xsl:template>

<!-- Calculate the score excluding the CC digits -->
<xsl:template name="score">
  <xsl:variable name="accum">
    <!-- The string lengths set the scores for the various offences -->
    <xsl:if test="./from">X</xsl:if>
    <xsl:if test="./expires">XXXXX</xsl:if>
    <xsl:if test="./name">XXXXXXXXXX</xsl:if>
    <xsl:if test="./signature">XXX</xsl:if>
  </xsl:variable>
  <xsl:value-of select="string-length($accum)"/>
</xsl:template>

</xsl:stylesheet>