<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE xsl:stylesheet
          [
          <!ENTITY cr "&#xD;"><!ENTITY lf "&#xA;"><!ENTITY sp "&#xA0;"><!ENTITY br "&lf;">
          <!ENTITY nl "<xsl:text xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>&br;</xsl:text>">
          ]>
<xsl:stylesheet version="1.0"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:xhtml="http://www.w3.org/1999/xhtml"
                xmlns:xlink="http://www.w3.org/1999/xlink"
                xmlns:TeXML="http://www.unterstein.net/ML/TeXML"
                xmlns:ent="urn:entitags:entity-as-empty-element-tag"
                >


<xsl:output method="text"
            media-type="text/plain"
            encoding="iso-8859-1" />  <!-- iso-8859-1 or utf-8 or windows-1250 ? -->
<xsl:preserve-space elements="*" />
<xsl:strip-space elements="xhtml:tr"/>

<xsl:template name="XSLTproc">
    <xsl:text>%% XML v</xsl:text><xsl:value-of select="system-property('xsl:version')" />&nl;
    <xsl:text>%% XSLTransformed by &br;</xsl:text>
    <xsl:text>%%   </xsl:text><xsl:value-of select="system-property('xsl:vendor')" />&nl;
    <xsl:text>%%   </xsl:text><xsl:value-of select="system-property('xsl:vendor-url')" />&nl;
</xsl:template>

<xsl:template name="cr"><xsl:text>&#xD;</xsl:text></xsl:template>
<xsl:template name="lf"><xsl:text>&#xA;</xsl:text></xsl:template>
<xsl:template name="sp"><xsl:text>&#xA0;</xsl:text></xsl:template>
<xsl:template name="ws"><xsl:text>&#x20;</xsl:text></xsl:template>
<xsl:template name="nl"><xsl:text>&#xA;</xsl:text></xsl:template>


<!--  ================================================  -->
<!--  ================================================

NameToken-Style 'foobar'
<xsl:template match="processing-instruction('TeXML')[.='foobar']" />

Attribute-Style 'foobar="value"'
<xsl:template match="processing-instruction('TeXML')[contains(.,'foobar=&quot;')]">
	<xsl:value-of select="substring-before(substring-after(.,'foobar=&quot;'),'&quot;')" />
	or
	<xsl:variable name="foobar" select="substring-before(substring-after(.,'foobar=&quot;'),'&quot;')" />
	<xsl:if test="$foobar='value'"></xsl:if>
</xsl:template>

      ================================================  -->
<!--  ================================================  -->


<xsl:template match="/">
    <xsl:apply-templates />
</xsl:template>

<xsl:template match="TeXML:TeXML|TeXML">
    <xsl:text>&br;&br;</xsl:text>
    <xsl:text>%%&br;</xsl:text>
        <xsl:call-template name="XSLTproc" />
    <xsl:text>%%&br;</xsl:text>
    <xsl:text>%% TeXML dtd/fpi: &quot;-//StUs//DTD TeXML//EN&quot;&br;</xsl:text>
    <xsl:text>%% TeXML dtd/uri: &quot;http://www.unterstein.net/ML/TeXML.dtd&quot;&br;</xsl:text>
    <xsl:text>%% TeXML dtd/mod: &quot;http://www.unterstein.net/ML/entitags.mod&quot;&br;</xsl:text>
    <xsl:text>%% TeXML    xslt: &quot;http://www.unterstein.net/ML/TeXML2TeX.xsl&quot;&br;</xsl:text>
    <xsl:text>%% TeXML entitag: &quot;http://www.unterstein.net/ML/entitags.xml&quot;&br;</xsl:text>
    <xsl:text>%% TeXML   xmlns: &quot;http://www.unterstein.net/ML/TeXML&quot;&br;</xsl:text>
    <xsl:text>%%&br;</xsl:text>
        <xsl:apply-templates />
    <xsl:text>%%&br;</xsl:text>
    <xsl:text>%% \endinput &lt;/TeXML&gt;&br;</xsl:text>
    <xsl:text>%%&br;</xsl:text>
</xsl:template>

<xsl:template match="xhtml:*" />
<xsl:template match="*[@type='xml'] | *[@role='xml'] | *[@TeXML:type='xml'] | *[@TeXML:role='xml']" priority="1" />
<!--  this template has precedence in case of ambiguous rules
      i.e. never typeout any type="xml"
      e.g. <xhtml:a type="xml" id="label"/>
           matches both "*[@type='xml']" and "xhtml:a[not(@href)]"
  -->

<!--  ================================================  -->
<!--  ================================================  -->

<xsl:template match="TeXML:TeX|TeX | processing-instruction('TeX')"> 
    <xsl:value-of select="." />
</xsl:template> <!--  PI 2005-03-28  -->

<xsl:template match="TeXML:cmd|cmd">
    <xsl:text>\</xsl:text><xsl:value-of select="@name" />
                          <xsl:value-of select="@args" />
    <xsl:apply-templates />
</xsl:template>
<xsl:template match="TeXML:cmd[not(child::node()) and not(@args) and following-sibling::node()[1][self::text()]]">
    <xsl:text>\</xsl:text><xsl:value-of select="@name" />
	<xsl:text> </xsl:text>
</xsl:template> <!--  WS 2005-04-18 (empty) command+space (if ff.) text  -->

<xsl:template match="TeXML:env|env">
    <xsl:variable name="name">
        <xsl:if test="not(@bg)"><xsl:text>{</xsl:text></xsl:if><xsl:value-of select="@bg" />
        <xsl:value-of select="@name" />
        <xsl:if test="not(@eg)"><xsl:text>}</xsl:text></xsl:if><xsl:value-of select="@eg" />
    </xsl:variable>
    <xsl:if test="not(@bc)"><xsl:text>\begin</xsl:text></xsl:if><xsl:value-of select="@bc" />
    <xsl:value-of select="$name" />
    <xsl:value-of select="@args" />
        <xsl:apply-templates />
    <xsl:if test="not(@ec)"><xsl:text>\end</xsl:text></xsl:if><xsl:value-of select="@ec" />
    <xsl:value-of select="$name" />
</xsl:template> <!--  (bc/ec = begincommand/endcommand  bg/eg = begingroup/endgroup)  -->

<!--  ================================================  -->
<!--  ================================================  -->

<xsl:template match="TeXML:opt|opt">
        <xsl:if test="not(@bg)">
            <xsl:text>[</xsl:text>    <!--  default in TeXML.dtd  -->
        </xsl:if>
    <xsl:value-of select="@bg" />
    <xsl:apply-templates />
        <xsl:if test="not(@eg)">
            <xsl:text>]</xsl:text>    <!--  default in TeXML.dtd  -->
        </xsl:if>
    <xsl:value-of select="@eg" />
</xsl:template>

<xsl:template match="TeXML:arg|arg">
        <xsl:if test="not(@bg)">
            <xsl:text>{</xsl:text>    <!--  default in TeXML.dtd  -->
        </xsl:if>
    <xsl:value-of select="@bg" />
    <xsl:apply-templates />
        <xsl:if test="not(@eg)">
            <xsl:text>}</xsl:text>    <!--  default in TeXML.dtd  -->
        </xsl:if>
    <xsl:value-of select="@eg" />
</xsl:template>

<xsl:template match="TeXML:grp|grp">
        <xsl:if test="not(@bg)">
            <xsl:text>{</xsl:text>    <!--  default in TeXML.dtd  -->
        </xsl:if>
    <xsl:value-of select="@bg" />
    <xsl:apply-templates />
        <xsl:if test="not(@eg)">
            <xsl:text>}</xsl:text>    <!--  default in TeXML.dtd  -->
        </xsl:if>
    <xsl:value-of select="@eg" />
</xsl:template>

<!--  ================================================  -->
<!--  ================================================  -->

<xsl:template name="texnames">
    <xsl:param name="tn" />
    <xsl:choose>
        <xsl:when test="contains($tn,' ')">
            <xsl:text>\</xsl:text><xsl:value-of select="substring-before($tn,' ')" />
            <xsl:call-template name="texnames">
                <xsl:with-param name="tn" select="substring-after($tn,' ')" />
            </xsl:call-template>
        </xsl:when>
        <xsl:otherwise>
            <xsl:text>\</xsl:text><xsl:value-of select="$tn" />
        </xsl:otherwise>
    </xsl:choose>
</xsl:template>

<xsl:template match="TeXML:grp[@name]">
        <xsl:if test="not(@bg)">
            <xsl:text>{</xsl:text>    <!--  default in TeXML.dtd  -->
        </xsl:if>
    <xsl:value-of select="@bg" />
    <xsl:call-template name="texnames">
        <xsl:with-param name="tn" select="normalize-space(@name)" />
    </xsl:call-template>
    <xsl:if test="child::node()[1][self::text()]"><xsl:text> </xsl:text></xsl:if> <!--  commands space contents  -->
    <xsl:apply-templates />
        <xsl:if test="not(@eg)">
            <xsl:text>}</xsl:text>    <!--  default in TeXML.dtd  -->
        </xsl:if>
    <xsl:value-of select="@eg" />
</xsl:template>

<!--  ================================================  -->
<!--  ================================================  -->

<xsl:template match="TeXML:act|act">
    <xsl:value-of select="@on" />
</xsl:template>

<xsl:template match="TeXML:par|par | TeXML:x|x">
    <xsl:apply-templates />
    &nl;
</xsl:template>

<!--  ================================================  -->
<!--  ================================================  -->
<!--  ================================================  -->
<!--  ================================================  -->
<!--                 Linking & Refering                 -->

<xsl:template name="ifidlabel">
    <xsl:if test="@id">
        <xsl:text>\label{</xsl:text>
        <xsl:value-of select="@id" />
        <xsl:text>}</xsl:text>
    </xsl:if>
</xsl:template>

<xsl:template name="ifidhyper">
    <xsl:if test="@id">
        <xsl:text>\hypertarget{</xsl:text>
            <xsl:value-of select="@id" />
        <xsl:text>}{</xsl:text>
            <xsl:apply-templates />
        <xsl:text>}</xsl:text>
    </xsl:if>
</xsl:template>

<xsl:template name="xlinkhref">
    <xsl:if test="@xlink:href">
        <xsl:text>{</xsl:text>
            <xsl:choose>
                <xsl:when test="starts-with(@xlink:href,'#')">
                    <xsl:value-of select="substring-after(@xlink:href,'#')" />
                </xsl:when>
                <xsl:otherwise>
                    <xsl:value-of select="@xlink:href" />
                </xsl:otherwise>
            </xsl:choose>
        <xsl:text>}</xsl:text>
    </xsl:if>
</xsl:template>

<!--  ================================================  -->
<!--  ================================================  -->

<xsl:template match="xhtml:a">
    <xsl:call-template name="ifidlabel" />
    <xsl:choose>
        <xsl:when test="starts-with(@href,'#')">
            <xsl:text>\hyperlink{</xsl:text>
            <xsl:value-of select="substring-after(@href,'#')" />
        </xsl:when>
        <xsl:otherwise>
            <xsl:text>\href{</xsl:text>
            <xsl:value-of select="@href" />
        </xsl:otherwise>
    </xsl:choose>
    <xsl:text>}{</xsl:text>
        <xsl:apply-templates />
    <xsl:text>}</xsl:text>
</xsl:template>

<xsl:template match="xhtml:a[not(@href)]">
    <xsl:choose>
        <xsl:when test="*|text()">
            <xsl:call-template name="ifidhyper" />
        </xsl:when>
        <xsl:otherwise>
            <xsl:call-template name="ifidlabel" />
        </xsl:otherwise>
    </xsl:choose>
</xsl:template>

<xsl:template match="xhtml:img">
    <xsl:text>\hyperimg{</xsl:text>
        <xsl:value-of select="@src" />
    <xsl:text>}</xsl:text>
    <xsl:if test="@width">
        <xsl:text> % wd:</xsl:text><xsl:value-of select="@width" /><xsl:text>px</xsl:text>
    </xsl:if>
    <xsl:if test="@height">
        <xsl:text> % ht:</xsl:text><xsl:value-of select="@height" /><xsl:text>px</xsl:text>
    </xsl:if>
</xsl:template>

<xsl:template match="TeXML:cmd[@name='href'] | TeXML:cmd[@name='hyperlink'] | TeXML:cmd[@name='hyperimg']">
    <xsl:text>\</xsl:text><xsl:value-of select="@name" />
                          <xsl:value-of select="@args" />
    <xsl:call-template name="xlinkhref" />
    <xsl:apply-templates />
</xsl:template>
<!--  this template processes the commands \href \hyperlink \hyperimg
      i.e.
      e.g. <cmd name="href"
                xlink:type="simple"
                xlink:show="replace"
                xlink:actuate="onRequest"
                xlink:href="URI"><arg>anchor-text</arg></cmd>
      e.g. <cmd name="hyperimg"
                xlink:type="simple"
                xlink:show="embed"
                xlink:actuate="onLoad"
                xlink:href="SRC" />
      all <cmd> <env> <x> elements can be xlinks, but the attributes are ignored for TeX
  -->

<xsl:template match="TeXML:cmd[@name='label' and not(@args)] | TeXML:cmd[@name='hypertarget' and not(@args)]">
    <xsl:text>\</xsl:text><xsl:value-of select="@name" />
    <xsl:text>{</xsl:text><xsl:value-of select="@id"   /><xsl:text>}</xsl:text>
    <xsl:apply-templates />
</xsl:template>
<!--  this template processes the commands \hypertarget \label
      i.e.
      e.g. <cmd name="hypertarget"
                id="id"><arg>anchor-text</arg></cmd>
      e.g. <cmd name="label" id="id" />
      not: <cmd name="label" args="texmark" id="id" />
      all other <cmd> <env> <x> elements can have ids, but they are ignored for TeX
      beware: xml-id-conventions are much more restrictive than TeX-label-conventions
      beware: any args-attribute (a TeX-label) has precedence over an id-attribute
  -->
<xsl:template match="TeXML:cmd[@name='ref' and not(@args)] | TeXML:cmd[@name='pageref' and not(@args)]">
    <xsl:text>\</xsl:text><xsl:value-of select="@name" />
    <xsl:text>{</xsl:text><xsl:value-of select="@to"   /><xsl:text>}</xsl:text>
    <xsl:apply-templates />
</xsl:template>

<xsl:template match="TeXML:cmd[@name='hyperref' and not(@args)]">
    <xsl:text>\</xsl:text><xsl:value-of select="@name" />
    <xsl:if test="@to">
        <xsl:text>[</xsl:text><xsl:value-of select="@to"   /><xsl:text>]</xsl:text>
    </xsl:if>
    <xsl:apply-templates />
</xsl:template>

<xsl:template match="TeXML:ANY|ANY | xlink:*">
    <xsl:apply-templates />
</xsl:template>

<!--  ================================================  -->
<!--  ================================================  -->

<xsl:template match="TeXML:env[@name='tabular']">      <!--  xhtml:table ignored ! -->
    <xsl:variable name="name">
        <xsl:if test="not(@bg)"><xsl:text>{</xsl:text></xsl:if><xsl:value-of select="@bg" />
        <xsl:value-of select="@name" />
        <xsl:if test="not(@eg)"><xsl:text>}</xsl:text></xsl:if><xsl:value-of select="@eg" />
    </xsl:variable>
    <xsl:if test="not(@bc)"><xsl:text>\begin</xsl:text></xsl:if><xsl:value-of select="@bc" />
    <xsl:value-of select="$name" />
    <xsl:value-of select="@args" />
    <xsl:choose> <!--  to change xhtml-order thead|tfoot|tbody  -->
        <xsl:when test="child::xhtml:thead or child::xhtml:tbody or child::xhtml:tfoot">
            <xsl:apply-templates select="child::xhtml:thead" />
            <xsl:apply-templates select="child::xhtml:tbody" />
            <xsl:apply-templates select="child::xhtml:tfoot" />
        </xsl:when>
        <xsl:otherwise>
            <xsl:apply-templates />
        </xsl:otherwise>
    </xsl:choose>
    <xsl:if test="not(@ec)"><xsl:text>\end</xsl:text></xsl:if><xsl:value-of select="@ec" />
    <xsl:value-of select="$name" />
</xsl:template>

<xsl:template match="xhtml:thead | xhtml:tbody | xhtml:tfoot">
    <xsl:apply-templates />
</xsl:template>

<xsl:template match="xhtml:tr">
    <xsl:apply-templates />
    <xsl:text> \\</xsl:text>
</xsl:template>

<xsl:template match="xhtml:td | xhtml:th">
    <xsl:if test="self::xhtml:th"><xsl:text>\bfseries </xsl:text></xsl:if>
    <xsl:apply-templates />
    <xsl:if test="following-sibling::xhtml:td or following-sibling::xhtml:th">
        <xsl:text> &amp; </xsl:text>
    </xsl:if>
</xsl:template>

<xsl:template match="xhtml:td[@colspan] | xhtml:th[@colspan]">
    <xsl:text>\multicolumn{</xsl:text>
        <xsl:value-of select="@colspan" />
    <xsl:text>}{</xsl:text>
    <xsl:choose>
        <xsl:when test="@align">
            <xsl:value-of select="substring(@align,1,1)" />
        </xsl:when>
        <xsl:otherwise>
            <xsl:text>c</xsl:text>
        </xsl:otherwise>
    </xsl:choose>
    <xsl:text>}{</xsl:text>
    <xsl:if test="self::xhtml:th"><xsl:text>\bfseries </xsl:text></xsl:if>
    <xsl:apply-templates />
    <xsl:text>}</xsl:text>
    <xsl:if test="following-sibling::xhtml:td or following-sibling::xhtml:th">
        <xsl:text> &amp; </xsl:text>
    </xsl:if>
</xsl:template>


<!--  ================================================  -->
<!--  ================================================  -->
<!--  entitags.xml 2003-12-21                           -->


<xsl:template match="ent:*">
    <xsl:variable name="TagName" select="local-name()" />
    <xsl:value-of select="document('entitags.xml')//ent:el[@tagname=$TagName]/@texcode" />
</xsl:template>

<xsl:template match="ent:*[@texcode]"><xsl:value-of select="@texcode" /></xsl:template>


<!--  entitags.xml 2003-12-21                           -->
<!--  ================================================  -->
<!--  ================================================  -->
<!--  ================================================  -->
<!--  IBM's or GetFO's TeXML 2005-04-14 (incomplete)    -->

<xsl:template match="cmd[not(child::node()) and not(@args) and following-sibling::node()[1][self::text()]]">
    <xsl:text>\</xsl:text><xsl:value-of select="@name" />
	<xsl:text> </xsl:text>
</xsl:template>

<xsl:template match="env[@begin or @end]">
    <xsl:variable name="name">
        <xsl:text>{</xsl:text><xsl:value-of select="@name" /><xsl:text>}</xsl:text>
    </xsl:variable>
    <xsl:text>\</xsl:text><xsl:if test="@begin=''"><xsl:text>begin</xsl:text></xsl:if><xsl:value-of select="@begin" />
    <xsl:value-of select="$name" />
        <xsl:apply-templates />
    <xsl:text>\</xsl:text><xsl:if test="@end=''"><xsl:text>end</xsl:text></xsl:if><xsl:value-of select="@end" />
    <xsl:value-of select="$name" />
</xsl:template>

<xsl:template match="parm | group">
    <xsl:text>{</xsl:text> 
    <xsl:apply-templates />
    <xsl:text>}</xsl:text> 
</xsl:template>

<xsl:template match="ctrl[@ch]">
	<xsl:text>\</xsl:text><xsl:value-of select="@ch"/>
</xsl:template>

<xsl:template match="active[@ch]">
	<xsl:text>{\active </xsl:text>
	<xsl:value-of select="@ch"/>
	<xsl:text>}</xsl:text>
</xsl:template>
<xsl:template match="active[@ch=' ']"><xsl:text>~</xsl:text></xsl:template>

<!-- IBM's -->
	<xsl:template match="spec[@cat='esc']"><xsl:text>&#x005C;</xsl:text></xsl:template>
	<xsl:template match="spec[@cat='bg']"><xsl:text>&#x007B;</xsl:text></xsl:template>
	<xsl:template match="spec[@cat='eg']"><xsl:text>&#x007D;</xsl:text></xsl:template>
	<xsl:template match="spec[@cat='mshift']"><xsl:text>&#x0024;</xsl:text></xsl:template>
	<xsl:template match="spec[@cat='align']"><xsl:text>&amp;</xsl:text></xsl:template>
	<xsl:template match="spec[@cat='parm']"><xsl:text>&#x0023;</xsl:text></xsl:template>
	<xsl:template match="spec[@cat='sup']"><xsl:text>&#x0056;</xsl:text></xsl:template>
	<xsl:template match="spec[@cat='sub']"><xsl:text>&#x0057;</xsl:text></xsl:template>
<!-- GetFO's -->
	<xsl:template match="spec[@cat='tilde']"><xsl:text>&#x007E;</xsl:text></xsl:template>
	<xsl:template match="spec[@cat='comment']"><xsl:text>&#x0025;</xsl:text></xsl:template>
	<xsl:template match="spec[@cat='vert']"><xsl:text>&#x007C;</xsl:text></xsl:template>
	<xsl:template match="spec[@cat='lt']"><xsl:text>&lt;</xsl:text></xsl:template>
	<xsl:template match="spec[@cat='gt']"><xsl:text>&gt;</xsl:text></xsl:template>
	<xsl:template match="spec[@cat='nl']"><xsl:text>&#xA;</xsl:text></xsl:template>
	<xsl:template match="spec[@cat='nl?']"><xsl:if test="preceding::node()[1][@cat='nl' or @cat='nl?']"><xsl:text>&#xA;</xsl:text></xsl:if></xsl:template>
	<xsl:template match="spec[@cat='space']"><xsl:text>&#x0020;</xsl:text></xsl:template>
	<xsl:template match="spec[@cat='nil']"/><!-- todo -->
<!-- StUs'
	 no default char-substitution/translation, therefore no special escapes
	 use <act/>s, <cmd/>s, named <grp/>s or <ent:isoname/>s for special TeX-chars/codes
	 i.e. principle of least differences and least surprises related to TeX
	      to avoid providing overide-mechanisms for every additional default-behavior in xml/transformation
    -->

<!--  ================================================  -->
<!--  ================================================  -->

</xsl:stylesheet>

<!--  ================================================  -->
<!--  ================================================  -->
<!--  EOF TeXML2TeX.xsl                                 -->
