<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE stylesheet [
<!ENTITY brvbar  "&#166;" >
<!ENTITY mdash  "&#8212;" >
<!ENTITY lsaquo "&#8249;" >
<!ENTITY rsaquo "&#8250;" >
]>

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

  <xsl:template match="@*|node()">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
  </xsl:template>
  
  <xsl:template match="abbreviations">
    <h4>Abbreviations</h4>
    <table border="0">
      <xsl:for-each select="child::abr_def">
	<tr>
	  <td><xsl:apply-templates select="k"/></td>
	  <td><xsl:apply-templates select="v"/></td>
	</tr>
      </xsl:for-each>
   </table>
   <br/>
  </xsl:template>

  <xsl:template match="abr_def">
      <xsl:apply-templates select="@*|node()"/>
      <br/>
  </xsl:template>

  <xsl:template match="k">
    <span style="color:#0000ff">
      <xsl:apply-templates select="@*|node()"/>
    </span>
  </xsl:template>

  <xsl:template match="opt">
    <span style="color:#bbbbff">
      <xsl:value-of select="."/>
    </span>
  </xsl:template>

  <xsl:template match="v">
      <xsl:value-of select="."/>
  </xsl:template>

  <xsl:template match="kref">
    <span style="text-decoration:underline">
      <xsl:value-of select="."/>
    </span>
  </xsl:template>

  <xsl:template match="em">
    <em><xsl:value-of select="."/></em>
  </xsl:template>
  
  <xsl:template match="iref">
    <a>
      <xsl:attribute name="href">
        <xsl:value-of select="."/>
      </xsl:attribute>
      <xsl:value-of select="."/>
    </a>
  </xsl:template>

  <xsl:template match="full_name">
    <h2><xsl:apply-templates select="@*|node()"/></h2>
    <br/>
    <br/>
  </xsl:template>
  
  <xsl:template match="style">
    <span style="color:#aaaa00">
      <em>
	<xsl:value-of select="."/>
      </em>
    </span>
  </xsl:template>

  <xsl:template match="description">
    <h4>Description</h4>
    <xsl:apply-templates select="@*|node()"/>
    <br/>
    <br/>
  </xsl:template>
  
  <xsl:template match="head">
    <b>
      <big>
	<xsl:apply-templates select="@*|node()"/>
      </big>
    </b>
    <br/>
  </xsl:template>
  
  <xsl:template match="etym">
    <span style="color:#009999">
      Etymology: 
    </span>
    <xsl:apply-templates select="@*|node()"/>
    <br/>
  </xsl:template>
  
  <xsl:template match="idiom">
    <span style="color:#009999">
      Idioms: 
    </span>
    <br/>
    <xsl:apply-templates select="@*|node()"/>
    <br/>
  </xsl:template>
  
  <xsl:template match="proverb">
    <span style="color:#009999">
      Idioms: 
    </span>
    <br/>
    <xsl:apply-templates select="@*|node()"/>
    <br/>
  </xsl:template>
  
  <xsl:template match="synonym">
    <span style="color:#009999">
      Synonyms: 
    </span>
    <xsl:apply-templates select="@*|node()"/>
    <br/>
  </xsl:template>
  
  <xsl:template match="antonym">
    <span style="color:#009999">
      Antonyms: 
    </span>
    <xsl:apply-templates select="@*|node()"/>
    <br/>
  </xsl:template>
  
  <xsl:template match="usage">
    <span style="color:#009999">
      Usage: 
    </span>
    <xsl:apply-templates select="@*|node()"/>
    <br/>
  </xsl:template>
  
  <xsl:template match="tr">
    [<xsl:value-of select="."/>] 
    <xsl:if test="name(../..) = 'ar'">
      <br/>
    </xsl:if>
  </xsl:template>

  <xsl:template match="ex">
    <!--     &lt; -->
    &lsaquo;
    <span style="color:#888888">
      <xsl:apply-templates select="@*|node()"/>
    </span>
    &rsaquo;
    <!--     &gt; -->
  </xsl:template>

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

  <xsl:template match="co">
    {<xsl:apply-templates select="@*|node()"/>}
  </xsl:template>

  <xsl:template match="gender">
    <em>
      <span style="color:#009900">
	<xsl:apply-templates select="@*|node()"/>
      </span>
      <xsl:if test="name(..) = 'def'">
	<br/>
      </xsl:if>
    </em>
  </xsl:template>
  

  <xsl:template match="abr">
    <em>
      <span style="text-decoration:underline">      
	<xsl:value-of select="."/>
      </span>
    </em>
  </xsl:template>

  <xsl:template match="field">
    <span style="color:#aa8800">
      <em>
	<xsl:apply-templates select="@*|node()"/>
      </em>
    </span>
  </xsl:template>

  <xsl:template match="attribute::* | node()">
    <xsl:if test="name()!='m'">
      <xsl:copy>
	<xsl:apply-templates select="attribute::* | node()"/>
      </xsl:copy>
    </xsl:if>
  </xsl:template>


  <xsl:template match="m[not(ancestor::m)]">
    <br/>
    <table border="1" bordercolor="#CCDDAA" rules="rows" cellpadding="2">
      <!-- loop over the innermost m elements -->
      <xsl:for-each select="descendant-or-self::m[not(descendant::m)]">
	<tr>
	  <!-- loop over all m element ancestors -->
	  <xsl:for-each select="ancestor-or-self::m">
	    <!-- loop over attributes of this element -->
	    <xsl:for-each select="attribute::*">
	      <td bgcolor="#EEFFCC">
		<xsl:value-of select="."/>
	      </td>
	      <!-- insert an empty cell between attributes of the same m element -->
	      <xsl:if test="name()!=name(../@*[last()])">
		<td/>
	      </xsl:if>
	    </xsl:for-each>
	    <td>
	      <xsl:apply-templates select="attribute::* | node()"/>
	    </td>
	  </xsl:for-each>
	</tr>
      </xsl:for-each>
    </table>
  </xsl:template>


  <xsl:template match="def">
    <xsl:for-each select="attribute::*">
      <xsl:choose>
	<xsl:when test="name() = 'pos'">
	  <span style="color:#009900">
	    <em>
	      <xsl:value-of select="."/>
	    </em>
	  </span>
	  <xsl:if test="not(parent/ancestor::def)">
	    <br/>
	  </xsl:if>
	</xsl:when>
	<xsl:when test="name() = 'l'">
	  <b>
	    <xsl:value-of select="."/>:
	  </b>
	</xsl:when>
	<xsl:otherwise>
	  <em>
	    <span style="color:#009900">
	      <xsl:value-of select="."/> 
	    </span>
	  </em>
	  :
	</xsl:otherwise>
      </xsl:choose>
    </xsl:for-each>
    <xsl:apply-templates select="@*|node()"/>
    <xsl:if test="not(child::def) and not(@l) and following-sibling::def">
      <!--       <b> :</b> -->
      <b> &brvbar;</b>
    </xsl:if>
    <xsl:if test="not(ancestor::def[@l]) and @l">
      <br/>
    </xsl:if>
  </xsl:template>
  
  <xsl:template match="//quote/text()[normalize-space(.)][../*]">
    &quot;<xsl:value-of select="."/>&quot;
  </xsl:template>

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

  <xsl:template match="author">
    &mdash;<xsl:apply-templates select="@*|node()"/>
  </xsl:template>

  <xsl:template match="auxiliary">
    <em>
      <span style="color:#009900">
	<xsl:apply-templates select="@*|node()"/>
      </span>
    </em>
  </xsl:template>

  <xsl:template match="ar">
    <xsl:apply-templates select="@*|node()"/>
    <br/>
    <br/>
  </xsl:template>

  <xsl:template match="/xdxf">
    <![CDATA[<!DOCTYPE html
    PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">]]>
    <html>
      <head>
	<title>XDXF dictionary</title>
      </head>
      <body>
        <xsl:apply-templates select="full_name"/>
        <xsl:apply-templates select="description"/>
        <xsl:apply-templates select="abbreviations"/>
        <xsl:apply-templates select="ar"/>
      </body>
    </html>
  </xsl:template>
  
</xsl:stylesheet>
