P4toP5NZETC

From TEIWiki

Jump to: navigation, search

Below is the stylesheet used to convert TEI P4 text to TEI P5 text at the NZETC (http://www.nzetc.org/). It is based on a previous stylesheet by Sebastian Rahtz. Many of the extensions to Sebastian's stylesheet are due to data-tidying needed to cope with P5's significantly better checking of content types.

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet 
    xmlns:edate="http://exslt.org/dates-and-times"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:tei="http://www.tei-c.org/ns/1.0" 
    xmlns="http://www.tei-c.org/ns/1.0"
    exclude-result-prefixes="tei edate" version="1.0">
  <!-- 
       
       P4 to P5 converter 
       
       Stuart Yeates <stuart.yeates@VUW.AC.NZ>
        based on a stylesheet by
       Sebastian Rahtz <sebastian.rahtz@oucs.ox.ac.uk>
       
       $Date: 2008-02-16 03:30:22 +1300 (Sat, 16 Feb 2008) $  $Id: p4top5.xsl 4420 2008-02-15 14:30:22Z rahtz $
       
       Copyright 2007 TEI Consortium
       
       Permission is hereby granted, free of charge, to any person obtaining
       a copy of this software and any associated documentation gfiles (the
       ``Software''), to deal in the Software without restriction, including
       without limitation the rights to use, copy, modify, merge, publish,
       distribute, sublicense, and/or sell copies of the Software, and to
       permit persons to whom the Software is furnished to do so, subject to
       the following conditions:
       
       The above copyright notice and this permission notice shall be included
       in all copies or substantial portions of the Software.
       
  -->
  <xsl:output method="xml" encoding="utf-8"
    cdata-section-elements="tei:eg" omit-xml-declaration="no"/>
  
  <xsl:variable name="processor">
    <xsl:value-of select="system-property('xsl:vendor')"/>
  </xsl:variable>
 
  <xsl:variable name="today">
    <xsl:choose>
      <xsl:when test="function-available('edate:date-time')">
	<xsl:value-of select="edate:date-time()"/>
      </xsl:when>
      <xsl:when test="contains($processor,'SAXON')">
	<xsl:value-of select="Date:toString(Date:new())"
		      xmlns:Date="/java.util.Date"/>
      </xsl:when>
      <xsl:otherwise>0000-00-00</xsl:otherwise>
    </xsl:choose>
  </xsl:variable>
  
  <xsl:variable name="uc">ABCDEFGHIJKLMNOPQRSTUVWXYZ</xsl:variable>
  <xsl:variable name="lc">abcdefghijklmnopqrstuvwxyz</xsl:variable>
  
  <xsl:template match="*">
    <xsl:choose>
      <xsl:when test="namespace-uri()=''">
	<xsl:element namespace="http://www.tei-c.org/ns/1.0" name="{local-name(.)}">
	  <xsl:apply-templates select="*|@*|processing-instruction()|comment()|text()"/>
	</xsl:element>
      </xsl:when>
      <xsl:otherwise>
	<xsl:copy>
	  <xsl:apply-templates select="*|@*|processing-instruction()|comment()|text()"/>
	</xsl:copy>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <xsl:template match="@*|processing-instruction()|comment()">
    <xsl:copy/>
  </xsl:template>
  
  
  <xsl:template match="text()">
    <xsl:value-of select="."/>
  </xsl:template>
  
  
  <!-- change of name, or replaced by another element -->
  <xsl:template match="teiCorpus.2">
    <teiCorpus xsi:schemaLocation="http://www.tei-c.org/ns/1.0 nzetc-p5.xsd"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
      <xsl:apply-templates select="*|@*|processing-instruction()|comment()|text()"/>
    </teiCorpus>
  </xsl:template>
  
  <xsl:template match="witness/@sigil">
    <xsl:attribute name="xml:id">
      <xsl:value-of select="."/>
    </xsl:attribute>
  </xsl:template>

  <xsl:template match="witList">
    <listWit>
      <xsl:apply-templates select="*|@*|processing-instruction()|comment()|text()"/>
    </listWit>
  </xsl:template>
  
  
  <xsl:template match="TEI.2">
    <TEI xsi:schemaLocation="http://www.tei-c.org/ns/1.0 nzetc-p5.xsd"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
      <xsl:apply-templates select="*|@*|processing-instruction()|comment()|text()"/>
    </TEI>
  </xsl:template>
  
  <xsl:template match="xref">
    <xsl:element namespace="http://www.tei-c.org/ns/1.0" name="ref">
      <xsl:apply-templates select="*|@*|processing-instruction()|comment()|text()"/>
    </xsl:element>
  </xsl:template>
  
  
  <xsl:template match="xptr">
    <xsl:element namespace="http://www.tei-c.org/ns/1.0" name="ptr">
      <xsl:apply-templates select="*|@*|processing-instruction()|comment()|text()"/>
    </xsl:element>
  </xsl:template>
  
  
  <xsl:template match="figure/@url"/>
  
  <xsl:template match="figure[@url]">
    <figure>
      <xsl:apply-templates select="@id"/>
       <xsl:value-of select="@id"/>/
      <xsl:value-of select="@n"/>/
      <xsl:value-of select="@url"/>
      <graphic>
        <xsl:apply-templates select="@n|@url"/>
        <xsl:apply-templates select="@id" mode="graphic"/>
      </graphic>
      <xsl:apply-templates select="*|processing-instruction()|comment()|text()"/>
    </figure>
    <xsl:text>
    </xsl:text>
  </xsl:template>
  
  
  <xsl:template match="figure/@entity"/>
  
  <xsl:template match="figure[@entity]">
    
    <xsl:variable name="entity">
      <xsl:choose>
      <xsl:when test="contains(@entity,'.')">
        <xsl:value-of select="substring-before(@entity, '.')"/>
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="@entity"/>
      </xsl:otherwise>
      </xsl:choose>
    </xsl:variable>
    
    <xsl:variable name="mimetype">
    <xsl:choose>
      <xsl:when test="contains(@entity,'.gif')">
	<xsl:text>image/gif</xsl:text>
      </xsl:when>
      <xsl:when test="contains(@entity,'.pdf')">
	<xsl:text>application/pdf</xsl:text>
      </xsl:when>
      <xsl:when test="contains(@entity,'.jpg')">
	<xsl:text>image/jpeg</xsl:text>
      </xsl:when>
      <xsl:otherwise>
	<xsl:text>image/jpeg</xsl:text>
      </xsl:otherwise>
    </xsl:choose>
    </xsl:variable>

    <xsl:variable name="url">
    <xsl:choose>
      <xsl:when test="contains(@entity,'.gif')">
	<xsl:value-of select="@entity"/>
      </xsl:when>
      <xsl:when test="contains(@entity,'.pdf')">
	<xsl:value-of select="@entity"/>
      </xsl:when>
      <xsl:when test="contains(@entity,'.jpg')">
	<xsl:value-of select="@entity"/>
      </xsl:when>
      <xsl:otherwise>
	<xsl:value-of select="concat(@entity,'.jpg')"/>
      </xsl:otherwise>
    </xsl:choose>
    </xsl:variable>
    
    <figure>
      <xsl:attribute name="xml:id"><xsl:value-of select="@id"/></xsl:attribute>
      <graphic>
          <xsl:attribute name="url"><xsl:value-of select="$url"/></xsl:attribute>
          <xsl:attribute name="mimeType"><xsl:value-of select="$mimetype"/></xsl:attribute>
          <xsl:attribute name="xml:id"><xsl:value-of select="concat(@id, '-g')"/></xsl:attribute>
          <xsl:apply-templates select="@n"/>
      </graphic>
      <xsl:apply-templates select="*|text()|comment()|processing-instruction()"/>
    </figure>
  </xsl:template>
  
  <xsl:template match="event">
    <incident>
      <xsl:apply-templates select="@*|*|text()|comment()|processing-instruction()"/>
    </incident>
  </xsl:template>
  
  <xsl:template match="state">
    <refState>
      <xsl:apply-templates select="@*|*|text()|comment()|processing-instruction()"/>
    </refState>
  </xsl:template>
  
  
  <xsl:template match="language">
    <xsl:element namespace="http://www.tei-c.org/ns/1.0" name="language">
	<xsl:if test="@id">
        <xsl:attribute name="ident">
	  <xsl:choose>
	    <xsl:when  test="@id='eng'"><xsl:text>en</xsl:text></xsl:when>
	    <xsl:when  test="@id='moa'"><xsl:text>mi</xsl:text></xsl:when>
	    <xsl:when  test="@id='mao'"><xsl:text>mi</xsl:text></xsl:when>
	    <xsl:when  test="@id='rap'"><xsl:text>rap</xsl:text></xsl:when>
	    <xsl:when  test="@id='SAMPA'"><xsl:text>x-sampa</xsl:text></xsl:when>
	    <xsl:otherwise><xsl:value-of select="@id"/></xsl:otherwise>
	  </xsl:choose>
        </xsl:attribute>
        </xsl:if>
      <xsl:apply-templates select="*|processing-instruction()|comment()|text()"/>
    </xsl:element>
  </xsl:template>
  
  <!-- attributes lost -->
  <!-- dropped from TEI. Added as new change records later -->
  <xsl:template match="@date.created"/>
  
  <xsl:template match="@date.updated"/>
  
  <!-- dropped from TEI. No replacement -->
  <xsl:template match="refsDecl/@doctype"/>
  
  <!-- dates -->
  <xsl:template match="dateRange">
    <date>
      <xsl:apply-templates select="*|@*|processing-instruction()|comment()|text()"/>
    </date>
  </xsl:template>
  
  
  <xsl:template match="dateRange/@from">
    <xsl:copy-of select="."/>
  </xsl:template>
  
  <xsl:template match="dateRange/@to">
    <xsl:copy-of select="."/>
  </xsl:template>
  
  <xsl:template match="date/@value|time/@value|docDate/@value">

    <xsl:variable name="normal">
      <xsl:choose>
        <!-- unknown years encoded badly -->
        <xsl:when test="starts-with(.,'0000-')">
          <xsl:value-of select="concat('--',substring-after(.,'0000-'))"/>
        </xsl:when>
        <!-- days encoded badly -->
        <xsl:when test="string-length(.)=9 and substring(.,8,1)='-'">
          <xsl:value-of select="substring(.,0,8)"/>-0<xsl:value-of select="substring(.,9,1)"/>
        </xsl:when>
        <!-- months encoded badly -->
        <xsl:when test="string-length(.)=9 and not (substring(.,8,1)='-')">
          <xsl:value-of select="substring(.,0,6)"/>-0<xsl:value-of select="substring(.,8,1)"/>
        </xsl:when>
        <!-- unknown months and days encoded badly -->
        <xsl:when test="string-length(.)=10 and contains(.,'-00-00')">
          <xsl:value-of select="substring(.,0,4)"/>
        </xsl:when>        
        <xsl:otherwise>
          <xsl:value-of select="."/>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:variable>

      <xsl:attribute name="when">
	<xsl:value-of select="$normal"/>
      </xsl:attribute>
  </xsl:template>

  <!-- eat dates with question marks in them (all are automatically generated)-->
  <xsl:template match="date[contains(@value,'?')]|dateRange[contains(@to,'?')]|dateRange[contains(@from,'?')]">
    <xsl:apply-templates  select="*|processing-instruction()|comment()|text()"/>
  </xsl:template>
  
  <!-- format of the dates in the maori legal corpus file are bad and 
       don't comply with the stricter schema. eat their values. -->
  <!-- more tei/legalmaoriarchive.teiCorpus | grep 'xi:include' | sed 's/<xi:include href="/or @id="/' | sed 's/.xml"\/>/"/'--> 
  <xsl:template match="/TEI.2[@id='Auc1873-1909Proc'
                          or @id='Auc1911NgaM'
                          or @id='Auc1912NgaM'
                          or @id='BIM1032Epam'
                          or @id='BIM1192Kape'
                          or @id='BIM1195Huin'
                          or @id='BIM1196Tong'
                          or @id='BIM1203Wiki'
                          or @id='BIM1204KiaH'
                          or @id='BIM1205KoNg'
                          or @id='BIM1250Kare'
                          or @id='BIM1251Kare'
                          or @id='BIM1269Meih'
                          or @id='BIM1310TeWa'
                          or @id='BIM1336West'
                          or @id='BIM1341Panu'
                          or @id='BIM1346Tawh'
                          or @id='BIM1386Nati'
                          or @id='BIM1390Paep'
                          or @id='BIM1427Tuu'
                          or @id='BIM1518Ehoa'
                          or @id='BIM1563Hepi'
                          or @id='BIM205KiNg'
                          or @id='BIM496Evid'
                          or @id='BIM521Ngat'
                          or @id='BIM533Kaho'
                          or @id='BIM534Frie'
                          or @id='BIM554Kawa'
                          or @id='BIM577Panu'
                          or @id='BIM588Tawh'
                          or @id='BIM594Furt'
                          or @id='BIM595Ngar'
                          or @id='BIM596Kian'
                          or @id='BIM597KiNg'
                          or @id='BIM623Puka'
                          or @id='BIM635Tino'
                          or @id='BIM636Nati'
                          or @id='BIM646Lett'
                          or @id='BIM658Whak'
                          or @id='BIM666Runa'
                          or @id='BIM673Tumu'
                          or @id='BIM675Kaiw'
                          or @id='BIM678Peti'
                          or @id='BIM685Peti'
                          or @id='BIM689Haer'
                          or @id='BIM694Runa'
                          or @id='BIM697Pueh'
                          or @id='BIM698Wita'
                          or @id='BIM737Kara'
                          or @id='BIM753Heta'
                          or @id='BIM754Kuik'
                          or @id='BIM762Rang'
                          or @id='BIM763Hiri'
                          or @id='BIM764Hira'
                          or @id='BIM765Tami'
                          or @id='BIM767Paor'
                          or @id='BIM781Inoi'
                          or @id='BIM782Kawe'
                          or @id='BIM783Timb'
                          or @id='BIM787Hena'
                          or @id='BIM814Mohi'
                          or @id='BIM825Hepe'
                          or @id='BIM840TeWa'
                          or @id='BIM854Kuin'
                          or @id='BIM859Kawe'
                          or @id='BIM867Tene'
                          or @id='BIM868Panu'
                          or @id='BIM869KiNg'
                          or @id='BIM870Rang'
                          or @id='BIM872Puka'
                          or @id='BIM873TeHa'
                          or @id='BIM874Tika'
                          or @id='BIM875Tomo'
                          or @id='BIM876Hena'
                          or @id='BIM885Peti'
                          or @id='BIM935Nati'
                          or @id='BIM952Panu'
                          or @id='BIMS36Kahi'
                          or @id='BIMs310Chur'
                          or @id='Gov1879Ture'
                          or @id='Gov1880Ture'
                          or @id='Gov1881NgaT'
                          or @id='Gov1903Acts'
                          or @id='Gov1904Acts'
                          or @id='Gov1905Acts'
                          or @id='Gov1906Acts'
                          or @id='Gov1907Acts'
                          or @id='Gov1908Acts'
                          or @id='Gov1909Acts'
                          or @id='Gov1910Acts'
                          or @id='HePPuka'
                          or @id='HuiMaor'
                          or @id='IndRitu'
                          or @id='KawRena'
                          or @id='Kot1894Kore'
                          or @id='Mac01Comp'
                          or @id='Mac02Comp'
                          or @id='MacRepo'
                          or @id='MacWhak'
                          or @id='MaoKore'
                          or @id='MaoPare'
                          or @id='MarKoNg'
                          or @id='MarOutl'
                          or @id='NZGNati'
                          or @id='NZPCorr'
                          or @id='NZPHous'
                          or @id='NZPPeti'
                          or @id='NZPRoya'
                          or @id='NZPa1900NgaK'
                          or @id='NZPa1901NgaK'
                          or @id='NZPa1902NgaK'
                          or @id='NZPa1904-5NgaK'
                          or @id='NZPaV01NgaK'
                          or @id='NZPaV02NgaK'
                          or @id='NZPaV03NgaK'
                          or @id='NZPaV04NgaK'
                          or @id='Nat1876Repo'
                          or @id='Nat1877Repo'
                          or @id='Nat1878Repo'
                          or @id='Nat1879Repo-1'
                          or @id='Nat1879Repo-2'
                          or @id='Nat1880Repo'
                          or @id='Nat1881Repo'
                          or @id='Nat1882Repo'
                          or @id='Nat1883Repo'
                          or @id='Nat1884Repo-2'
                          or @id='Nat1885Repo'
                          or @id='Nat1887Repo-1'
                          or @id='Nat1887Repo-2'
                          or @id='Nat1888Repo'
                          or @id='Nat1889Repo'
                          or @id='Nat1890Repo'
                          or @id='Nat1891Repo-2'
                          or @id='Nat1892Repo'
                          or @id='Nat1893Repo'
                          or @id='Nat1894Repo'
                          or @id='Nat1895Repo'
                          or @id='Nat1896Repo'
                          or @id='Nat1897Repo'
                          or @id='Nat1898Repo'
                          or @id='Nat1899Repo'
                          or @id='Nat1900Repo'
                          or @id='Nat1901Repo'
                          or @id='Nat1902Repo'
                          or @id='Nat1903Repo'
                          or @id='Nat1904Repo'
                          or @id='Nat1905Repo'
                          or @id='Nat1906Repo'
                          or @id='Nat1907Repo'
                          or @id='Nat1908Repo'
                          or @id='PunLett'
                          or @id='SedWhai'
                          or @id='TaiProp'
                          or @id='TeWRetu'
                          or @id='Tur01Nort'
                          or @id='Tur01Plan'
                          or @id='Tur02Nort'
                          or @id='Tur02Plan'
                          or @id='TurEpit'
                          or @id='TurOldP'
                          or @id='TurWhen'
                          or @id='Wai1861Mahi'
                          or @id='Wai1863Mahi'
                          or @id='Wai864Mahi'
                          or @id='Wai1865Mahi'
                          or @id='Wai1872NgaM'
                          or @id='Wai1874NgaM'
                          or @id='Wai1875NgaM'
                          or @id='Wai1887NgaM'
                          or @id='Wai1891NgaM'
                          or @id='Wai1896NgaM'
                          or @id='Wait1872NgaM'
                          or @id='WesRepo'
                          or @id='WhaEasy'
                          ]/text//date/@value"/>

  <!-- attributes changed name -->
  
  <xsl:template match="@url">
    <xsl:attribute name="target">
      <xsl:value-of select="."/>
    </xsl:attribute>
  </xsl:template>

  <xsl:template match="@doc">
    <xsl:attribute name="target">
      <xsl:value-of select="unparsed-entity-uri(.)"/>
    </xsl:attribute>
  </xsl:template>
  
  
  <xsl:template match="@id">
    <xsl:choose>
      <xsl:when test="parent::lang">
	<xsl:attribute name="ident">
	  <xsl:value-of select="."/>
	</xsl:attribute>
      </xsl:when>
      <xsl:otherwise>
        <xsl:if test=".!=''">
          <xsl:attribute name="xml:id">
            <xsl:value-of select="."/>
          </xsl:attribute>
        </xsl:if>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>
  
  <xsl:template match="@lang">
    <xsl:attribute name="xml:lang">
      <xsl:choose>
	<xsl:when  test=".='eng'"><xsl:text>en</xsl:text></xsl:when>
	<xsl:when  test=".='moa'"><xsl:text>mi</xsl:text></xsl:when>
	<xsl:when  test=".='rap'"><xsl:text>rap</xsl:text></xsl:when>
	<xsl:when  test=".='SAMPA'"><xsl:text>x-sampa</xsl:text></xsl:when>
	<xsl:otherwise><xsl:value-of select="."/></xsl:otherwise>
      </xsl:choose>
    </xsl:attribute>
  </xsl:template>
  
  <xsl:template match="change/@date"/>
  
  <xsl:template match="date/@certainty">
    <xsl:attribute name="cert">
      <xsl:value-of select="."/>
    </xsl:attribute>
  </xsl:template>
  
  <!-- all pointing attributes preceded by # -->
  
  <xsl:template match="variantEncoding/@location|num/@value">
    <xsl:copy-of select="."/>
  </xsl:template>

  <xsl:template match="@ana|@active|@adj|@adjFrom|@adjTo|@children|@children|@class|@code|@code|@copyOf|@corresp|@decls|@domains|@end|@exclude|@fVal|@feats|@follow|@from|@hand|@inst|@langKey|@location|@mergedin|@new|@next|@old|@origin|@otherLangs|@parent|@passive|@perf|@prev|@render|@resp|@sameAs|@scheme|@script|@select|@since|@start|@synch|@target|@targetEnd|@to|@to|@value|@value|@who|@wit">
    <xsl:attribute name="{name(.)}">
      <xsl:call-template name="splitter">
	<xsl:with-param name="val">
	  <xsl:value-of select="."/>
	</xsl:with-param>
      </xsl:call-template>
    </xsl:attribute>
  </xsl:template>
  
  
  <xsl:template name="splitter">
    <xsl:param name="val"/>
    <xsl:choose>
      <xsl:when test="not(starts-with($val,'#'))">
	<xsl:text>#</xsl:text>
      </xsl:when>
    </xsl:choose>
    <xsl:choose>
      <xsl:when test="contains($val,' ')">
	<xsl:value-of select="substring-before($val,' ')"/>
	<xsl:text> </xsl:text>
	<xsl:call-template name="splitter">
	  <xsl:with-param name="val">
	    <xsl:value-of select="substring-after($val,' ')"/>
	  </xsl:with-param>
	</xsl:call-template>
      </xsl:when>
      <xsl:otherwise>
	<xsl:value-of select="$val"/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>
  
  
  <!-- fool around with selected elements -->

  
 <!-- hand stuff -->
 <xsl:template match="hand">
   <handNote>
     <xsl:apply-templates select="@*|*|text()|comment()|processing-instruction()"/>
   </handNote>
 </xsl:template>

 <xsl:template match="handList">
   <handNotes>
     <xsl:apply-templates select="@*|*|text()|comment()|processing-instruction()"/>
   </handNotes>
 </xsl:template>

  

 <!-- TODO fix me, this should be allowed -->
 <xsl:template match="bibl/imprint">
    <xsl:apply-templates select="*|text()|comment()|processing-instruction()"/>
  </xsl:template>

  <xsl:template match="editionStmt/editor">
    <respStmt>    
      <xsl:apply-templates select="@*[local-name()!='role']"/>
      <resp><xsl:value-of select="@role"/></resp>
      <name><xsl:apply-templates/></name>
    </respStmt>
  </xsl:template>
  
  <!-- header -->  
  
  <xsl:template match="teiHeader">
    <teiHeader>
      <xsl:apply-templates select="@*|*|comment()|processing-instruction()"/>
      
      <xsl:if test="not(revisionDesc) and (@date.created or @date.updated)">
	<revisionDesc>
	  <xsl:if test="@date.updated">
	    <change>
	    <label>updated</label>
	    <date>
	      <xsl:value-of select="@date.updated"/>
	    </date>
	    <label>Date edited</label>
	    </change>
	  </xsl:if>
	  <xsl:if test="@date.created">
	    <change>
	      <label>created</label>
	      <date>
		<xsl:value-of select="@date.created"/>
	      </date>
	      <label>Date created</label>
	    </change>
	  </xsl:if>
	</revisionDesc>
      </xsl:if>
      <!--
	  <change when="{$today}">Converted to TEI P5 XML by p4top5.xsl
	  written by Sebastian
	  Rahtz at Oxford University Computing Services.</change>
	  </revisionDesc>
	  </xsl:if>
      -->
    </teiHeader>
  </xsl:template>
  
  <xsl:template match="revisionDesc">
    <revisionDesc>
      <xsl:apply-templates
	  select="@*|*|comment()|processing-instruction()"/>
    </revisionDesc>
  </xsl:template>
  
  <xsl:template match="publicationStmt">
    <publicationStmt>
      <xsl:apply-templates select="@*|*|comment()|processing-instruction()"/>
      <!--
	  <availability>
	  <p>Licensed under <ptr target="http://creativecommons.org/licenses/by-sa/2.0/uk/"/></p>
	  </availability>
      -->
    </publicationStmt>
  </xsl:template>
  
 <!-- space does not have @extent any more -->
  <xsl:template match="space/@extent">
    <xsl:attribute name="quantity">
      <xsl:value-of select="."/>
    </xsl:attribute>
  </xsl:template>

  <!-- tagsDecl has a compulsory namespace child now -->
  <xsl:template match="tagsDecl">
    <xsl:if test="*">
      <tagsDecl>
	<namespace name="http://www.tei-c.org/ns/1.0">
	  <xsl:apply-templates select="*|comment()|processing-instruction"/>
	</namespace>
      </tagsDecl>
    </xsl:if>
  </xsl:template>
  
  <!-- orgTitle inside orgName? redundant -->
  <xsl:template match="orgName/orgTitle">
      <xsl:apply-templates/>
  </xsl:template>

 <!-- no need for empty <p> in sourceDesc -->  
 <!-- actually we do need these when it's all there is
  <xsl:template match="sourceDesc/p[string-length(.)=0]"/>
  -->

  <!-- start creating the new choice element -->
  <xsl:template match="corr[@sic]">
    <choice>
      <corr>
	<xsl:value-of select="text()" />
      </corr>
      <sic>
	<xsl:value-of select="@sic" />
      </sic>
    </choice>
  </xsl:template>
  
  <xsl:template match="gap/@desc">
    <desc>
      <xsl:value-of select="."/>
    </desc>
  </xsl:template>

  <xsl:template match="sic[@corr]">
    <choice>
      <sic>
	<xsl:apply-templates/>
      </sic>
      <corr>
	<xsl:value-of select="@corr" />
      </corr>
    </choice>
  </xsl:template>
  
  <xsl:template match="abbr[@expan]">
    <choice>
      <abbr>
	<xsl:apply-templates/>
      </abbr>
      <expan>
	<xsl:value-of select="@expan" />
      </expan>
    </choice>
  </xsl:template>
  
  <xsl:template match="expan[@abbr]">
    <choice>
      <expan>
	<xsl:apply-templates/>
      </expan>
      <abbr>
	<xsl:value-of select="@abbr" />
      </abbr>
    </choice>
  </xsl:template>
  
  <!-- special consideration for <change> element -->
  <xsl:template match="change">
    <change>
      <xsl:apply-templates select="@*"/>
      <xsl:apply-templates select="item/@*"/>
      
      <xsl:apply-templates select="date"/>
      
      <xsl:if test="respStmt/resp">
	<label>
	  <xsl:value-of select="respStmt/resp/text()"/>
	</label>
      </xsl:if>
	<xsl:for-each select="respStmt/name">
	  <name>
	    <xsl:apply-templates
		select="@*|*|comment()|processing-instruction()|text()"/>
	  </name>
	</xsl:for-each>
	<xsl:for-each select="item">
	  <xsl:apply-templates
	      select="*|comment()|processing-instruction()|text()"/>
	</xsl:for-each>
        <xsl:apply-templates select="comment()"/>
    </change>
  </xsl:template>


  <xsl:template match="respStmt[resp]">
    <respStmt>
      <xsl:apply-templates select="@*"/>  
      <xsl:choose>
	<xsl:when test="resp/name">
	  <resp>
	    <xsl:value-of select="resp/text()"/>
	  </resp>
	    <xsl:for-each select="resp/name">
	      <name>
		<xsl:apply-templates/>
	      </name>
	    </xsl:for-each>
	</xsl:when>
	<xsl:otherwise>
	  <xsl:apply-templates select="*|comment()|processing-instruction()|text()"/>
	</xsl:otherwise>
      </xsl:choose>
    </respStmt>
  </xsl:template>

  <xsl:template match="q/@direct"/>
  
  <xsl:template match="q">
    <q>
      <xsl:apply-templates
	  select="@*|*|comment()|processing-instruction()|text()"/>
    </q>
  </xsl:template>

  
<!-- if we are reading the P4 with a DTD,
       we need to avoid copying the default values
       of attributes -->
  
  <xsl:template match="@targOrder">
    <xsl:if test="not(translate(.,$uc,$lc) ='u')">
      <xsl:attribute name="targOrder">
	<xsl:value-of select="."/>
      </xsl:attribute>
    </xsl:if>
  </xsl:template>
  
  
  <xsl:template match="@opt">
    <xsl:if test="not(translate(.,$uc,$lc) ='n')">
      <xsl:attribute name="opt">
	<xsl:value-of select="."/>
      </xsl:attribute>
    </xsl:if>
  </xsl:template>
  
  
  <xsl:template match="@to">
    <xsl:if test="not(translate(.,$uc,$lc) ='ditto')">
      <xsl:attribute name="to">
	<xsl:value-of select="."/>
      </xsl:attribute>
    </xsl:if>
  </xsl:template>
  
  
  <xsl:template match="@default">
    <xsl:choose>
      <xsl:when test="translate(.,$uc,$lc)= 'no'"/>
      <xsl:otherwise>
	<xsl:attribute name="default">
	  <xsl:value-of select="."/>
	</xsl:attribute>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>
  
  
  <xsl:template match="@part">
    <xsl:if test="not(translate(.,$uc,$lc) ='n')">
      <xsl:attribute name="part">
	<xsl:value-of select="."/>
      </xsl:attribute>
    </xsl:if>
  </xsl:template>
  
  
  <xsl:template match="@full">
    <xsl:if test="not(translate(.,$uc,$lc) ='yes')">
      <xsl:attribute name="full">
	<xsl:value-of select="."/>
      </xsl:attribute>
    </xsl:if>
  </xsl:template>
  
  
  <xsl:template match="@from">
    <xsl:if test="not(translate(.,$uc,$lc) ='root')">
      <xsl:attribute name="from">
	<xsl:value-of select="."/>
      </xsl:attribute>
    </xsl:if>
  </xsl:template>
  
  
  <xsl:template match="@status">
    <xsl:choose>
      <xsl:when test="parent::teiHeader">
	<xsl:if test="not(translate(.,$uc,$lc) ='new')">
	  <xsl:attribute name="status">
	    <xsl:value-of select="."/>
	  </xsl:attribute>
	</xsl:if>
      </xsl:when>
      <xsl:when test="parent::del">
	<xsl:if test="not(translate(.,$uc,$lc) ='unremarkable')">
	  <xsl:attribute name="status">
	    <xsl:value-of select="."/>
	  </xsl:attribute>
	</xsl:if>
      </xsl:when>
      <xsl:otherwise>
	<xsl:attribute name="status">
	  <xsl:value-of select="."/>
	</xsl:attribute>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>
  
  
  <xsl:template match="@place">
    <xsl:if test="not(translate(.,$uc,$lc) ='unspecified')">
      <xsl:attribute name="place">
	<xsl:value-of select="."/>
      </xsl:attribute>
    </xsl:if>
  </xsl:template>
  
  
  <xsl:template match="@sample">
    <xsl:if test="not(translate(.,$uc,$lc) ='complete')">
      <xsl:attribute name="sample">
	<xsl:value-of select="."/>
      </xsl:attribute>
    </xsl:if>
  </xsl:template>
  
  
  <xsl:template match="@org">
    <xsl:if test="not(translate(.,$uc,$lc) ='uniform')">
      <xsl:attribute name="org">
	<xsl:value-of select="."/>
      </xsl:attribute>
    </xsl:if>
  </xsl:template>
  
  <!-- do some normalisation -->
  <xsl:template match="@type">
    <xsl:variable name="normal">
      <xsl:value-of select="translate(translate(.,'-',''),$uc,$lc)"/>
    </xsl:variable>

    <xsl:variable name="unsub">
      <xsl:choose>
        <xsl:when test="$normal='subsection'">section</xsl:when>
        <xsl:when test="$normal='subsubsection'">section</xsl:when>
        <xsl:when test="$normal='subsubsubsection'">section</xsl:when>
        <xsl:when test="$normal='subsubsubsubsection'">section</xsl:when>
        <xsl:when test="$normal='subsubsubsubsubsection'">section</xsl:when>
        <xsl:when test="$normal='subsubsubsubsubsubsection'">section</xsl:when>
        <xsl:when test="$normal='poem'">verse</xsl:when>
        <xsl:when test="$normal='poetry'">verse</xsl:when>
        <xsl:when test="$normal='acknowledgement'">acknowledgements</xsl:when>
        <xsl:when test="$normal='reference'">references</xsl:when>
        <xsl:when test="$normal='notes'">note</xsl:when>
        <xsl:when test="$normal='chpater'">chapter</xsl:when>
        <xsl:when test="$normal='front cover'">cover</xsl:when>
        <xsl:when test="$normal='back cover'">cover</xsl:when>
        <xsl:when test="$normal='author index'">index</xsl:when>
        <xsl:when test="$normal='subject index'">index</xsl:when>
        <xsl:when test="$normal='illus'">illustration</xsl:when>
        <xsl:when test="$normal='article text'">article</xsl:when>
        <xsl:when test="$normal='cutting from catalogue'">cutting</xsl:when>
        <xsl:when test="contains(., ' ')"><xsl:value-of select="."/></xsl:when>
        <xsl:otherwise>
          <xsl:value-of select="$normal"/>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:variable>
    
      <xsl:attribute name="type">
	<xsl:value-of select="$unsub"/>
      </xsl:attribute>
  </xsl:template>

  <xsl:template match="@type[contains(.,' ')][not (parent::*/@n)][not (normalize-space(.)='')]">
      <xsl:attribute name="n">
	<xsl:value-of select="."/>
      </xsl:attribute>
  </xsl:template>

  <!-- eat empty tyep attributes -->
  <xsl:template match="@type[normalize-space(.)='']">     
  </xsl:template>

  <xsl:template match="title/@type[.='245']">
      <xsl:attribute name="type">marc245</xsl:attribute>
  </xsl:template>
  
  <!-- yes|no to boolean -->
  
  <xsl:template match="@anchored">
    <xsl:attribute name="anchored">
      <xsl:choose>
	<xsl:when test="translate(.,$uc,$lc)='yes'">true</xsl:when>
	<xsl:when test="translate(.,$uc,$lc)='no'">false</xsl:when>
      </xsl:choose>
    </xsl:attribute>
  </xsl:template>
  
  <xsl:template match="sourceDesc/@default"/>
  
  <xsl:template match="@tei">
    <xsl:attribute name="tei">
      <xsl:choose>
	<xsl:when test="translate(.,$uc,$lc)='yes'">true</xsl:when>
	<xsl:when test="translate(.,$uc,$lc)='no'">false</xsl:when>
      </xsl:choose>
    </xsl:attribute>
  </xsl:template>
  
  <xsl:template match="textClass/@default|biblFull/@default">
    <xsl:attribute name="default">
      <xsl:choose>
	<xsl:when test="translate(.,$uc,$lc)='yes'">true</xsl:when>
	<xsl:when test="translate(.,$uc,$lc)='no'">false</xsl:when>
      </xsl:choose>
    </xsl:attribute>
  </xsl:template>
  
  <xsl:template match="@langKey"/>  
  
  <xsl:template match="@TEIform"/>  

<!-- assorted atts -->
  <xsl:template match="@old"/>  

  <xsl:template match="@mergedin">  
    <xsl:attribute name="mergedIn">
      <xsl:value-of select="."/>
    </xsl:attribute>
  </xsl:template>

<!-- deal with the loss of div0 -->  

  <xsl:template match="div0|div1|div2|div3|div4|div5|div6">
    <div>
      <xsl:apply-templates select="*|@*|processing-instruction()|comment()|text()"/>
    </div>
  </xsl:template>

<!-- from Conal Tuohy -->
<xsl:template match="orig[@reg]">
  <choice>
    <orig><xsl:apply-templates select="*|@*|processing-instruction()|comment()|text()"/></orig>
    <reg><xsl:value-of select="@reg"/></reg>
  </choice>
</xsl:template>

<xsl:template match="reg[@orig]">
  <choice>
    <reg><xsl:apply-templates select="*|@*|processing-instruction()|comment()|text()"/></reg>
    <orig><xsl:value-of select="@orig"/></orig>
  </choice>
</xsl:template>

<xsl:template match="@orig|@reg"/>

<!-- Start of Stuart Yeates's stuff -->

<xsl:template match="name[not(@key)]">
  <name>
    <xsl:apply-templates select="@*"/>
    <xsl:choose>
      <xsl:when test="@reg">
        <choice>
          <orig><xsl:apply-templates select="*|processing-instruction()|comment()|text()"/></orig>
          <reg><xsl:value-of select="@reg"/></reg>
       </choice>
      </xsl:when>
      <xsl:otherwise>
        <xsl:apply-templates select="*|processing-instruction()|comment()|text()"/>
      </xsl:otherwise>
    </xsl:choose>
  </name>
</xsl:template>

<!-- schemes are now URLs  -->
<xsl:template match="@scheme[contains (., 'nzetc-subjects')]">
  <xsl:attribute name="scheme">http://www.nzetc.org/nzetc-subjects</xsl:attribute>
</xsl:template>

<xsl:template match="@scheme[contains (., 'nzetc-mads')]">
  <xsl:attribute name="scheme">http://www.nzetc.org/nzetc-mads</xsl:attribute>
</xsl:template>

<xsl:template match="keywords[not(@scheme)]">
  <keywords>
  <xsl:attribute name="scheme">http://www.example.org/folksonomy</xsl:attribute>
    <xsl:apply-templates select="@*|*|processing-instruction()|comment()|text()"/>
  </keywords>
</xsl:template>

<!-- floatingText tag -->
	  
<xsl:template match="text[not(parent::TEI|parent::TEI.2|parent::group)]">
  <floatingText>
    <xsl:apply-templates select="@*|*|processing-instruction()|comment()|text()"/>
  </floatingText>
</xsl:template>

<xsl:template match="p[text][not(parent::quote)]|div[text][not(parent::quote)]">
  <figure>
    <xsl:apply-templates  select="@*|*|processing-instruction()|comment()|text()"/>
  </figure>
</xsl:template>
  

<xsl:template match="p[text][parent::quote]|div[text][parent::quote]">
  <xsl:apply-templates  select="*|processing-instruction()|comment()|text()"/>
</xsl:template>
  


<xsl:template match="figDesc/hi">
  <emph>
    <xsl:apply-templates select="@*|*|processing-instruction()|comment()|text()"/>
  </emph>
</xsl:template>

<!-- create addSpans, ugly, ugly, ugly -->
<xsl:template match="add[p]">
  <xsl:variable name="count" select="count(preceding::add[p])"/>
  <addSpan>
    <xsl:attribute name="spanTo">#addSpan-<xsl:value-of select="$count"/></xsl:attribute>
    <xsl:apply-templates select="@*"/>
  </addSpan>
  <xsl:apply-templates select="*|processing-instruction()|comment()|text()"/>
  <anchor>
    <xsl:attribute name="xml:id">addSpan-<xsl:value-of select="$count"/></xsl:attribute>
  </anchor>
</xsl:template>

<!-- eat empty lists-->
<xsl:template match="list[not(node())]"/>

<!-- eat empty titles-->
<xsl:template match="title[not(text())][not(child::*)]"/>

<!-- eat empty page numbers -->
<xsl:template match="pb/@n[.='']|div/@n[.='']"/>



<!-- fix an ugly cludge -->
  <xsl:template match="xref/@type[starts-with(.,'http://')]">
    <xsl:attribute name="target">
      <xsl:value-of select="."/>
    </xsl:attribute>
  </xsl:template>
  

<!-- move pbs from listBibls into bibls-->
  <xsl:template match="bibl[parent::listBibl][following-sibling::*[1][local-name()='pb']]">
    <xsl:element  name="bibl">
      <xsl:apply-templates select="*|@*|processing-instruction()|comment()|text()"/>
      <xsl:apply-templates select="following-sibling::pb[1]"/>
    </xsl:element>
  </xsl:template>

  <xsl:template match="listBibl/pb"/>


  <!-- don't allow p's in change tags -->
  <xsl:template match="change/item/p">
      <xsl:apply-templates select="*|@*|processing-instruction()|comment()|text()"/>
  </xsl:template>

</xsl:stylesheet>

Personal tools