<?xml version="1.0"?>
<!--
$Id: entities.xsl,v 1.9 2003/09/17 14:49:06 davidc Exp $
Generate entity files for MathML
David Carlisle
-->

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:saxon="http://icl.com/saxon"  
                extension-element-prefixes="saxon"
                version="1.0">

<xsl:output
  method="text"
  />


<xsl:variable name="top"> produced by the XSL script characters.xsl
     from input data in unicode.xml.

     Please report any errors to David Carlisle
     via the public W3C list www-math@w3.org.

     The numeric character values assigned to each entity
     (should) match the Unicode assignments in Unicode 4.0.
</xsl:variable>

<xsl:variable name="vs1">
     References to the VARIANT SELECTOR 1 character (&amp;#x0FE00;)
     should match the uses listed in Unicode Technical Report 25.
</xsl:variable>

<xsl:variable name="copy">
     Entity names in this file are derived from files carrying the
     following notice:

     (C) International Organization for Standardization 19XY
     Permission to copy in any form is granted for use with
     conforming SGML systems and applications as defined in
     ISO 8879, provided this notice is included in all copies.
</xsl:variable>

<xsl:template name="alphadecl">
<xsl:param name="set"/>
<xsl:param name="ignore" select="false()"/>
<xsl:variable name="d">
<xsl:choose>
<xsl:when test="starts-with($set,'957')">iso9573-13</xsl:when>
<xsl:when test="starts-with($set,'88')">iso8879</xsl:when>
<xsl:when test="starts-with($set,'mml')">mathml</xsl:when>
<xsl:when test="starts-with($set,'htm')">html</xsl:when>
</xsl:choose>
</xsl:variable>
<xsl:variable name="f">
<xsl:choose>
<xsl:when test="starts-with($set,'957')"><xsl:value-of select="substring-after($set,'13-')"/></xsl:when>
<xsl:when test="starts-with($set,'88')"><xsl:value-of select="substring-after($set,'-')"/></xsl:when>
<xsl:when test="starts-with($set,'html')"><xsl:value-of select="substring-after($set,'-')"/></xsl:when>
<xsl:otherwise><xsl:value-of select="$set"/></xsl:otherwise>
</xsl:choose>.ent</xsl:variable>


<saxon:output method="text"  href="../DTD/mathml2/{$d}/{$f}">
  <xsl:text>
&lt;!--
     File </xsl:text>
  <xsl:value-of select="$f"/>
  <xsl:copy-of select="$top"/>
  <xsl:if test="key('set',$set)/../@id[contains(.,'-0FE00')]">
   <xsl:copy-of select="$vs1"/>
  </xsl:if>

  <xsl:choose>
  <xsl:when test="starts-with($set,'9')">
    <xsl:value-of select="translate($copy,'XY','91')"/>
  </xsl:when>
  <xsl:when test="starts-with($set,'8')">
    <xsl:value-of select="translate($copy,'XY','86')"/>
  </xsl:when>
  </xsl:choose>
  <xsl:text>
-->

</xsl:text>
  <xsl:if test="key('set',$set)/../@id[starts-with(.,'U1D')]">
   <xsl:text>&lt;!ENTITY % plane1D  "&amp;#38;#38;#x1D">

</xsl:text>
  </xsl:if>
  <xsl:for-each select="key('set',$set)">
    <xsl:sort select="@id"/>
    <xsl:sort select="not(@optional)"/>
    <xsl:if test="@optional">
      <xsl:text/>&lt;!ENTITY % <xsl:value-of select="@optional"/> "<xsl:value-of select="@default"/>"&gt;&#10;<xsl:text/>
      <xsl:text/>&lt;![%<xsl:value-of select="@optional"/>;[<xsl:text/>
    </xsl:if>
    <xsl:if test="../description/@unicode='none'">&lt;!--</xsl:if>
    <xsl:text>&lt;!ENTITY </xsl:text>
    <xsl:value-of  select="@id"/>
    <xsl:value-of select="substring('               ',string-length(@id))"/>
    <xsl:text> "</xsl:text>
    <xsl:choose>
    <xsl:when test="60 = ../@dec or 38 = ../@dec">
      <xsl:text>&amp;#38;#</xsl:text><xsl:value-of select="../@dec"/>
    </xsl:when>
    <xsl:when test="starts-with(../@id,'U1D')">
      <xsl:text>%plane1D;</xsl:text><xsl:value-of select="substring(../@id,4)"/>
    </xsl:when>
    <xsl:when test="starts-with(../description,'COMBINING')">
      <xsl:text> &amp;#x</xsl:text><xsl:value-of select="substring(../@id,2)"/>
    </xsl:when>
    <xsl:otherwise>
      <xsl:text/>&amp;<xsl:if test="starts-with(../@dec,'60-')">#38;</xsl:if>#x<xsl:text/>
      <xsl:call-template name="string-replace">
        <xsl:with-param name="string" select="translate(../@id,'Ux','')"/>
        <xsl:with-param name="from" select="'-'"/>
        <xsl:with-param name="to" select="';&amp;#x'"/>
      </xsl:call-template>
    </xsl:otherwise>
    </xsl:choose>
    <xsl:text>;" &gt;</xsl:text>
    <xsl:if test="../description/@unicode='none'"> --&gt;</xsl:if>
    <xsl:text>&lt;!--</xsl:text>
    <xsl:value-of select="desc"/>
    <xsl:if test="../description/@unicode='provisional'"> --&gt;&lt;!-- (Provisional)</xsl:if>
    <xsl:text> --&gt;</xsl:text>
    <xsl:if test="@optional">
      <xsl:text>]]&gt;</xsl:text>
    </xsl:if>
    <xsl:text>&#xA;</xsl:text>
 
  </xsl:for-each>
</saxon:output>

</xsl:template>


<xsl:key name="set" match="entity" use="@set"/>

<xsl:template match="charlist">



  <xsl:call-template name="alphadecl">
    <xsl:with-param name="set" select="'9573-13-isoamsa'"/>
  </xsl:call-template>
  <xsl:call-template name="alphadecl">
    <xsl:with-param name="set" select="'9573-13-isoamsb'"/>
  </xsl:call-template>
  <xsl:call-template name="alphadecl">
    <xsl:with-param name="set" select="'9573-13-isoamsc'"/>
  </xsl:call-template>
  <xsl:call-template name="alphadecl">
    <xsl:with-param name="set" select="'9573-13-isoamsn'"/>
  </xsl:call-template>
  <xsl:call-template name="alphadecl">
    <xsl:with-param name="set" select="'9573-13-isoamso'"/>
  </xsl:call-template>
  <xsl:call-template name="alphadecl">
    <xsl:with-param name="set" select="'9573-13-isoamsr'"/>
  </xsl:call-template>
  <xsl:call-template name="alphadecl">
    <xsl:with-param name="set" select="'9573-13-isogrk3'"/>
  </xsl:call-template>
  <xsl:call-template name="alphadecl">
    <xsl:with-param name="set" select="'9573-13-isogrk4'"/>
  </xsl:call-template>
  <xsl:call-template name="alphadecl">
    <xsl:with-param name="set" select="'9573-13-isomfrk'"/>
  </xsl:call-template>
  <xsl:call-template name="alphadecl">
    <xsl:with-param name="set" select="'9573-13-isomopf'"/>
  </xsl:call-template>
  <xsl:call-template name="alphadecl">
    <xsl:with-param name="set" select="'9573-13-isomscr'"/>
  </xsl:call-template>
  <xsl:call-template name="alphadecl">
    <xsl:with-param name="set" select="'9573-13-isotech'"/>
  </xsl:call-template>

  <xsl:call-template name="alphadecl">
    <xsl:with-param name="set" select="'8879-isoamsa'"/>
  </xsl:call-template>
  <xsl:call-template name="alphadecl">
    <xsl:with-param name="set" select="'8879-isoamsb'"/>
  </xsl:call-template>
  <xsl:call-template name="alphadecl">
    <xsl:with-param name="set" select="'8879-isoamsc'"/>
  </xsl:call-template>
  <xsl:call-template name="alphadecl">
    <xsl:with-param name="set" select="'8879-isoamsn'"/>
  </xsl:call-template>
  <xsl:call-template name="alphadecl">
    <xsl:with-param name="set" select="'8879-isoamso'"/>
  </xsl:call-template>
  <xsl:call-template name="alphadecl">
    <xsl:with-param name="set" select="'8879-isoamsr'"/>
  </xsl:call-template>
  <xsl:call-template name="alphadecl">
    <xsl:with-param name="set" select="'8879-isogrk1'"/>
  </xsl:call-template>
  <xsl:call-template name="alphadecl">
    <xsl:with-param name="set" select="'8879-isogrk2'"/>
  </xsl:call-template>
  <xsl:call-template name="alphadecl">
    <xsl:with-param name="set" select="'8879-isogrk3'"/>
  </xsl:call-template>
  <xsl:call-template name="alphadecl">
    <xsl:with-param name="set" select="'8879-isogrk4'"/>
  </xsl:call-template>
  <xsl:call-template name="alphadecl">
    <xsl:with-param name="set" select="'8879-isotech'"/>
  </xsl:call-template>
  <xsl:call-template name="alphadecl">
    <xsl:with-param name="set" select="'8879-isocyr1'"/>
  </xsl:call-template>
  <xsl:call-template name="alphadecl">
    <xsl:with-param name="set" select="'8879-isocyr2'"/>
  </xsl:call-template>
  <xsl:call-template name="alphadecl">
    <xsl:with-param name="set" select="'8879-isolat1'"/>
  </xsl:call-template>
  <xsl:call-template name="alphadecl">
    <xsl:with-param name="set" select="'8879-isolat2'"/>
  </xsl:call-template>
  <xsl:call-template name="alphadecl">
    <xsl:with-param name="set" select="'8879-isobox'"/>
  </xsl:call-template>
  <xsl:call-template name="alphadecl">
    <xsl:with-param name="set" select="'8879-isodia'"/>
  </xsl:call-template>
  <xsl:call-template name="alphadecl">
    <xsl:with-param name="set" select="'8879-isonum'"/>
  </xsl:call-template>
  <xsl:call-template name="alphadecl">
    <xsl:with-param name="set" select="'8879-isopub'"/>
  </xsl:call-template>


  <xsl:call-template name="alphadecl">
    <xsl:with-param name="set" select="'mmlextra'"/>
  </xsl:call-template>
  <xsl:call-template name="alphadecl">
    <xsl:with-param name="set" select="'mmlalias'"/>
  </xsl:call-template>


  <xsl:call-template name="alphadecl">
    <xsl:with-param name="set" select="'html4-lat1'"/>
  </xsl:call-template>
  <xsl:call-template name="alphadecl">
    <xsl:with-param name="set" select="'html4-symbol'"/>
  </xsl:call-template>
  <xsl:call-template name="alphadecl">
    <xsl:with-param name="set" select="'html4-special'"/>
    <xsl:with-param name="ignore" select="'html4circ'"/>
  </xsl:call-template>


</xsl:template>



<!-- replace all occurences of the character(s) `from'
     by the string `to' in the string `string'.-->
<xsl:template name="string-replace" >
  <xsl:param name="string"/>
  <xsl:param name="from"/>
  <xsl:param name="to"/>
  <xsl:choose>
    <xsl:when test="contains($string,$from)">
      <xsl:value-of select="substring-before($string,$from)"/>
      <xsl:value-of select="$to"/>
      <xsl:call-template name="string-replace">
      <xsl:with-param name="string" select="substring-after($string,$from)"/>
      <xsl:with-param name="from" select="$from"/>
      <xsl:with-param name="to" select="$to"/>
      </xsl:call-template>
    </xsl:when>
    <xsl:otherwise>
      <xsl:value-of select="$string"/>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>

</xsl:stylesheet>
