<!DOCTYPE xsl:stylesheet [
<!ENTITY asserts "&#x22A6;">
]>
<xsl:stylesheet version="1.0" exclude-result-prefixes="p"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:fo="http://www.w3.org/1999/XSL/Format"
  xmlns:p="http://relaxng.org/ns/proofsystem">

<xsl:variable name="fixed-font" select="'Arial'"/>

<xsl:output encoding="iso-8859-1"/>

<xsl:template match="p:proofSystem">
  <xsl:apply-templates/>
</xsl:template>

<xsl:template match="p:formula">
  <fo:block xsl:use-attribute-sets="para" text-indent="-0.71cm" start-indent="1.42cm" text-align="left">
    <xsl:apply-templates/>
  </fo:block>
</xsl:template>

<xsl:template match="p:rule">
  <fo:table xsl:use-attribute-sets="para">
    <fo:table-body>
      <fo:table-row>
	<fo:table-cell padding-end="20pt" display-align="center">
           <fo:block>(<xsl:value-of select="@name"/>)</fo:block>
        </fo:table-cell>
	<fo:table-cell>
	  <xsl:variable name="ncols" select="count(*) - 1"/>
	  <fo:table>
            <fo:table-body>
	      <xsl:if test="$ncols">
		<fo:table-row text-align="center" relative-align="baseline">
		  <xsl:for-each select="*[position() != last()]">
		    <fo:table-cell>
                      <fo:block>
		        <xsl:if test="position() != 1">&#160;&#160;&#160;&#160;</xsl:if>
		        <xsl:apply-templates select="."/>
                      </fo:block>
		    </fo:table-cell>
		  </xsl:for-each>
		</fo:table-row>
	      </xsl:if>
	      <fo:table-row>
		<fo:table-cell number-columns-spanned="{$ncols}">
                  <xsl:if test="$ncols">
                     <xsl:attribute name="border-before-style">solid</xsl:attribute>
                     <xsl:attribute name="border-before-width">0.8pt</xsl:attribute>
                  </xsl:if>
		  <fo:block text-align="center">
                    <xsl:apply-templates select="*[last()]"/>
                  </fo:block>
		</fo:table-cell>
	      </fo:table-row>
            </fo:table-body>
	  </fo:table>
	</fo:table-cell>
      </fo:table-row>
    </fo:table-body>
  </fo:table>
</xsl:template>

<xsl:template match="p:rule" mode="ignore">
  <fo:table text-align="center">
    <fo:table-body>
      <fo:table-row>
        <fo:table-cell number-columns-spanned="2">
          <fo:block text-align="center">
            <xsl:text>Rule </xsl:text>
            <xsl:value-of select="@name"/>
          </fo:block>
        </fo:table-cell>
      </fo:table-row>
      <xsl:if test="count(*)=1">
        <fo:table-row>
          <fo:table-cell><fo:block>If</fo:block></fo:table-cell>
          <fo:table-cell></fo:table-cell>
        </fo:table-row>
      </xsl:if>
      <xsl:for-each select="*">
        <fo:table-row relative-align="baseline">
          <fo:table-cell>
            <fo:block>
              <xsl:choose>
                <xsl:when test="position()=last()">then</xsl:when>
                <xsl:when test="position()=1">If</xsl:when>
                <xsl:otherwise>and</xsl:otherwise>
              </xsl:choose>
            </fo:block>
          </fo:table-cell>
          <fo:table-cell>
            <fo:block>
              <xsl:apply-templates select="."/>
            </fo:block>
          </fo:table-cell>
        </fo:table-row>
      </xsl:for-each>
    </fo:table-body>
  </fo:table>
</xsl:template>


<xsl:template match="p:judgement[@name='match']">
  <xsl:apply-templates select="*[1]"/>
  <xsl:text> </xsl:text>
  <fo:wrapper font-family="Lucida Sans Unicode">&asserts;</fo:wrapper>
  <xsl:text> </xsl:text>
  <xsl:apply-templates select="*[2]"/>
  <xsl:text>; </xsl:text>
  <xsl:apply-templates select="*[3]"/>
  <xsl:text> =~ </xsl:text>
  <xsl:apply-templates select="*[4]"/>
</xsl:template>

<xsl:template match="p:judgement[@name='weakMatch']">
  <xsl:apply-templates select="*[1]"/>
  <xsl:text> </xsl:text>
  <fo:wrapper font-family="Lucida Sans Unicode">&asserts;</fo:wrapper>
  <xsl:text> </xsl:text>
  <xsl:apply-templates select="*[2]"/>
  <xsl:text>; </xsl:text>
  <xsl:apply-templates select="*[3]"/>
  <xsl:text> =~</xsl:text>
  <fo:inline baseline-shift="sub" font-size="8pt">weak</fo:inline>
  <xsl:text> </xsl:text>
  <xsl:apply-templates select="*[4]"/>
</xsl:template>

<xsl:template match="p:judgement[@name='belongs']">
  <xsl:apply-templates select="*[1]"/>
  <xsl:text> in </xsl:text>
  <xsl:apply-templates select="*[2]"/>
</xsl:template>

<xsl:template match="p:judgement[@name='contentType']">
  <xsl:apply-templates select="*[1]"/>
  <xsl:text> </xsl:text><fo:wrapper font-weight="bold">:</fo:wrapper><fo:inline baseline-shift="sub" font-size="8pt">c</fo:inline><xsl:text> </xsl:text>
  <xsl:apply-templates select="*[2]"/>
</xsl:template>


<xsl:template match="p:judgement[@name='subset']">
  <!-- This is a really hacky way to get the parentheses in.  -->
  <xsl:text>(</xsl:text>
  <xsl:apply-templates select="*[1]"/>
  <xsl:text>)</xsl:text>
  <xsl:text> subset </xsl:text>
  <xsl:apply-templates select="*[2]"/>
</xsl:template>

<xsl:template match="p:judgement[@name='interleave']">
  <xsl:apply-templates select="*[1]"/>
  <xsl:text> interleaves </xsl:text>
  <xsl:apply-templates select="*[2]"/>
  <xsl:text>; </xsl:text>
  <xsl:apply-templates select="*[3]"/>
</xsl:template>

<xsl:template match="p:judgement[@name='whiteSpace']">
  <xsl:text>WS( </xsl:text>
  <xsl:apply-templates select="*"/>
  <xsl:text> )</xsl:text>
</xsl:template>

<xsl:template match="p:judgement">
  <xsl:value-of select="@name"/>
  <xsl:text>(</xsl:text>
  <xsl:for-each select="*">
    <xsl:if test="position()!=1">
      <xsl:text>, </xsl:text>
    </xsl:if>
    <xsl:apply-templates select="."/>
  </xsl:for-each>
  <xsl:text>)</xsl:text>
</xsl:template>

<xsl:template match="p:judgement[@name='equal']">
  <xsl:apply-templates select="*[1]"/>
  <xsl:text> = </xsl:text>
  <xsl:apply-templates select="*[2]"/>
</xsl:template>

<xsl:template match="p:not">
   <xsl:text>not(</xsl:text>
     <xsl:apply-templates select="*"/>
   <xsl:text>)</xsl:text>
</xsl:template>

<xsl:template match="p:function">
  <xsl:value-of select="@name"/>
  <xsl:text>( </xsl:text>
  <xsl:for-each select="*">
    <xsl:if test="position() != 1">
      <xsl:text>, </xsl:text>
    </xsl:if>
    <xsl:apply-templates select="."/>
  </xsl:for-each>
  <xsl:text> )</xsl:text>
</xsl:template>

<xsl:template match="p:var">
  <xsl:apply-templates select="@range"/>
  <xsl:if test="@sub">
    <fo:inline baseline-shift="sub" font-size="8pt"><xsl:value-of select="@sub"/></fo:inline>
  </xsl:if>
</xsl:template>

<xsl:template match="p:element">
  <xsl:choose>
    <xsl:when test="not(*[not(self::p:attribute|self::p:context)])">
      <fo:wrapper font-family="{$fixed-font}">&lt;<xsl:value-of select="@name"/></fo:wrapper>
        <xsl:apply-templates select="p:attribute" mode="start-tag"/>
      <fo:wrapper font-family="{$fixed-font}">/&gt;</fo:wrapper>
    </xsl:when>
    <xsl:otherwise>
      <fo:wrapper font-family="{$fixed-font}">&lt;<xsl:value-of select="@name"/></fo:wrapper>
        <xsl:apply-templates select="p:attribute|p:context" mode="start-tag"/>
      <fo:wrapper font-family="{$fixed-font}">&gt;</fo:wrapper>
	<xsl:for-each select="*[not(self::p:attribute|self::p:context)]">
	  <xsl:text> </xsl:text>
	  <xsl:apply-templates select="."/>
	</xsl:for-each>
	<xsl:text> </xsl:text>
      <fo:wrapper font-family="{$fixed-font}">&lt;/<xsl:value-of select="@name"/>&gt;</fo:wrapper>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>

<xsl:template match="p:group">
  <xsl:for-each select="*">
    <xsl:if test="position() != 1">
      <xsl:text> </xsl:text>
    </xsl:if>
    <xsl:apply-templates select="."/>
  </xsl:for-each>
</xsl:template>

<xsl:template match="p:attribute" mode="start-tag">
  <fo:wrapper font-family="{$fixed-font}">
    <xsl:text> </xsl:text>
    <xsl:value-of select="@name"/>
    <xsl:text>="</xsl:text>
  </fo:wrapper>
    <xsl:apply-templates select="*"/>
  <fo:wrapper font-family="{$fixed-font}">"</fo:wrapper>
</xsl:template>

<xsl:template match="p:context" mode="start-tag">
  <fo:wrapper font-family="{$fixed-font}"><xsl:text> </xsl:text></fo:wrapper>
  <xsl:apply-templates select="."/>
</xsl:template>

<xsl:template match="p:context">
  <xsl:text>[</xsl:text>
  <xsl:apply-templates select="*"/>
  <xsl:text>]</xsl:text>
</xsl:template>

<xsl:template match="p:attribute//p:string">
  <fo:wrapper font-family="{$fixed-font}"><xsl:value-of select="."/></fo:wrapper>
</xsl:template>

<xsl:template match="p:string">
  <fo:wrapper font-family="{$fixed-font}">"<xsl:value-of select="."/>"</fo:wrapper>
</xsl:template>

<xsl:template match="p:attribute//p:function[@name='emptyString']" priority="2">
 <xsl:text></xsl:text>
</xsl:template>

<xsl:template match="p:function[@name='emptyString']">
 <fo:wrapper font-family="{$fixed-font}">""</fo:wrapper>
</xsl:template>

<xsl:template match="@range">
 <fo:wrapper font-style="italic"><xsl:value-of select="."/></fo:wrapper>
</xsl:template>

<xsl:template match="@range[.='pattern']">
 <fo:wrapper font-style="italic">p</fo:wrapper>
</xsl:template>

<xsl:template match="@range[.='grammar']">
 <fo:wrapper font-style="italic">g</fo:wrapper>
</xsl:template>

<xsl:template match="@range[.='att']">
 <fo:wrapper font-style="italic">a</fo:wrapper>
</xsl:template>

<xsl:template match="@range[.='element']">
 <fo:wrapper font-style="italic">e</fo:wrapper>
</xsl:template>

<xsl:template match="@range[.='mixed']">
 <fo:wrapper font-style="italic">m</fo:wrapper>
</xsl:template>

<xsl:template match="@range[.='string']">
 <fo:wrapper font-style="italic">s</fo:wrapper>
</xsl:template>

<xsl:template match="@range[.='whiteSpace']">
 <fo:wrapper font-style="italic">ws</fo:wrapper>
</xsl:template>

<xsl:template match="@range[.='nameClass']">
 <fo:wrapper font-style="italic">nc</fo:wrapper>
</xsl:template>

<xsl:template match="@range[.='name']">
 <fo:wrapper font-style="italic">n</fo:wrapper>
</xsl:template>

<xsl:template match="@range[.='ncname']">
 <fo:wrapper font-style="italic">ln</fo:wrapper>
</xsl:template>

<xsl:template match="@range[.='context']">
 <fo:wrapper font-style="italic">cx</fo:wrapper>
</xsl:template>

<xsl:template match="@range[.='contentType']">
 <fo:wrapper font-style="italic">ct</fo:wrapper>
</xsl:template>

<xsl:template match="@range[.='uri']">
 <fo:wrapper font-style="italic">u</fo:wrapper>
</xsl:template>

<xsl:template match="p:function[@name='emptySet']">
 <xsl:text>{ }</xsl:text>
</xsl:template>

<xsl:template match="p:function[@name='emptySequence']">
 <xsl:text>( )</xsl:text>
</xsl:template>

<xsl:template match="p:function[@name='union']">
  <xsl:for-each select="*">
    <xsl:if test="position() != 1">
      <xsl:text> + </xsl:text>
    </xsl:if>
    <xsl:apply-templates select="."/>
  </xsl:for-each>
</xsl:template>

<xsl:template match="p:function[@name='append']">
  <xsl:for-each select="*">
    <xsl:if test="position() != 1">
      <xsl:text>, </xsl:text>
    </xsl:if>
    <xsl:apply-templates select="."/>
  </xsl:for-each>
</xsl:template>

<xsl:template match="p:judgement[@name='start']">
  <xsl:text>start() = </xsl:text>
  <xsl:apply-templates select="*[1]"/>
</xsl:template>

<xsl:template match="p:judgement[@name='bind']">
  <xsl:text>deref(</xsl:text>
  <xsl:apply-templates select="*[1]"/>
  <xsl:text>)</xsl:text>
  <xsl:text> = </xsl:text>
  <fo:wrapper font-family="{$fixed-font}">&lt;element> </fo:wrapper>
  <xsl:apply-templates select="*[2]"/>
  <xsl:text> </xsl:text>
  <xsl:apply-templates select="*[3]"/>
  <fo:wrapper font-family="{$fixed-font}"> &lt;/element></fo:wrapper>
</xsl:template>

</xsl:stylesheet>

