<?xml-stylesheet href="http://www.w3.org/StyleSheets/base.css" type="text/css"?><?xml-stylesheet href="http://www.w3.org/2002/02/style-xsl.css" type="text/css"?>
<xsl:stylesheet version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns="http://www.w3.org/1999/xhtml" xmlns:html="http://www.w3.org/1999/xhtml" exclude-result-prefixes="html" xmlns:cards="http://www.nimbustier.net/2004/09/cardsml"  xmlns:saxon="http://icl.com/saxon" extension-element-prefixes="saxon">

<!-- Output method XML -->
<xsl:output method="xml" 
  indent="yes"
  omit-xml-declaration="no" 
  encoding="utf-8" />

<ol class="colors">
  <li id="spades"/>
  <li id="hearts"/>
  <li id="diamonds"/>
  <li id="clubs"/>
</ol>

<ol class="players">
  <li id="north"/>
  <li id="east"/>
  <li id="south"/>
  <li id="west"/>
</ol>

<dl>
<dt>7</dt>
<dd>7</dd>
<dt>8</dt>
<dd>8</dd>
<dt>9</dt>
<dd>9</dd>
<dt>10</dt>
<dd>10</dd>
<dt>V</dt>
<dd>jack</dd>
<dt>D</dt>
<dd>queen</dd>
<dt>R</dt>
<dd>king</dd>
<dt>As</dt>
<dd>1</dd>
</dl>

 <xsl:template match="/html:html">
   <xsl:for-each select="//html:table">
   <saxon:output href="tour-{position()}-cards.xml" method="xml" indent="yes">
     <cards xmlns="http://www.nimbustier.net/2004/09/cardsml">
       <hand position="north">
         <xsl:for-each select="html:tr/html:td[1]">
           <xsl:call-template name="cards">
             <xsl:with-param name="cardList" select="concat(substring-before(.,html:del),substring-after(.,html:del))"/>
           </xsl:call-template>
         </xsl:for-each>
       </hand>
       <hand position="east">
         <xsl:for-each select="html:tr/html:td[2]">
           <xsl:call-template name="cards">
             <xsl:with-param name="cardList" select="concat(substring-before(.,html:del),substring-after(.,html:del))"/>
           </xsl:call-template>
         </xsl:for-each>

       </hand>
       <hand position="south">
         <xsl:for-each select="html:tr/html:td[3]">
           <xsl:call-template name="cards">
             <xsl:with-param name="cardList" select="concat(substring-before(.,html:del),substring-after(.,html:del))"/>
           </xsl:call-template>
         </xsl:for-each>

       </hand>
       <hand position="west">
         <xsl:for-each select="html:tr/html:td[4]">
           <xsl:call-template name="cards">
             <xsl:with-param name="cardList" select="concat(substring-before(.,html:del),substring-after(.,html:del))"/>
           </xsl:call-template>
         </xsl:for-each>
       </hand>
       <xsl:if test=".//html:del">
         <trick>
           <xsl:for-each select=".//html:td[html:del]">
             <xsl:variable name="line" select="count(parent::html:tr/preceding-sibling::html:tr)"/>
             <xsl:variable name="value" select="html:del"/>
             <xsl:variable name="col" select="position()"/>
             <card color="{document('')/xsl:stylesheet/html:ol[@class='colors']/html:li[position()=$line]/@id}" value="{document('')/xsl:stylesheet/html:dl/html:dt[normalize-space(.)=$value]/following-sibling::html:dd[1]}" playedBy="{document('')/xsl:stylesheet/html:ol[@class='players']/html:li[position()=$col]/@id}"/>
           </xsl:for-each>
         </trick>
       </xsl:if>
     </cards>
   </saxon:output>
   </xsl:for-each>
</xsl:template>

<xsl:template name="cards">
  <xsl:param name="cardList"/>
  <xsl:if test="normalize-space($cardList) and not(normalize-space($cardList)='-' or normalize-space($cardList)='!')">
      <xsl:variable name="line" select="count(parent::html:tr/preceding-sibling::html:tr)"/>
      <xsl:variable name="value" select="document('')/xsl:stylesheet/html:dl/html:dt[normalize-space(.)=normalize-space(substring-before(concat($cardList,','),','))]/following-sibling::html:dd[1]"/>
      <xsl:if test="$value">
        <card color="{document('')/xsl:stylesheet/html:ol[@class='colors']/html:li[position()=$line]/@id}" value="{$value}"/>
      </xsl:if>
      <xsl:call-template name="cards">
        <xsl:with-param name="cardList" select="substring-after($cardList,',')"/>
      </xsl:call-template>
  </xsl:if>
</xsl:template>


</xsl:stylesheet>