<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    version="1.0" xmlns:xdif="http://www.wa5znu.org/2006/04/xdif/"
    xmlns:math="http://exslt.org/math" xmlns:common="http://exslt.org/common" >

  <xsl:output method="xml" indent="yes"/>

<!--
 Todo: loses non-qso elements in xdif:qsos
       make work as a filter (template/@mode)
       configure dup rules somehow?
       for mode, just uses the content part, not the emission
-->


  <!-- Dups key -->
  <xsl:key
      name="qsosbycallbandmode" match="xdif:qso[xdif:om/xdif:control/xdif:callsign and xdif:band and xdif:mode]"
      use="concat(xdif:om/xdif:control/xdif:callsign, '-', xdif:band, '-', xdif:mode)" />
  <xsl:variable
      name="nodupslist"
      select="/xdif:log/xdif:qsos/xdif:qso[generate-id(.) = generate-id(key('qsosbycallbandmode', concat(xdif:om/xdif:control/xdif:callsign, '-', xdif:band, '-', xdif:mode)))]" />

  <!-- See http://www-106.ibm.com/developerworks/xml/library/x-tipxsltmp.html for multi-pass XSLT -->
  <!-- http://www.dpawson.co.uk/xsl/sect1/approaches.html for filter -->
  <!--
      <xsl:template match="xdif:qso">
    <xsl:variable name="processed">
      <xsl:apply-templates mode="dups" />
    </xsl:variable>
    <xsl:apply-templates select="common:node-set($processed)" />
  </xsl:template>
  -->

  <!-- These two templates copy the entire document but leave specified elements open for alteration -->
  <xsl:template match="/">
    <xsl:copy>
      <xsl:copy-of select="@*"/>
      <xsl:apply-templates/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="*">
    <xsl:copy>
      <xsl:copy-of select="@*"/>
      <xsl:apply-templates/>
    </xsl:copy>
  </xsl:template>

  <!-- This template copies the QSOS portion but lets us filter out dups -->
  <xsl:template match="xdif:qsos" >
    <xsl:copy>
      <xsl:copy-of select="@*"/>
      <!-- this has the problem that it loses non-qso elements in xdif:qsos -->
      <xsl:copy-of select="common:node-set($nodupslist)" />
    </xsl:copy>
  </xsl:template>

</xsl:stylesheet>

