
<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:variable name="u"  select="document('unicode.xml')"/>
<xsl:variable name="stix"  select="document('stix-tbl.xml')"/>
<xsl:variable name="n"  select="document('stixdiff.xml')"/>
<xsl:variable name="dbk"  select="document('dbk.xml')"/>
<xsl:variable name="xhtml"  select="document('xhtml1.xml')"/>
<xsl:variable name="doc"  select="/"/>


<xsl:key name="d" match="character" use="@dec"/>
<xsl:key name="m" match="character/entity[starts-with(@set,'9573-2003')]" use="@id"/>

<xsl:key name="s" match="data[@entity][@entityset]" use="@entity"/>

<xsl:key name="set" match="entity" use="@set"/>
<xsl:variable name="sets"
  select="$u/charlist/entitygroups/group[@name='iso9573-2003'][1]/set"/>


<xsl:template name="uplus">
<xsl:param name="u"/>
<xsl:choose>
<xsl:when test="normalize-space($u)=''"/>
<xsl:when test="starts-with($u,'U0') and contains($u,'-0')"
  >&lt;<xsl:value-of select="substring($u,3,4)"
   />,<xsl:value-of select="substring($u,9,4)"/>&gt;<xsl:text/>
</xsl:when>
<xsl:when test="starts-with($u,'U0')"
  >U+<xsl:value-of select="substring($u,3)"/>
</xsl:when>
<xsl:when test="starts-with($u,'U1')"
  >U+1<xsl:value-of select="substring($u,3)"/>
</xsl:when>
<xsl:otherwise><xsl:message>!!!BAD UNICODE: [<xsl:value-of select="$u"/>]</xsl:message></xsl:otherwise>
</xsl:choose>
</xsl:template>

<xsl:template mode="doc" match="stixdiff">
<xsl:for-each select="$stix/stix/data[@entity][@entityset]">
<xsl:sort select="@entityset"/>
<xsl:sort select="@entity"/>
<xsl:if test="(@mathmlid and generate-id()=generate-id(key('s',@entity)[@mathmlid][1]))
or not(key('s',@entity)/@mathmlid)">
<xsl:variable name="x" select="."/>
<xsl:for-each select="$u">
<xsl:if test="not(id($x/@mathmlid)/entity[starts-with(@set,'9573-2003')]/@id=$x/@entity)">
Entity: [<xsl:value-of select="$x/@entity"/>][<xsl:value-of select="$x/@entityset"/>]
   MathML [<xsl:call-template
name="uplus">
<xsl:with-param name="u" select="key('m',$x/@entity)/../@id"/>
</xsl:call-template>][<xsl:value-of select="key('m',$x/@entity)/../description"/>]
   Stix   [<xsl:call-template
name="uplus">
<xsl:with-param name="u" select="$x/@mathmlid"/>
</xsl:call-template>][<xsl:value-of
   select="$x/@mathmlid/description"/>]<xsl:text/>
   <xsl:choose>
   <xsl:when test="string($x/@mathmlid) and $n/stix/note[contains(@ref,$x/@mathmlid)]"
   ><xsl:value-of
   select="$n/stix/note[contains(@ref,$x/@mathmlid)]"/>
   </xsl:when><xsl:when
    test="not(key('m',$x/@entity))">
Not in MathML data
   </xsl:when>
    <xsl:otherwise>
   <xsl:text>&#10;</xsl:text>
   </xsl:otherwise>
   </xsl:choose>

</xsl:if>
</xsl:for-each>
</xsl:if>
</xsl:for-each>
</xsl:template>


<xsl:template mode="doc" match="docbookdiff">
<xsl:for-each select="$dbk/entities/e">
<xsl:variable name="x" select="."/>
<xsl:for-each select="$u">
<xsl:if test="not($x/@u=key('m',$x/@name)/../@id) and $x/@name != 'lt'
and $x/@name != 'amp'">
<xsl:text>&#10;</xsl:text>
<xsl:value-of select="$x/@name"/>
  dkb=[<xsl:call-template name="uplus">
  <xsl:with-param name="u" select="$x/@u"/>
   </xsl:call-template>][<xsl:value-of select="id($x/@u)/description"/>]
  mml=[<xsl:call-template name="uplus">
  <xsl:with-param name="u" select="key('m',$x/@name)/../@id"/>
  </xsl:call-template>][<xsl:value-of select="key('m',$x/@name)/../description"/>]</xsl:if>
</xsl:for-each>
</xsl:for-each>
</xsl:template>


<xsl:template mode="doc" match="xhtmldiff">
<xsl:for-each select="$xhtml/entities/e">
<xsl:variable name="x" select="."/>
<xsl:for-each select="$u">
<xsl:if test="not($x/@d=key('m',$x/@name)/../@dec) and $x/@name != 'lt'
and $x/@name != 'amp'">
<xsl:text>&#10;</xsl:text>
<xsl:value-of select="$x/@name"/>
  xht=[<xsl:call-template name="uplus">
  <xsl:with-param name="u"  select="key('d',$x/@d)/@id"/>
  </xsl:call-template>][<xsl:value-of select="key('d',$x/@d)/description"/>]
  mml=[<xsl:call-template name="uplus">
  <xsl:with-param name="u" select="key('m',$x/@name)/../@id"/>
   </xsl:call-template>][<xsl:value-of select="key('m',$x/@name)/../description"/>]</xsl:if>
</xsl:for-each>
</xsl:for-each>
</xsl:template>


<xsl:template mode="doc" match="duplicates">
<xsl:for-each select="$u">
<xsl:for-each select="key('set',$sets/@name)/..">
<xsl:variable name="n" select="entity[contains(@set,'9573-2003')][1]/@id"/>
<xsl:if test="entity[contains(@set,'9573-2003')]/@id !=$n">
<xsl:text>&#10;</xsl:text>
<xsl:call-template name="uplus">
  <xsl:with-param name="u"  select="@id"/>
</xsl:call-template>
<xsl:for-each select="entity[contains(@set,'9573-2003')]">
<xsl:text/>: <xsl:value-of select="@id"/>(<xsl:value-of select="@set"/>)<xsl:text/>
</xsl:for-each>
</xsl:if>
</xsl:for-each>
</xsl:for-each>
</xsl:template>

<xsl:template match="combining" mode="doc">
<xsl:for-each select="$u">
<xsl:for-each
select="key('set',$sets/@name)[../description[starts-with(.,'COMBINING')]]">
<xsl:call-template name="uplus">
  <xsl:with-param name="u" select="../@id"
/>
</xsl:call-template>: <xsl:value-of select="@id"/> [<xsl:value-of select="../description"/>]
</xsl:for-each>
</xsl:for-each>
</xsl:template>


<xsl:template match="*" mode="doc">
<xsl:message>!!!!<xsl:value-of select="name()"/></xsl:message>
</xsl:template>

</xsl:stylesheet>
