<?xml version="1.0"?>
<!--
$Id: entities.xsl,v 1.2 2003/12/08 15:02:15 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:param name="plane1hack" select="false()"/>
<xsl:param name="olddesc" select="false()"/>

<xsl:strip-space elements="group"/>

<xsl:output
  method="text"
  />


<xsl:variable name="top"> produced by the XSL script entities.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="copy1">
     Entity names in this file are derived from files carrying the
     following notice:
</xsl:variable>

<xsl:variable name="copy">
     (C) International Organization for Standardization WQYZ
     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 match="set">
<xsl:variable name="d" select="../@name"/>
<xsl:variable name="f">
<xsl:choose>
<xsl:when test="starts-with(@name,'957')"><xsl:value-of select="substring(@name,11)"/></xsl:when>
<xsl:when test="starts-with(@name,'88')"><xsl:value-of select="substring-after(@name,'-')"/></xsl:when>
<xsl:when test="starts-with(@name,'html')"><xsl:value-of select="substring-after(@name,'-')"/></xsl:when>
<xsl:otherwise><xsl:value-of select="@name"/></xsl:otherwise>
</xsl:choose>
</xsl:variable>


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


  <xsl:choose>
  <xsl:when test="starts-with(@name,'9573-19')">
    <xsl:value-of select="$copy1"/>
    <xsl:value-of select="translate($copy,'WQYZ','1991')"/>
  </xsl:when>
  <xsl:when test="starts-with(@name,'9573-2003')">
    <xsl:value-of select="translate($copy,'WQYZ','2003')"/>
  </xsl:when>
  <xsl:when test="starts-with(@name,'9573-2004')">
    <xsl:value-of select="translate($copy,'WQYZ','2004')"/>
  </xsl:when>
  <xsl:when test="starts-with(@name,'8')">
    <xsl:value-of select="$copy1"/>
    <xsl:value-of select="translate($copy,'WQYZ','1986')"/>
  </xsl:when>
  </xsl:choose>
  <xsl:text>
-->
</xsl:text>

&lt;!-- 
     Version: &#36;Id:&#36;

       <xsl:if test="@fpi">Public identifier: <xsl:value-of select="@fpi"/>//XML</xsl:if>
       System identifier: http://www.w3.org/2003/entities/<xsl:value-of select="$d"/>/<xsl:value-of select="$f"/>.ent

     <xsl:if test="@fpi">The public identifier should always be used verbatim.
     </xsl:if>The system identifier may be changed to suit local requirements.

     Typical invocation:

       &lt;!ENTITY % <xsl:value-of select="$f"/><xsl:if test="@fpi"> PUBLIC
         "<xsl:value-of select="@fpi"/>//XML"</xsl:if><xsl:if test="not(@fpi)"> SYSTEM</xsl:if>
         "http://www.w3.org/2003/entities/<xsl:value-of select="$d"/>/<xsl:value-of select="$f"/>.ent"
       >
       %<xsl:value-of select="$f"/>;

<xsl:text>-->

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

</xsl:text>
  </xsl:if>
  <xsl:for-each select="key('set',@name)">
    <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="$plane1hack and 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[$olddesc]|../description[not($olddesc)]"/>
    <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:apply-templates select="entitygroups/group"/>

</xsl:template>

<xsl:template match="group[@name='predefined']"/>
<xsl:template match="group[@name='mathml']">
 <xsl:apply-templates select="set[starts-with(@name,'m')]"/>
</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>
