<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns="http://www.tei-c.org/ns/1.0"
                xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main"
                xmlns:cals="http://www.oasis-open.org/specs/tm9901"
                xmlns:iso="http://www.iso.org/ns/1.0"
                xmlns:m="http://schemas.openxmlformats.org/officeDocument/2006/math"
                xmlns:mml="http://www.w3.org/1998/Math/MathML"
                xmlns:o="urn:schemas-microsoft-com:office:office"
                xmlns:pic="http://schemas.openxmlformats.org/drawingml/2006/picture"
                xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships"
                xmlns:rel="http://schemas.openxmlformats.org/package/2006/relationships"
                xmlns:tbx="http://www.lisa.org/TBX-Specification.33.0.html"
                xmlns:tei="http://www.tei-c.org/ns/1.0"
                xmlns:teidocx="http://www.tei-c.org/ns/teidocx/1.0"
                xmlns:v="urn:schemas-microsoft-com:vml"
                xmlns:ve="http://schemas.openxmlformats.org/markup-compatibility/2006"
                xmlns:w10="urn:schemas-microsoft-com:office:word"
                xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main"
                xmlns:wne="http://schemas.microsoft.com/office/word/2006/wordml"
                xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing"
                xmlns:custprops="http://schemas.openxmlformats.org/officeDocument/2006/custom-properties"
                xmlns:vt="http://schemas.openxmlformats.org/officeDocument/2006/docPropsVTypes"
                xmlns:html="http://www.w3.org/1999/xhtml"
                version="2.0"
                exclude-result-prefixes="a pic rel ve o teidocx r m v wp w10 w wne mml vt cals tbx iso custprops">
  <doc xmlns="http://www.oxygenxml.com/ns/doc/xsl" scope="stylesheet" type="stylesheet">
    <desc>
      <p> TEI stylesheet for simplifying TEI ODD markup </p>
      <p> This library is free software; you can redistribute it and/or modify it under the
      terms of the GNU Lesser General Public License as published by the Free Software Foundation;
      either version 2.1 of the License, or (at your option) any later version. This library is
      distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the
      implied warranty of MAINTABILITY 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 </p>
      <p>Author: See AUTHORS</p>
      <p>Id: $Id: from-pass2.xsl 7952 2010-08-12 21:14:51Z rahtz $</p>
      <p>Copyright: 2008, TEI Consortium</p>
    </desc>
  </doc>

    <!-- ******************************************************************************************* -->
    <!-- second stage processing -->

      <doc xmlns="http://www.oxygenxml.com/ns/doc/xsl"  >
	<desc> Analyze numbers, marking them up to allow for decimal
	character changing </desc>
      </doc>
    <xsl:template match="text()" mode="pass2">
        <xsl:choose>
            <xsl:when test="parent::tei:num">
                <xsl:value-of select="."/>
            </xsl:when>
            <xsl:when test="parent::tei:seg[@rend='nonumber']">
                <xsl:value-of select="."/>
            </xsl:when>
            <!-- do not search for numbers inside math -->
            <xsl:when test="ancestor-or-self::m:t">
                <xsl:value-of select="."/>
            </xsl:when>
            <xsl:when test="ancestor::mml:math">
                <xsl:value-of select="."/>
            </xsl:when>

            <xsl:when test="ancestor::tbx:termEntry">
                <xsl:value-of select="."/>
            </xsl:when>

            <xsl:otherwise>
                <xsl:analyze-string select="." regex="((\d+ \d+)+,?\d*|\d+,\d+)">
                    <xsl:matching-substring>
                        <num>
                            <xsl:value-of select="regex-group(1)"/>
                        </num>
                    </xsl:matching-substring>
                    <xsl:non-matching-substring>
                        <xsl:value-of select="."/>
                    </xsl:non-matching-substring>
                </xsl:analyze-string>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>
    
    
    <doc xmlns="http://www.oxygenxml.com/ns/doc/xsl">
      <desc>Assign a unique ID to each<gi>bibl</gi></desc>
    </doc>
    <xsl:template match="tei:bibl" mode="pass2">
      <xsl:copy>
	<xsl:if test="not(@xml:id and not(w:bookmarkStart))">
	  <xsl:attribute name="xml:id">
	    <xsl:text>BIB_</xsl:text>
	    <xsl:number level="any" count="tei:bibl" />
	  </xsl:attribute>
	</xsl:if>
	<xsl:attribute name="n" select="tei:idno[@type='orgref']" />
	<xsl:attribute name="type">
	  <xsl:choose>
	    <xsl:when test="tei:edition">dated</xsl:when>
	    <xsl:otherwise>undated</xsl:otherwise>
	  </xsl:choose>
	</xsl:attribute>
	<xsl:apply-templates select="@*|*|processing-instruction()|comment()|text()" mode="pass2" />
      </xsl:copy>
    </xsl:template>
    
   <doc xmlns="http://www.oxygenxml.com/ns/doc/xsl"  >
   <desc>Remove unwanted elements from bibl</desc></doc>
    <xsl:template match="tei:bibl/tei:idno[@type='orgref']" mode="pass2">
    </xsl:template>

      <doc xmlns="http://www.oxygenxml.com/ns/doc/xsl"  >
      <desc>Look at the sections we have generated, and put
        them in &lt;front&gt; or &lt;body&gt; as appropriate</desc></doc>
	<xsl:template match="tei:text" mode="pass2">
	  <xsl:variable name="Doctext">
	    <text>
	      <xsl:for-each select="tei:fw">
                <xsl:copy-of select="."/>
	      </xsl:for-each>
	      <front>
                <xsl:apply-templates select="tei:body/tei:div[@type='toc']" mode="pass2"/>
                <xsl:apply-templates select="tei:body/tei:div[@type='foreword']" mode="pass2"/>
                <xsl:apply-templates select="tei:body/tei:div[@type='introduction']" mode="pass2"/>
	      </front>
	      <body>
                <xsl:for-each select="tei:body">
		  <xsl:for-each select="tei:div|tei:p|tei:table|cals:table">
		    <xsl:choose>
		      <xsl:when test="self::tei:div[@type='toc']"/>
		      <xsl:when test="self::tei:div[@type='foreword']"/>
		      <xsl:when test="self::tei:div[@type='introduction']"/>
		      <xsl:when test="self::tei:div[@type='bibliography']"/>
		      <xsl:when test="self::tei:div[@type='annex']"/>
		      <xsl:otherwise>
			<xsl:apply-templates select="." mode="pass2"/>
		      </xsl:otherwise>
		    </xsl:choose>
		  </xsl:for-each>
                </xsl:for-each>
	      </body>
	      <back>
                <xsl:apply-templates select="tei:body/tei:div[@type='bibliography' or @type='annex']"
				     mode="pass2"/>
	      </back>
	      
	      <!-- copy last milestone -->
	      <xsl:apply-templates select="tei:body/tei:milestone[count(//tei:body/tei:milestone)]" mode="pass2"/>
	    </text>
	  </xsl:variable>
	  <xsl:apply-templates select="$Doctext" mode="pass3"/>
    </xsl:template>

      <doc xmlns="http://www.oxygenxml.com/ns/doc/xsl"  >
      <desc> inner lists and notes in lists must be moved to inside items </desc></doc>
    <xsl:template match="tei:list/tei:list" mode="pass2"/>
    <xsl:template match="tei:list/tei:note" mode="pass2"/>

    <xsl:template match="tei:item" mode="pass2">
        <item>
            <xsl:copy-of select="@*"/>
            <xsl:variable name="me" select="generate-id()"/>
            <xsl:apply-templates mode="pass2"/>
            <!-- find following sibling lists and notes -->
            <xsl:for-each select="following-sibling::tei:list[preceding-sibling::tei:item[1][generate-id()=$me]]">
                <list>
                    <xsl:apply-templates select="*|@*|processing-instruction()|comment()|text()" mode="pass2"/>
                </list>
            </xsl:for-each>
            <xsl:for-each select="following-sibling::tei:note[preceding-sibling::tei:item[1][generate-id()=$me]]">
                <note>
                    <xsl:apply-templates select="*|@*|processing-instruction()|comment()|text()" mode="pass2"/>
                </note>
            </xsl:for-each>
        </item>
    </xsl:template>

    <!-- overrides for part 2 -->
    
    <doc xmlns="http://www.oxygenxml.com/ns/doc/xsl"  >
    <desc>Zap &lt;div&gt; with empty head only </desc></doc>
    
    <xsl:template match="tei:div[count(*)=1 and tei:head[.='']]" mode="pass2"/>

    <doc xmlns="http://www.oxygenxml.com/ns/doc/xsl"  >
    <desc>Zap spurious page break </desc></doc>
    <xsl:template match="tei:body/tei:p[count(*)=1 and tei:pb]" mode="pass2"/>


    <doc xmlns="http://www.oxygenxml.com/ns/doc/xsl"  >
    <desc>Zap empty paragraph </desc></doc>
    <xsl:template match="tei:p[not(*) and not(text())]" mode="pass2"/>

    <doc xmlns="http://www.oxygenxml.com/ns/doc/xsl"  >
    <desc>End of bookmark not needed </desc></doc>
    <xsl:template match="w:bookmarkEnd" mode="pass0"/>

    <doc xmlns="http://www.oxygenxml.com/ns/doc/xsl"  >
    <desc>Grammatical errors discarded</desc></doc>
    <xsl:template match="w:proofErr" mode="pass0"/>

    <xsl:template match="tei:div[@type='termsAndDefinitions']" mode="pass2">
      <xsl:copy>
	<xsl:copy-of select="@*"/>
	<xsl:variable name="name">
	  <xsl:text>termHeading2</xsl:text>
	</xsl:variable>
	<xsl:for-each-group select="*" group-starting-with="tei:p[@rend=$name]">
	  <xsl:choose>
	    <xsl:when test="self::tei:p[@rend=$name]">
	      <xsl:apply-templates select="." mode="group"/>
	    </xsl:when>
	    <xsl:otherwise>
	      <xsl:apply-templates select="current-group()" mode="pass2"/>
	    </xsl:otherwise>
	  </xsl:choose>
	</xsl:for-each-group>
      </xsl:copy>
    </xsl:template>
  <xsl:template match="tei:note/tei:p/tei:hi[@rend='FootnoteReference']"
		mode="pass2" priority="1001">
  </xsl:template>

  <doc xmlns="http://www.oxygenxml.com/ns/doc/xsl"  >
  <desc>Merge adjacent &lt;termEntry&gt; with same ID</desc></doc>
  <xsl:template match="tbx:termEntry" mode="pass2">
      <xsl:variable name="ID" select="@id"/>
      <xsl:choose>
	<xsl:when test="$ID=preceding-sibling::tbx:termEntry/@id"/>
	<xsl:when test="not(following-sibling::tbx:termEntry[@id=$ID])">
	  <xsl:copy>
	    <xsl:apply-templates select="@*" mode="pass2"/>
	    <xsl:apply-templates mode="pass2"/>
	  </xsl:copy>
	</xsl:when>
	<xsl:otherwise>
	  <xsl:copy>
	    <xsl:copy-of select="@*"/>
	    <xsl:for-each select="tbx:langSet">
	      <xsl:copy>
		<xsl:apply-templates select="@*" mode="pass2"/>
		<xsl:apply-templates mode="pass2" select="../tbx:note"/>
		<xsl:apply-templates mode="pass2" select="../tbx:descripGrp"/>
		<xsl:apply-templates mode="pass2"/>
	      </xsl:copy>
	    </xsl:for-each>
	    
	    <xsl:for-each select="following-sibling::tbx:termEntry[@id=$ID]/tbx:langSet">
	      <xsl:copy>
		<xsl:apply-templates select="@*" mode="pass2"/>
		<xsl:apply-templates mode="pass2" select="tbx:note"/>
		<xsl:apply-templates mode="pass2" select="../tbx:descripGrp"/>
		<xsl:apply-templates mode="pass2"/>
	      </xsl:copy>
	    </xsl:for-each>
	  </xsl:copy>
	</xsl:otherwise>
      </xsl:choose>

  </xsl:template>
  <xsl:template match="tbx:langSet/@xml:lang" mode="pass2">
    <xsl:choose>
      <xsl:when
	  test="parent::tbx:langSet/tbx:ntig/tbx:termGrp/tbx:termNote[@type='geographicalUsage']">
	<xsl:copy-of select="."/>
      </xsl:when>
      <xsl:when test="contains(.,'-')">
	<xsl:attribute name="xml:lang">
	  <xsl:value-of
	      select="substring-before(.,'-')"/>
	</xsl:attribute>
      </xsl:when>
      <xsl:otherwise>
	<xsl:copy-of select="."/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

   <doc xmlns="http://www.oxygenxml.com/ns/doc/xsl"  >
   <desc>Remove [SOURCE: ] from source</desc></doc>

  <xsl:template match="tbx:admin[@type='entrySource']/text()" mode="pass2">
    <xsl:analyze-string select="replace(.,'\[SOURCE: ','')" regex="\]$">
      <xsl:matching-substring>
      </xsl:matching-substring>
      <xsl:non-matching-substring>
	<xsl:value-of select="."/>
      </xsl:non-matching-substring>
    </xsl:analyze-string>
  </xsl:template>


  <xsl:template match="tbx:note[@type='noteTerm']" mode="pass2">
    <xsl:if
	test="not(ancestor::tbx:ntig/preceding-sibling::tbx:ntig)">
      <xsl:copy>
	<xsl:apply-templates mode="pass2"/>
      </xsl:copy>
    </xsl:if>
  </xsl:template>
  <!--
      <doc xmlns="http://www.oxygenxml.com/ns/doc/xsl"  >
      <desc>Strip label from note on definition</desc></doc>
      <xsl:template match="tbx:descrip/tbx:note/text()" mode="pass2">
      <xsl:value-of select="replace(.,'Note to definition: ','')"/>
      </xsl:template>
  -->

  <xsl:template match="tbx:descrip/tei:hi[@rend='domain']" mode="pass2"/>

   <doc xmlns="http://www.oxygenxml.com/ns/doc/xsl"  >
   <desc>Remove space before a moved domain</desc></doc>
  <xsl:template match="tbx:descrip/text()" mode="pass2">
    <xsl:choose>
      <xsl:when test="preceding-sibling::node()[1][self::tei:hi/@rend='domain']">
	<xsl:value-of select="normalize-space(.)"/>
      </xsl:when>
      <xsl:otherwise>
	<xsl:value-of select="."/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

   <doc xmlns="http://www.oxygenxml.com/ns/doc/xsl"  >
   <desc>Move domain and source to separate containers</desc></doc>

  <xsl:template match="tbx:descripGrp" mode="pass2">
    <xsl:if test="tbx:descrip/tei:hi[@rend='domain']">
      <descripGrp xmlns="http://www.lisa.org/TBX-Specification.33.0.html">
	<descrip type="subjectField">
	  <xsl:for-each select="tbx:descrip/tei:hi[@rend='domain']">
	    <xsl:value-of select="translate(.,'&lt;&gt;〈〉','')"/>
	  </xsl:for-each>
	</descrip>
      </descripGrp>
    </xsl:if>
    <xsl:copy>
      <xsl:copy-of select="@*"/>
      <xsl:apply-templates mode="pass2"/>
    </xsl:copy>
    <xsl:if test="tbx:descrip/tei:hi[@rend='source']">
      <xsl:for-each select="tbx:descrip">
	<admin xmlns="http://www.lisa.org/TBX-Specification.33.0.html" type="source">
	  <xsl:for-each select="tei:hi[@rend='source']|tei:ref">
	    <xsl:copy-of select="."/>
	  </xsl:for-each>
	</admin>
      </xsl:for-each>
    </xsl:if>
  </xsl:template>

  <xsl:template match="tbx:descrip/tei:hi[@rend='source']" mode="pass2"/>

  <xsl:template match="tbx:descrip/tei:ref" mode="pass2">
    <xsl:copy>
      <xsl:copy-of select="@*"/>
      <xsl:apply-templates mode="pass2"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="tbx:descrip" mode="pass2">
    <xsl:copy>
      <xsl:copy-of select="@*"/>
      <xsl:apply-templates mode="pass2"/>
      <xsl:for-each select="ancestor::tbx:termEntry/following-sibling::*[1][self::tei:list]">
	<xsl:copy>
	  <xsl:copy-of select="@*"/>
	  <xsl:apply-templates mode="pass2"/>
	</xsl:copy>
      </xsl:for-each>
    </xsl:copy>
   </xsl:template>
   
   <doc xmlns="http://www.oxygenxml.com/ns/doc/xsl"  >
   <desc>Zap empty &lt;list&gt; and one after termEntry </desc></doc>

   <xsl:template match="tei:list" mode="pass2">
     <xsl:choose>
       <xsl:when test="preceding-sibling::*[1][self::tbx:termEntry]">
       </xsl:when>
       <xsl:when test="count(*)=0">
       </xsl:when>
       <xsl:otherwise>
	 <xsl:copy>
	   <xsl:copy-of select="@*"/>
	   <xsl:apply-templates mode="pass2"/>
	 </xsl:copy>
       </xsl:otherwise>
     </xsl:choose>
   </xsl:template>

    <doc xmlns="http://www.oxygenxml.com/ns/doc/xsl"  >
    <desc>Strip prefix from note</desc></doc>

   <xsl:template match="tbx:note/text()" mode="pass2">
     <xsl:analyze-string select="." regex="^(NOTE|Note|ANMERKUNG)[^:]+:\s(.*)">
       <xsl:matching-substring>
	 <xsl:value-of select="regex-group(2)"/>
       </xsl:matching-substring>
       <xsl:non-matching-substring>
	 <xsl:value-of select="."/>
       </xsl:non-matching-substring>
     </xsl:analyze-string>
   </xsl:template>

    <doc xmlns="http://www.oxygenxml.com/ns/doc/xsl"  >
    <desc>Zap empty <gi>seg</gi> in term</desc></doc>

   <xsl:template match="tbx:term/tei:seg[.=' ']" mode="pass2"/>


    <doc xmlns="http://www.oxygenxml.com/ns/doc/xsl"  >
    <desc>Zap empty <gi>seg</gi> in <gi>hi</gi> in term</desc></doc>

   <xsl:template match="tbx:term/tei:hi[.=' ']" mode="pass2">
   </xsl:template>

    <doc xmlns="http://www.oxygenxml.com/ns/doc/xsl"  >
    <desc>Strip words and punctuation from term</desc></doc>

   <xsl:template match="tbx:term/tei:hi[.=', ']" mode="pass2"/>

   <xsl:template match="tbx:term/text()" mode="pass2">
     <xsl:variable name="s" select="replace(.,'DEPRECATED: ','')"/>
     <xsl:analyze-string select="$s" regex="(.+),\s*$">
       <xsl:matching-substring>
	 <xsl:value-of select="regex-group(1)"/>
       </xsl:matching-substring>
       <xsl:non-matching-substring>
	 <xsl:value-of select="$s"/>
       </xsl:non-matching-substring>
     </xsl:analyze-string>
   </xsl:template>

   <!--
  <xsl:template match="tbx:descripGrp" mode="pass2">
    <xsl:copy>
      <xsl:apply-templates select="tbx:descrip"/>
      <xsl:if test="tbx:descrip/tei:hi[@rend='source']">
	<termNote type="source">
	  <xsl:copy-of select="tbx:descrip/tei:hi[@rend='source']"/>
	</termNote>
      </xsl:if>
    </xsl:copy>
  </xsl:template>
   -->

    <xsl:template match="iso:error" mode="pass2">
      <note place="comment" resp="ISO_validator">
	<date>
	  <xsl:attribute name="when">
	    <xsl:value-of select="teidocx:whatsTheDate()"/>
	  </xsl:attribute>
	</date>
	<xsl:value-of select="."/>
      </note>
    </xsl:template>

</xsl:stylesheet>