<?xml version="1.0"?>

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" indent="yes" />

<!-- A parameter, to be set on the command line, of whether or not to include
     paragraph style information -->
<xsl:param name="styles" />
<xsl:param name="linenumbers" />
<xsl:template match="/">
<book>
  <ver></ver>

  <front>
    <titleinf>
      <title>
        <xsl:apply-templates select="(//Attribute[AttrName='ShortTitle'])[1]" />
      </title>
      <subtitle>
        <xsl:apply-templates select="(//Para[PgfTag='LongTitle'])[1]" />
      </subtitle>
    </titleinf>

    <extraneous>
      <p>
        <xsl:if test="$styles">
	        <xsl:attribute name="class">House</xsl:attribute>
        </xsl:if>
        <xsl:apply-templates select="(//Attribute[AttrName='House'])[1]/AttrValue" />
      </p>
      <p>
        <xsl:if test="$styles">
	        <xsl:attribute name="class">PrintNumber</xsl:attribute>
        </xsl:if>
        <xsl:apply-templates select="(//Attribute[AttrName='PrintNumber'])[1]/AttrValue" />
      </p>
      <p>
        <xsl:if test="$styles">
	        <xsl:attribute name="class">SessionNumber</xsl:attribute>
        </xsl:if>
        <xsl:apply-templates select="(//Attribute[AttrName='SessionNumber'])[1]/AttrValue" />
      </p>
    </extraneous>

    <bibinfo />

    <preface>
      <xsl:for-each select="//ElementEnd[text()='Cover']/following::*[1]/preceding::Para[count(preceding::ElementBegin[ETag[text()='Cover']]) eq 1]">
      <xsl:choose>
        <xsl:when test="PgfTag='Rubric' and ParaLine/String">
          <p> <xsl:apply-templates select="ParaLine/String" /> </p>
        </xsl:when>
      </xsl:choose>
    </xsl:for-each>
    <xsl:for-each-group select="//ElementEnd[text()='Cover']/following::*[1]/preceding::Para[count(preceding::ElementBegin[ETag[text()='Cover']]) eq 1 and ParaLine/String]" group-starting-with="//Para[PgfTag='Heading']">
      <xsl:if test="position() &gt; 1">
        <section>
        <head> <xsl:apply-templates select="./ParaLine/String" /> </head>
        <xsl:apply-templates select="(current-group()/self::Para[PgfTag])[position() &gt; 1]" />
        </section>
      </xsl:if>
    </xsl:for-each-group>

    <!-- Commons Bills have a PrintOrder table -->
    <xsl:apply-templates select="//Tbl[TblTag='PrintOrder']//Para" />
    </preface>
  </front>

  <body>
  <!-- CLAUSES -->
  <!-- This group allows templates to assume they are in a group 
       and use current-group() as a context.  -->
  <!-- Exclude everything after the end of the clauses -->
  <xsl:for-each-group select="//ElementEnd[text()='Clauses']/following::*[1]/preceding::TextFlow/Para[PgfTag]" group-starting-with="//Para[PgfTag='A']">
    <!-- 1st group contents; 2nd group clauses. Don't process contents -->
    <xsl:if test="position() eq 2">
    <xsl:call-template name="parts">
      <xsl:with-param name="depth" select="'section'" />
    </xsl:call-template>
    </xsl:if>
  </xsl:for-each-group>
  </body>

  <back>
  <!-- SCHEDULES -->
  <!-- This group allows templates to assume they are in a group 
       and use current-group() as a context.  -->
  <!-- Exclude everything after the end of the schedules -->
  <xsl:for-each-group select="//ElementEnd[text()='Schedules']/following::*[1]/preceding::TextFlow/Para[PgfTag]" group-starting-with="//Para[PgfTag='SchedulesTitle']">
    <!-- 1st group contents; 2nd group clauses. Don't process contents -->
    <xsl:if test="position() eq 2">
      <xsl:call-template name="schedules" />
    </xsl:if>
  </xsl:for-each-group>
  </back>

</book>
</xsl:template>



<!-- STRUCTURAL TEMPLATES -->
<!-- 
A set for the Clauses, and a set for the Schedules
Clauses: Part, Chapter, Group, Clause
Schedules: Part, Group, Paragraph
each template calls the next one down in its hierarchy
-->

<!-- Allows templates to vary the element name <subsect1> etc
     depending on which levels exists in a particular part of the bill -->
<xsl:template name="hierarchy">
<xsl:param name="depth" />
<xsl:choose>
  <xsl:when test="$depth = 'section'">
    <xsl:value-of select="'subsect1'" />
  </xsl:when>
  <xsl:when test="$depth = 'subsect1'">
    <xsl:value-of select="'subsect2'" />
  </xsl:when>
  <xsl:when test="$depth = 'subsect2'">
    <xsl:value-of select="'subsect3'" />
  </xsl:when>
</xsl:choose>
</xsl:template>


<!-- STRUCTURAL TEMPLATES FOR SCHEDULES -->

<xsl:template name="schedules">
  <xsl:for-each-group select="current-group()/self::Para[PgfTag]" group-starting-with="/mif2xml/mif2xml/TextFlow/Para[PgfTag='ScheduleNumber']">
    <xsl:if test="position() &gt; 1">
      <section>
        <xsl:if test="$styles">
	  <xsl:attribute name="class">schedule</xsl:attribute>
        </xsl:if>
        <head>
          <xsl:apply-templates select="./PgfNumString" />
          -
          <xsl:apply-templates select="(current-group()/self::Para[PgfTag='ScheduleTitle']//String)[1]" />
          [<xsl:apply-templates select="current-group()/self::Para[PgfTag='SectionReference']//String" />]
        </head>
        <xsl:call-template name="parts.sch" />
      </section>
    </xsl:if>
  </xsl:for-each-group>
</xsl:template>

<xsl:template name="parts.sch">
<xsl:choose>
<xsl:when test="count(current-group()/self::Para[PgfTag='PartNumber.sch'])">
  <xsl:for-each-group select="current-group()/self::Para[PgfTag]" group-starting-with="/mif2xml/mif2xml/TextFlow/Para[PgfTag='PartNumber.sch']">
    <xsl:if test="position() &gt; 1">
      <subsect1>
        <xsl:if test="$styles">
          <xsl:attribute name="class">part.sch</xsl:attribute>
        </xsl:if>
        <head>
          <xsl:apply-templates select="./PgfNumString" />
          -
          <xsl:apply-templates select="(current-group()/self::Para[PgfTag='PartTitle.sch']//String)[1]" />
        </head>
        <xsl:call-template name="groups.sch">
          <xsl:with-param name="depth" select="'subsect2'" />
        </xsl:call-template>
      </subsect1>
    </xsl:if>
  </xsl:for-each-group>
</xsl:when>
<xsl:otherwise>
  <xsl:call-template name="groups.sch">
    <xsl:with-param name="depth" select="'subsect1'" />
  </xsl:call-template>
</xsl:otherwise>
</xsl:choose>
</xsl:template>


<xsl:template name="groups.sch">
<xsl:param name="depth" />
<xsl:choose>
<xsl:when test="count(current-group()/self::Para[PgfTag='CrossHeading.sch'])">
  <xsl:for-each-group select="current-group()/self::Para[PgfTag]" group-starting-with="/mif2xml/mif2xml/TextFlow/Para[PgfTag='CrossHeading.sch']">
    <xsl:if test="position() &gt; 1">
      <xsl:element name="{$depth}">
        <xsl:if test="$styles">
          <xsl:attribute name="class">group.sch</xsl:attribute>
        </xsl:if>
        <head>
          <xsl:apply-templates select="(current-group()/self::Para[PgfTag='CrossHeading.sch']//String)[1]" />
        </head>
        <xsl:call-template name="content.sch">
          <xsl:with-param name="depth">
            <xsl:call-template name="hierarchy">
              <xsl:with-param name="depth" select="$depth" />
            </xsl:call-template>
          </xsl:with-param>
         </xsl:call-template>
      </xsl:element>
    </xsl:if>
  </xsl:for-each-group>
</xsl:when>
<xsl:otherwise>
  <xsl:call-template name="content.sch">
    <xsl:with-param name="depth" select="$depth" />
  </xsl:call-template>
</xsl:otherwise>
</xsl:choose>
</xsl:template>

<xsl:template name="content.sch">
<xsl:param name="depth" />
<xsl:choose>
  <xsl:when test="count(current-group()/self::Para[PgfTag='Paragraph.sch' or PgfTag='SubParagraph.sch'])">
    <!-- Most schedules are divided into Paragraphs -->
    <xsl:call-template name="paragraphs.sch">
      <xsl:with-param name="depth" select="$depth" />
    </xsl:call-template>
  </xsl:when>
  <xsl:otherwise>
    <!-- But some only contain tables or definitions -->
    <xsl:call-template name="noparagraphs.sch">
      <xsl:with-param name="depth" select="$depth" />
    </xsl:call-template>
  </xsl:otherwise>
</xsl:choose>
</xsl:template>

<xsl:template name="paragraphs.sch">
<xsl:param name="depth" />
<xsl:for-each-group select="current-group()/self::Para[PgfTag]" group-starting-with="//Para[PgfTag='Paragraph.sch'] | //Para[PgfTag='SubParagraph.sch']">
  <xsl:if test="position() &gt; 1">
    <xsl:element name="{$depth}">
      <xsl:if test="$styles">
        <xsl:attribute name="class">paragraphs.sch</xsl:attribute>
      </xsl:if>
      <head>
      Paragraph 
      <xsl:value-of select="replace(replace(./PgfNumString, '\(.*', ''), '\\t', '')" />
      </head>
      <xsl:apply-templates select="current-group()" />
    </xsl:element>
  </xsl:if>
</xsl:for-each-group>
</xsl:template>

<xsl:template name="noparagraphs.sch">
  <xsl:param name="depth" />
  <xsl:apply-templates select="current-group()" />
</xsl:template>


<!-- STRUCTURAL TEMPLATES FOR CLAUSES-->

<xsl:template name="parts">
<xsl:param name="depth" />
<xsl:choose>
<xsl:when test="count(current-group()/self::Para[PgfTag='PNum'])">
  <xsl:for-each-group select="current-group()/self::Para[PgfTag]" group-starting-with="/mif2xml/mif2xml/TextFlow/Para[PgfTag='PNum']">
    <xsl:if test="position() &gt; 1">
      <xsl:element name="{$depth}">
      <xsl:if test="$styles">
        <xsl:attribute name="class">part</xsl:attribute>
      </xsl:if>
      <head>
        <xsl:call-template name="pagenos" />
        <xsl:value-of select="./PgfNumString" />
        <xsl:value-of select="(current-group()/self::Para[PgfTag='PNum']//String)[1]" />
        -
        <xsl:value-of select="(current-group()/self::Para[PgfTag='PartTitle']//String)[1]" />
      </head>
      <xsl:call-template name="chapters">
        <xsl:with-param name="depth">
          <xsl:call-template name="hierarchy">
            <xsl:with-param name="depth" select="$depth" />
          </xsl:call-template>
        </xsl:with-param>
      </xsl:call-template>
    </xsl:element>
    </xsl:if>
  </xsl:for-each-group>
</xsl:when>
<xsl:otherwise>
  <xsl:call-template name="chapters">
    <xsl:with-param name="depth" select="$depth" />
  </xsl:call-template>
</xsl:otherwise>
</xsl:choose>
</xsl:template>


<xsl:template name="chapters">
<xsl:param name="depth" />
<xsl:choose>
<xsl:when test="count(current-group()/self::Para[PgfTag='CNum'])">
  <xsl:for-each-group select="current-group()/self::Para[PgfTag]" group-starting-with="//Para[PgfTag='CNum']">
    <xsl:if test="position() &gt; 1">
    <xsl:element name="{$depth}">
    <xsl:if test="$styles">
      <xsl:attribute name="class">chapter</xsl:attribute>
    </xsl:if>
    <head>
    <xsl:apply-templates select="./PgfNumString" />
    -
    <xsl:apply-templates select="(current-group()/self::Para[PgfTag='ChapterHeading']//String)[1]" />
    </head>

    <xsl:call-template name="groups">
      <xsl:with-param name="depth">
        <xsl:call-template name="hierarchy">
          <xsl:with-param name="depth" select="$depth" />
        </xsl:call-template>
      </xsl:with-param>
    </xsl:call-template>

    </xsl:element>
    </xsl:if>
  </xsl:for-each-group>
</xsl:when>
<xsl:otherwise>
  <xsl:call-template name="groups">
    <xsl:with-param name="depth" select="$depth" />
  </xsl:call-template>
</xsl:otherwise>
</xsl:choose>
</xsl:template>


<xsl:template name="groups">
<xsl:param name="depth" />
<xsl:choose>
<xsl:when test="count(current-group()/self::Para[PgfTag='PCSubHeading'])">
<xsl:for-each-group select="current-group()/self::Para[PgfTag]" group-starting-with="//Para[PgfTag='PCSubHeading']">
  <xsl:if test="position() &gt; 1">
    <xsl:element name="{$depth}">
    <xsl:if test="$styles">
      <xsl:attribute name="class">group</xsl:attribute>
    </xsl:if>
    <head>
    <xsl:apply-templates select="(current-group()/self::Para[PgfTag='PCSubHeading']//String)[1]" />
    </head>
      <xsl:call-template name="clauses">
        <xsl:with-param name="depth">
          <xsl:call-template name="hierarchy">
            <xsl:with-param name="depth" select="$depth" />
          </xsl:call-template>
        </xsl:with-param>
      </xsl:call-template>
    </xsl:element>
  </xsl:if>
</xsl:for-each-group>
</xsl:when>
<xsl:otherwise>
  <xsl:call-template name="clauses">
    <xsl:with-param name="depth" select="$depth" />
  </xsl:call-template>
</xsl:otherwise>
</xsl:choose>
</xsl:template>


<xsl:template name="clauses">
<xsl:param name="depth" />
<xsl:for-each-group select="current-group()/self::Para[PgfTag]" group-starting-with="//Para[PgfTag='ClauseTitle']">
  <xsl:if test="position() &gt; 1">
    <xsl:element name="{$depth}">
      <xsl:if test="$styles">
        <xsl:attribute name="class">clause</xsl:attribute>
      </xsl:if>
      <!-- <head> Clause Title is provided by a template </head> -->
      <xsl:apply-templates select="current-group()" />
    </xsl:element>
  </xsl:if>
</xsl:for-each-group>
</xsl:template>



<!-- CONTENT TEMPLATES - DEALING WITH CLAUSES, PARAS, ETC. -->

<!-- Front Cover - titles etc -->
<xsl:template match="//Attribute[AttrName='ShortTitle']">
  <xsl:apply-templates select="./AttrValue" />
</xsl:template>
<xsl:template match="//Para[PgfTag='LongTitle']">
  <xsl:apply-templates select=".//String | .//Char" />
</xsl:template>

<!-- Clauses -->
<!-- Separate template was to allow brackets around the number
     This is now incorporated in the PgfNumString template -->
<xsl:template match="/mif2xml/mif2xml/TextFlow/Para[PgfTag='ClauseTitle']">
  <head>
  <xsl:if test="$styles">
  <xsl:attribute name="class" select="./PgfTag" />
  </xsl:if>
  <xsl:apply-templates select="./PgfNumString" />
  <xsl:apply-templates select=".//String | .//Char | .//ATbl | .//AFrame | .//Marker" />
  </head>
</xsl:template>

<xsl:template match="/mif2xml/mif2xml/TextFlow/Para[PgfTag='ClauseText' or PgfTag='SubSection' or PgfTag='Paragraph' or PgfTag='SubParagraph' or PgfTag='SubSubParagraph' or PgfTag='Definition' or PgfTag='DefinitionList1' or PgfTag='DefinitionList2' or PgfTag='TableTitle' or PgfTag='Body']">
  <p>
  <xsl:if test="$styles">
  <xsl:attribute name="class" select="./PgfTag" />
  </xsl:if>
  <xsl:apply-templates select="./PgfNumString" />
  <xsl:text> </xsl:text>
  <xsl:apply-templates select=".//String | .//Char" />
  </p>
  <xsl:apply-templates select=".//ATbl | .//AFrame | .//Marker" />
</xsl:template>

<!-- Clauses etc. quoted for amendments -->
<xsl:template match="/mif2xml/mif2xml/TextFlow/Para[PgfTag='_ClauseText' or PgfTag='_SubSection' or PgfTag='_Paragraph' or PgfTag='_SubParagraph' or PgfTag='_SubSubParagraph' or PgfTag='_Definition' or PgfTag='_DefinitionList1' or PgfTag='_DefinitionList2' or PgfTag='_TableTitle']">
  <p>
  <xsl:if test="$styles">
  <xsl:attribute name="class" select="./PgfTag" />
  </xsl:if>
  <xsl:apply-templates select="./PgfNumString" />
  <xsl:text> </xsl:text>
  <xsl:apply-templates select=".//String | .//Char" />
  </p>
  <xsl:apply-templates select=".//ATbl | .//AFrame | .//Marker" />
</xsl:template>

<!-- Structures (Clause and Schedule) quoted for amendments -->
<xsl:template match="/mif2xml/mif2xml/TextFlow/Para[PgfTag='_PNum' or PgfTag='_PartTitle' or PgfTag='_CNum' or PgfTag='_ChapterHeading' or PgfTag='AMDPCSubHeading' or PgfTag='_ClauseTitle' or PgfTag='_ScheduleNumber' or PgfTag='_PartNumber.sch' or PgfTag='_CrossHeading.sch']">
  <p>
  <xsl:if test="$styles">
  <xsl:attribute name="class" select="./PgfTag" />
  </xsl:if>
  <xsl:apply-templates select="./PgfNumString" />
  <xsl:text> </xsl:text>
  <xsl:apply-templates select=".//String | .//Char" />
  </p>
  <xsl:apply-templates select=".//ATbl | .//AFrame | .//Marker" />
</xsl:template>

<!-- Schedules -->
<xsl:template match="/mif2xml/mif2xml/TextFlow/Para[PgfTag='_Paragraph.sch' or PgfTag='_SubParagraph.sch' or PgfTag='_SubParagraphCont.sch' or PgfTag='_SubSubParagraph.sch' or PgfTag='_SubSubSubParagraph.sch']">
  <p>
  <xsl:if test="$styles">
  <xsl:attribute name="class" select="./PgfTag" />
  </xsl:if>
  <xsl:apply-templates select="./PgfNumString" />
  <xsl:text> </xsl:text>
  <xsl:apply-templates select=".//String | .//Char" />
  </p>
  <xsl:apply-templates select=".//ATbl | .//AFrame | .//Marker" />
</xsl:template>

<!-- Schedules etc. quoted for amendments -->
<xsl:template match="/mif2xml/mif2xml/TextFlow/Para[PgfTag='Paragraph.sch' or PgfTag='SubParagraph.sch' or PgfTag='SubParagraphCont.sch' or PgfTag='SubSubParagraph.sch' or PgfTag='SubSubSubParagraph.sch']">
  <p>
  <xsl:if test="$styles">
  <xsl:attribute name="class" select="./PgfTag" />
  </xsl:if>
  <xsl:apply-templates select="./PgfNumString" />
  <xsl:text> </xsl:text>
  <xsl:apply-templates select=".//String | .//Char" />
  </p>
  <xsl:apply-templates select=".//ATbl | .//AFrame | .//Marker" />
</xsl:template>

<!-- Suppress Schedule elements that are output from strings in templates -->
<xsl:template match="//Para[PgfTag='CrossHeading.sch' or PgfTag='PartNumber.sch' or PgfTag='PartTitle.sch' or PgfTag='ScheduleNumber' or PgfTag='SectionReference' or PgfTag='ScheduleTitle']">
  <!-- But sometimes they host tables and things so... -->
  <xsl:apply-templates select=".//ATbl | .//AFrame | .//Marker" />
</xsl:template>

<!-- Suppress other annoyances -->
<xsl:template match="//Para[PgfTag]" priority="-3">
  <p> PgfTag: <xsl:value-of select="./PgfTag" /> </p>
</xsl:template>
<xsl:template match="//Para" priority="-5">
</xsl:template>


<!-- CONTENT TEMPLATES - DEALING WITH TABLES AND FORMULAE -->

<!-- Markers (Markers), which appear to be to do with links-->
<xsl:template match="/mif2xml/mif2xml/TextFlow/Para//Marker">
</xsl:template>

<!-- Tables (ATbls) -->
<xsl:template match="ATbl">
  <xsl:variable name="id" select="." />
  <!-- mif2xml[1] b/c Schedules table ids start from 1 again so clash -->
  <xsl:apply-templates select="ancestor::mif2xml[1]/Tbls/Tbl[TblID=$id]" />
  <!--
  <table>
    <p> TABLE <xsl:value-of select="." /> </p>
      <xsl:if test="../../PgfTag='TableTitle'">
        <p> <xsl:apply-templates select="../../ParaLine/String | ../../ParaLine/Char" /> </p>
      </xsl:if>
  </table>
  -->
</xsl:template>

<xsl:template match="//Tbl">
  <table>
    <p> TABLE <xsl:value-of select="TblID" /> </p>
    <tablebody>
      <xsl:apply-templates select="TblH/Row | TblBody/Row" />
    </tablebody>
  </table>
</xsl:template>

<xsl:template match="TblH/Row">
  <colheads> <xsl:apply-templates select="Cell" /> </colheads>
</xsl:template>
<xsl:template match="TblH/Row/Cell">
  <chead> <xsl:apply-templates select="CellContent/Para" /> </chead>
</xsl:template>
<xsl:template match="TblBody/Row">
  <row> <xsl:apply-templates select="Cell" /> </row>
</xsl:template>
<xsl:template match="TblBody/Row/Cell">
  <di> <xsl:apply-templates select="CellContent/Para" /> </di>
</xsl:template>

<xsl:template match="Para[PgfTag='CellHeading' or PgfTag='CellBody']">
  <xsl:apply-templates select=".//String | .//Char" />
</xsl:template>

<!-- Mathematical Formulae (AFrames) -->
<xsl:template match="AFrame">
  <xsl:variable name="id">
    <xsl:value-of select="." /> 
  </xsl:variable>
  <p>
    FORMULA <xsl:value-of select="." /> 
  "<xsl:value-of select="/mif2xml/mif2xml/AFrames/Frame[ID=$id]/Math/MathFullForm" />"
  </p>
</xsl:template>


<!-- CONTENT TEMPLATES - DEALING WITH SPECIAL CHARACTERS -->

<xsl:template match="/mif2xml/mif2xml/TextFlow/Para/ParaLine/TextRectID">
  <hr />
    <xsl:variable name="tr">
      <xsl:value-of select="." />
    </xsl:variable>
    <xsl:value-of select="(/mif2xml/mif2xml/Page/TextRect[1][ID=$tr])[1]/../PageNum" />
</xsl:template>

<xsl:template name="pagenos">
  <ppage />
  <xsl:if test="generate-id(preceding::TextRectID[1]/following::String[1]) = generate-id(.)">
  <ppage>
    <!-- TextRectID contains a page code no -->
    <xsl:variable name="tr">
      <xsl:value-of select="preceding::TextRectID[1]" />
    </xsl:variable>
    <!-- Use that to look up the real page no from the Pages section -->
    <xsl:value-of select="(/mif2xml/mif2xml/Page/TextRect[1][ID=$tr])[1]/../PageNum" />
  </ppage>
  </xsl:if>
</xsl:template>

<!-- Page numbers -->
<xsl:template match="/mif2xml/mif2xml/TextFlow/Para/ParaLine/String" priority="1">
  <!-- Page Nos have to be within content elements like <section> etc. -->
  <!-- Check if this is the first String after a page no -->
  <!-- TextRectID contains a page code no -->
  <xsl:variable name="tr">
    <xsl:value-of select="preceding::TextRectID[1]" />
  </xsl:variable>
  <xsl:if test="generate-id(preceding::TextRectID[1]/following::String[1]) = generate-id(.)">
  <ppage>
    <!-- Use that to look up the real page no from the Pages section -->
    <xsl:value-of select="(/mif2xml/mif2xml/Page/TextRect[1][ID=$tr])[1]/../PageNum" />
  </ppage>
  </xsl:if>
  <xsl:if test="position() = 1">
    <xsl:if test="$linenumbers">
      <ppage>
        <xsl:value-of select="(/mif2xml/mif2xml/Page/TextRect[1][ID=$tr])[1]/../PageNum" />.<xsl:value-of select="count(preceding::ParaLine/String[1][preceding::TextRectID[1]=$tr]) + 1" />
      </ppage>
    </xsl:if>
  </xsl:if>
  <xsl:apply-templates />
  <xsl:text> </xsl:text>
</xsl:template>

<xsl:template match="/mif2xml/mif2xml/TextFlow/Para/PgfNumString">
<!-- This template is horrible because the brackets around ClauseTitles 
     are implicit and need manual adding -->
  <xsl:if test="$styles">
    <xsl:if test="../PgfTag = 'ClauseTitle'">
      <span class="PgfNumString">
        (<xsl:apply-templates />)
      </span>
    </xsl:if>
    <xsl:if test="not(../PgfTag = 'ClauseTitle')">
      <span class="PgfNumString">
        <xsl:apply-templates />
      </span>
    </xsl:if>
  </xsl:if>
  <xsl:if test="not($styles)">
  <xsl:if test="../PgfTag = 'ClauseTitle'">
      (<xsl:apply-templates />)
    </xsl:if>
    <xsl:if test="not(../PgfTag = 'ClauseTitle')">
      <xsl:apply-templates />
    </xsl:if>
  </xsl:if>
</xsl:template>

<xsl:template match="text()">
  <xsl:call-template name="charcodes">
    <xsl:with-param name="string" select="." />
    <xsl:with-param name="pos" select="0" />
  </xsl:call-template>
</xsl:template>


<xsl:template name="charcodes">
  <!-- Replaces \xd2 type hex character codes, if present, with XML equivs -->
  <!-- Extend by adding xsl:whens, and adjusting the $pos params -->
  <xsl:param name="string" />
  <xsl:param name="pos" />

  <xsl:choose>
    <xsl:when test="not(contains($string, '\'))">
      <xsl:value-of select="$string" />
    </xsl:when>

    <xsl:when test="$pos eq 1"> 
      <xsl:call-template name="charcodes">
        <xsl:with-param name="string" select="replace($string, '\\xd2 ', '&#x22;')" />
        <xsl:with-param name="pos" select="2" />
      </xsl:call-template>
    </xsl:when>

    <xsl:when test="$pos eq 2"> 
      <xsl:call-template name="charcodes">
        <xsl:with-param name="string" select="replace($string, '\\xd3 ', '&#x22;')" />
        <xsl:with-param name="pos" select="3" />
      </xsl:call-template>
    </xsl:when>
    
    <xsl:when test="$pos eq 3"> 
      <xsl:call-template name="charcodes">
        <xsl:with-param name="string" select='replace($string, "\\xd5 ", "&#x27;")' />
        <xsl:with-param name="pos" select="4" />
      </xsl:call-template>
    </xsl:when>
    
    <xsl:when test="$pos eq 4"> 
      <xsl:call-template name="charcodes">
        <xsl:with-param name="string" select='replace($string, "\\xd5 ", "&#x27;")' />
        <xsl:with-param name="pos" select="1024" />
      </xsl:call-template>
    </xsl:when>
    
    <xsl:when test="$pos eq 1024"> 
      <xsl:value-of select="$string" />
    </xsl:when>

    <xsl:otherwise> 
      <xsl:call-template name="charcodes">
        <xsl:with-param name="string" select="replace($string, '\\t', '')" />
        <xsl:with-param name="pos" select="1" />
      </xsl:call-template>
    </xsl:otherwise>

  </xsl:choose>
</xsl:template>


<xsl:template match="/mif2xml/mif2xml/TextFlow//Para/ParaLine/Char">
  <!-- <xsl:apply-templates /> -->
  <xsl:choose>
    <xsl:when test="text() eq 'EmDash'">
      <xsl:text>-</xsl:text>
    </xsl:when>
    <xsl:when test="text() eq 'Pound'">
      <xsl:text>&#xa3;</xsl:text>
    </xsl:when>
    <xsl:when test="text() eq 'HardReturn'">
      <xsl:text> </xsl:text>
    </xsl:when>
    <xsl:when test="text() eq 'HardSpace'">
      <xsl:text> </xsl:text>
    </xsl:when>
  </xsl:choose>
</xsl:template>


</xsl:stylesheet>

