<?xml version="1.0" encoding="iso-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <!-- RMRS2LaTeX Transformation Stylesheet version 2005-07-18
       Input DTD: A subset of rmrs.dtd by Ann Copestake
       plus additional input characters in a <text> element below <rmrs>,
       e.g., <text><c>t</c><c>e</c><c>x</c><c>t</c></text>
       Output: LaTeX
       Processing: e.g. xsltproc rmrs2latex.xsl inputrmrs.xml > rmrs.tex

       Adjust number of EPs in variable lineBreakAfterThisManyPreds below.
       Set overall font size after \begin{rmrs}.
       Current output encoding is latin1. You may wish to experiment with 
       LaTeX UTF-8 mode. In this case, set xsl:output encoding below to
       utf-8 and modify the documentclass/usepackage header (not tested).
       
       Developed by Ulrich Schaefer,
       Project QUETAL, quetal@dfki.de

       (C) 2005  DFKI Language Technology Lab http://www.dfki.de/lt

       This program is free software; you can redistribute it and/or
       modify it under the terms of the GNU Lesser General Public
       License 2.1 as published by the Free Software Foundation.

       This program is distributed in the hope that it will be useful,
       but WITHOUT ANY WARRANTY; without even the implied warranty of
       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
       Lesser General Public License for more details.
       
       You should have received a copy of the GNU Lesser General Public
       License along with this library; if not, write to the Free Software
       Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
       
       CHANGES
       2005-07-21 uschaefer Comments
-->
<!-- input DTD -->
<!-- The root element can have an arbitrary name (here rmrs-list); however, that's not -->
<!-- expressible with DTD, one would have to switch to using Schemas instead. -->
<!--
<!DOCTYPE rmrs-list [
    <!ELEMENT rmrs-list (rmrs*)>
    <!ELEMENT rmrs (text?, label, ep*, rarg*, ing*, hcons*)>
    <!ELEMENT text (c*)>
    <!ELEMENT c (#PCDATA)>
    <!ELEMENT label EMPTY>
    <!ELEMENT ep ((gpred | realpred), label, var)>
    <!ELEMENT gpred (#PCDATA)>
    <!ELEMENT realpred EMPTY>
    <!ELEMENT rarg (rargname, label, (var | constant))>
    <!ELEMENT rargname (#PCDATA)>
    <!ELEMENT var EMPTY>
    <!ELEMENT constant (#PCDATA)>
    <!ELEMENT ing (ing-a, ing-b)>
    <!ELEMENT ing-a (var)>
    <!ELEMENT ing-b (var)>
    <!ELEMENT hcons (hi, lo)>
    <!ELEMENT hi (var)>
    <!ELEMENT lo (label | var)>
    <!ATTLIST rmrs     cfrom CDATA #IMPLIED
                cto   CDATA #IMPLIED >
    <!ATTLIST ep    cfrom CDATA #REQUIRED
                  cto CDATA #REQUIRED >
    <!ATTLIST label   vid CDATA #REQUIRED >
    <!ATTLIST var    sort CDATA #REQUIRED
                  vid CDATA #REQUIRED >
    <!ATTLIST realpred lemma CDATA #REQUIRED
          pos (v | n | j | r | p | q | c | x | u) #REQUIRED
          sense CDATA #IMPLIED >
    <!ATTLIST hcons hreln (qeq | lheq | outscopes) #REQUIRED >
]>

  -->
  <xsl:output method="text" encoding="ISO-8859-1"/>
  <!-- number of elementary predicates in a row -->
  <xsl:variable name="lineBreakAfterThisManyPreds" select="number(5)" />
  <!-- var separator symbol -->
  <xsl:variable name="varsep"><xsl:text> </xsl:text></xsl:variable>
  <!-- name for arg0 if "arg0" is taken -->
  <xsl:variable name="arg0name"><xsl:text>argzero</xsl:text></xsl:variable>

  <!-- ================  /  ============= -->
  <!-- top template just calls all rmrs descendants of the root (root-element may have any name) -->
  <xsl:template match="/">
\documentclass{article}
\usepackage[latin1]{inputenc}
\newcommand{\attrib}[1]{\mbox{\sc\lowercase{#1}\ }}
\newcommand{\type}[1]{\mbox{\it #1\/}}
\newenvironment{rmrs}{\vspace{-0.5cm}\setlength{\arraycolsep}{0.1ex}\renewcommand{\arraystretch}{1.0}}{\relax}
\begin{document}
%--------------------
\begin{rmrs}
{\scriptsize
<xsl:apply-templates select="//rmrs"/>
}
\end{rmrs}
%--------------------
\end{document}
  </xsl:template>

  <!-- ===================  RMRS  ============================ -->
  <!-- just call the templates for the 5 rows of the main table (text, top,
       rels, hcons, ing), each with the rmrs-number as a parameter -->
  <xsl:template match="rmrs">
    <xsl:variable name="rmrsNum" select="position()"/>
\[\left[\begin{array}{ll}
    <xsl:call-template name="txt"/>
    <xsl:call-template name="top"/>
    <xsl:call-template name="rels"/>
    <xsl:call-template name="hcons"/>
    <xsl:call-template name="ingroup"/>
\end{array}\right]\]
  </xsl:template>

  <!-- ==================  TXT  ================= -->
  <!-- new class in stylesheet might be good -->
  <xsl:template name="txt">
    <xsl:variable name="text">
      <xsl:for-each select="text/c">
          <xsl:call-template name="filterstring">
            <xsl:with-param name="string">
              <xsl:value-of select="."/>
            </xsl:with-param>
          </xsl:call-template>
        </xsl:for-each>
    </xsl:variable>
\attrib{TEXT}&amp;\mbox{<xsl:value-of select="$text"/>}\\
  </xsl:template>

  <!-- =================  TOP  ============= -->
  <xsl:template name="top">
\attrib{TOP}&amp;<xsl:apply-templates select="label"/>\\
  </xsl:template>

  <!-- =================  REL  ============= -->
  <!-- The relations (main entry). Calls ep and freeArgs, and puts all that into {} -->
  <xsl:template name="rels">
\attrib{RELS}&amp;\left\{\begin{array}{l}<xsl:call-template name="ep"/>
    <xsl:call-template name="freeArgs"/>\end{array}\right\}\\
  </xsl:template>

  <!-- =================  EP  ============ -->
  <xsl:template name="ep">
    <xsl:variable name = "argAfterLbl">
    	<xsl:choose>
    		<xsl:when test="count(rarg/rargname[text()='ARG0'])=0">ARG0</xsl:when>
    		<xsl:otherwise><xsl:value-of select="$arg0name" /></xsl:otherwise>
    	</xsl:choose>
    </xsl:variable>
    <xsl:for-each select="ep">
      <!-- remember handle for later putting the rmrs-vars to the right
           place -->
      <xsl:variable name="handle">
        <xsl:value-of select="label/@vid"/>
      </xsl:variable>
\left[\begin{array}{ll}
\multicolumn{2}{l}{\type{<xsl:apply-templates select="gpred | realpred"/>}}\\
\attrib{LBL}&amp;<xsl:call-template name="varspan">
                  <xsl:with-param name="varname">
                    <xsl:apply-templates select="label"/>
                  </xsl:with-param>
                </xsl:call-template>\\
\attrib{<xsl:value-of select="$argAfterLbl"/>}
&amp;<xsl:call-template name="varspan">
                  <xsl:with-param name="varname">
                    <xsl:apply-templates select="var"/>
                  </xsl:with-param>
                </xsl:call-template>\\
            <!-- rels-rows greater 3: the other arguments / variables
             other arguments of current ep come from the older
                 rarg-sisters, having a label with vid = the vid of  
                 the current ep, which was stored into variable
                 $handle above. Parent goes back up from the label
                 to the rarg element, because that's what has to be
                 selected / looped -->
            <xsl:for-each select="following-sibling::rarg/label[@vid=$handle]/parent::*">
\attrib{<xsl:value-of select="rargname"/>}&amp;
                  <xsl:apply-templates select="constant"/>
                  <xsl:call-template name="varspan">
                    <xsl:with-param name="varname">
                      <xsl:apply-templates select="var"/>
                    </xsl:with-param>
                  </xsl:call-template>\\
            </xsl:for-each>\end{array}\right]
	  <xsl:if test = "position() mod $lineBreakAfterThisManyPreds = 0">
\\
 	  </xsl:if>
    </xsl:for-each>
  </xsl:template>

  <!-- =================  FREEARGS  ============ -->
  <xsl:template name="freeArgs">
    <xsl:for-each select="rarg">
      <xsl:variable name="label">
        <xsl:value-of select="label/@vid"/>
      </xsl:variable>
      <!-- if the argument (rarg) has an ep sister with same label@vid, then
           the argument is not free -->
      <xsl:variable name="numWitnessesOfNonFreedom">
        <xsl:value-of select="count(parent::*/ep/label[@vid=$label])"/>
      </xsl:variable>
      <!-- if the argument has no such sisters, it's free. -->
      <xsl:if test="$numWitnessesOfNonFreedom = 0">
\multicolumn{2}{l}{\type{free\_args}}\\
\attrib{<xsl:value-of select="rargname"/>}&amp;
                  <xsl:apply-templates select="constant"/>
                  <xsl:call-template name="varspan">
                    <xsl:with-param name="varname">
                      <xsl:apply-templates select="var"/>
                    </xsl:with-param>
                  </xsl:call-template>\\
      </xsl:if>
    </xsl:for-each>
  </xsl:template>

  <!-- ================  GPRED  ================== -->
  <!-- predicate names are text from gpred elements -->
  <xsl:template match="gpred">
<xsl:call-template name="filterstring"><xsl:with-param name="string" select="."/></xsl:call-template></xsl:template>

  <!-- ===========  REALPRED  =============== -->
  <!-- To distinguish gpreds from realpreds, the latter start
       with "_". E.g. the verb-realpred "use" is output _use_v. -->
  <xsl:template match="realpred">\_<xsl:value-of select="@lemma"/>\_<xsl:value-of select="@pos"/></xsl:template>

  <!-- =======  HCONS  ================ -->
  <xsl:template name="hcons">
\attrib{HCONS}&amp;\{
        <xsl:for-each select="hcons">
          <xsl:call-template name="varspan">
            <xsl:with-param name="varname">
              <xsl:apply-templates select="hi"/>
            </xsl:with-param>
          </xsl:call-template>
          \mbox{<xsl:text> </xsl:text>
          <xsl:value-of select="@hreln"/>
          <xsl:text> </xsl:text>}
          <xsl:call-template name="varspan">
            <xsl:with-param name="varname">
              <xsl:apply-templates select="lo"/>
            </xsl:with-param>
          </xsl:call-template>
          <xsl:if test="not (position()=last())">
            <xsl:text>, </xsl:text>
          </xsl:if>
        </xsl:for-each>\}\\
  </xsl:template>

  <!-- ========  HI  =================== -->
  <xsl:template match="hi">
<xsl:apply-templates select="var"/></xsl:template>

  <!-- ========  LO  =================== -->
  <xsl:template match="lo">
<xsl:apply-templates select="label"/></xsl:template>

  <!-- ========  INGROUP  =================== -->
  <xsl:template name="ingroup">
\attrib{ING}&amp;\{
        <xsl:for-each select="ing">
          <xsl:call-template name="varspan">
            <xsl:with-param name="varname">
              <xsl:apply-templates select="ing-a"/>
            </xsl:with-param>
          </xsl:call-template>
          <xsl:text>\mbox{ ing }</xsl:text>
          <xsl:call-template name="varspan">
            <xsl:with-param name="varname">
              <xsl:apply-templates select="ing-b"/>
            </xsl:with-param>
          </xsl:call-template>
          <xsl:if test="not (position()=last())">
            <xsl:text>,</xsl:text>
          </xsl:if>
        </xsl:for-each>\}\\
  </xsl:template>

  <!-- ========  ING-A  =================== -->
  <xsl:template match="ing-a">
<xsl:apply-templates select="var"/></xsl:template>

  <!-- ========  ING-B  =================== -->
  <xsl:template match="ing-b">
<xsl:apply-templates select="var"/></xsl:template>

  <!-- ============  VAR  ============ -->
  <xsl:template match="var">
    <xsl:value-of select="@sort"/>
    <xsl:value-of select="@vid"/>
    <xsl:for-each select="@*">
      <xsl:if test='name(.)!="sort" and name(.)!="vid"'>
        <xsl:value-of select="$varsep"/>
        <xsl:value-of select="name(.)"/>
        <xsl:text>=</xsl:text>
        <xsl:value-of select="."/>
      </xsl:if>
    </xsl:for-each>
  </xsl:template>

  <!-- ==========  LABEL  =============== -->
  <!-- labels always start with "h", followed by their vid -->
  <xsl:template match="label">
<xsl:text>h</xsl:text><xsl:value-of select="@vid"/></xsl:template>

  <!-- ==========  CONSTANT  =============== -->
  <xsl:template match="constant">
\mbox{<xsl:call-template name="filterstring"><xsl:with-param name="string" select="."/></xsl:call-template>}</xsl:template>


  <!-- ==============  AUXILIARY TEMPLATES  =============== -->
  <!-- format variables and morph features -->
  <xsl:template name="varspan">
    <xsl:param name="varname">error</xsl:param>
    <xsl:choose>
      <xsl:when test='contains($varname, $varsep) and contains($varname, "=")'>
\mbox{\it <xsl:value-of select='substring-before($varname, $varsep)'/>\/}
{\tiny\begin{array}{l}\mbox{
      <xsl:call-template name="stackit">
        <xsl:with-param name="string" select='substring-after($varname, $varsep)'/>
      </xsl:call-template>
}\end{array}}
        </xsl:when>
        <xsl:otherwise>
\mbox{\it <xsl:value-of select="$varname"/>\/}
        </xsl:otherwise>
    </xsl:choose></xsl:template>

  <!-- stack morph fv-pairs -->
  <xsl:template name="stackit">
    <xsl:param name="string"/>
    <xsl:if test="string-length($string)>0">
      <xsl:choose>
        <xsl:when test="substring($string,1,1)=' '">}\\\mbox{</xsl:when>
        <xsl:otherwise><xsl:value-of select="substring($string,1,1)"/></xsl:otherwise>
      </xsl:choose>
      <xsl:call-template name="stackit">
        <xsl:with-param name="string" select="substring($string,2)"/>
      </xsl:call-template>
    </xsl:if>
  </xsl:template>

  <!-- filter zombie characters -->
  <xsl:template name="filterstring">
    <xsl:param name="string"/>
    <xsl:if test="string-length($string)>0">
      <xsl:choose>
        <xsl:when test="substring($string,1,1)='&amp;'">\&amp;</xsl:when>
        <xsl:when test="substring($string,1,1)='#'">\#</xsl:when>
        <xsl:when test="substring($string,1,1)='$'">\$</xsl:when>
        <xsl:when test="substring($string,1,1)='^'">\^{ }</xsl:when>
        <xsl:when test="substring($string,1,1)='%'">\%</xsl:when>
        <xsl:when test="substring($string,1,1)='_'">\_</xsl:when>
        <xsl:when test="substring($string,1,1)='{'">\{</xsl:when>
        <xsl:when test="substring($string,1,1)='}'">\}</xsl:when>
        <xsl:when test="substring($string,1,1)='|'">$|$</xsl:when>
        <xsl:when test="substring($string,1,1)='~'">\~{ }</xsl:when>
        <xsl:otherwise><xsl:value-of select="substring($string,1,1)"/></xsl:otherwise>
      </xsl:choose>
      <xsl:call-template name="filterstring">
        <xsl:with-param name="string" select="substring($string,2)"/>
      </xsl:call-template>
    </xsl:if>
  </xsl:template>

</xsl:stylesheet>

