<?xml version="1.0"?>
<!--
$Id: entities.xsl,v 1.5 2008/04/13 10:36:18 dcarlis Exp $
Generate entity files for MathML
David Carlisle
-->

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

<xsl:param name="plane1hack" select="false()"/>
<xsl:param name="olddesc" select="false()"/>
<xsl:param name="mml2" select="doc('mml2.xml')"/>
<xsl:param name="xhtml1" select="doc('xhtml1.xml')"/>
<xsl:key name="mml2" match="char" use="@name"/>


<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.

     Copyright 1998 - 2008 W3C.

     Use and distribution of this code are permitted under the terms of the
     W3C Software Notice and License.
     http://www.w3.org/Consortium/Legal/copyright-software-19980720

     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 5.x.
</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>
<xsl:variable name="n" select="@name"/>

<xsl:text>&#10;&lt;!ENTITY % </xsl:text>
<xsl:value-of select="$f"/>
<xsl:text> PUBLIC "</xsl:text>
<xsl:value-of select="@fpi"/>
<xsl:text>" "</xsl:text>
<xsl:value-of select="$f"/>
<xsl:text>.ent"&gt;&#10;%</xsl:text>
<xsl:value-of select="$f"/>
<xsl:text>;</xsl:text>
<xsl:result-document 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') and replace(@name,'2003','1991')=../../group[@name='iso9573-1991']/set/@name">
    <xsl:value-of select="$copy1"/>
    <xsl:value-of select="translate($copy,'WQYZ','1991')"/>
  </xsl:when>
  <xsl:when test="starts-with(@name,'9573')">
    <xsl:value-of select="$copy1"/>
    <xsl:value-of select="translate($copy,'WQYZ','1986')"/>
  </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;!-- 

       <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 lang="en" select="@id"/>
    <xsl:sort lang="en" select="not(@optional)"/>
    <xsl:variable name="a" select="string(../@id)"/>
    <xsl:variable name="b" select="string(key('mml2',@id,$mml2)/@val)"/>
    <xsl:if test="not($a=$b) and $n=/unicode/entitygroups/group[@name='mathml']/set/@name">
      <xsl:message>mathml2: <xsl:value-of select="@id,$a,$b"/></xsl:message>
    </xsl:if>
    <xsl:variable name="ad" select="string(../@dec)"/>
    <xsl:variable name="c" select="string(key('mml2',@id,$xhtml1)/@dec)"/>
    <xsl:if test="not($ad=$c) and $n=/unicode/entitygroups/group[@name='xhtml1']/set/@name">
      <xsl:message>xhtml1: <xsl:value-of select="@id,$ad,$c"/></xsl:message>
    </xsl:if>
    <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:value-of select="replace(translate(../@id,'Ux',''),'-',';&amp;#x')"/>
    </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>
</xsl:result-document>

</xsl:template>


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

<xsl:template match="/">
  <xsl:apply-templates select="unicode/entitygroups/group[@name='2007']"/>
</xsl:template>


<xsl:template match="charlist"/>

<xsl:template match="group[@name='2007']">
<xsl:result-document method="text"  href="../2007/w3centities.ent">
<xsl:text>

&lt;!-- 
     Copyright 1998 - 2008 W3C.

     Use and distribution of this code are permitted under the terms of the
     W3C Software Notice and License.
     http://www.w3.org/Consortium/Legal/copyright-software-19980720

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

 
       Public identifier: -//W3C//ENTITIES Combined Set//EN//XML
       System identifier: http://www.w3.org/2003/entities/2007/w3centities.ent

     The public identifier should always be used verbatim.
     The system identifier may be changed to suit local requirements.

     Typical invocation:

       &lt;!ENTITY % w3centities PUBLIC
         "-//W3C//ENTITIES Combined Set//EN//XML"
         "http://www.w3.org/2003/entities/2007/w3centities.ent"
       >
       %w3centities;

-->

</xsl:text>

<xsl:apply-templates select="set">
</xsl:apply-templates>
</xsl:result-document>
</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>



</xsl:stylesheet>
