<?xml version="1.0" encoding="UTF-8"?>
    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" xmlns="http://www.tei-c.org/ns/1.0" 
        xmlns:tei="http://www.tei-c.org/ns/1.0">
        <xsl:output method="xml" version="1.0" encoding="utf-8" />
        
        <!-- 
            
            This file is used to convert
            XML files previously created by the Image Markup Tool version 1.7 into 
            XML files conforming to the imt_1_8 TEI schema used for version 1.8.
            
            Changes are:
	    1. Change the version identifiers in the schema links.
	    2. Add a new <ptr> to the <application> element specifying the xml:id
	       of the new <facsimile> element.
	    3. Replace the <svg:svg> element and its children with a new 
	       <facsimile> element.
	
            August 2008
            
            LICENSE
            
            The contents of this file are subject to the Mozilla Public License Version
            1.1 (the "License"); you may not use this file except in compliance with
            the License. You may obtain a copy of the License at
            "http://www.mozilla.org/MPL/"
            
            Software distributed under the License is distributed on an "AS IS" basis,
            WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
            the specific language governing rights and limitations under the License.
            
            The Original Code is "[imt_1_7_to_imt_1_8.xsl]". 
            
            The Initial Developer of the Original Code is Martin Holmes (Victoria,
            BC, Canada, "http://www.mholmes.com/"). Copyright (C) 2008 Martin Holmes
            and the University of Victoria Computing and Media Centre. The code was
            co-developed for university and personal projects, and rights are shared
            by Martin Holmes and the University of Victoria. All Rights Reserved.
            
        -->

        <xsl:template match="/">
            <!-- Add return (some processors don't provide one automatically). -->
            <xsl:text>
            </xsl:text>
                <xsl:apply-templates />
        </xsl:template> 
        
        <!-- We need to modify a couple of processing instructions which link
        to the schema. We should only do this if the previous schema was 
        an IMT schema. -->
        <!-- First, the oasis-schema PI according to Makato Murata's proposal at
                http://www.asahi-net.or.jp/~eb2m-mrt/hidden/spec.html. -->
        <xsl:template match="processing-instruction('oasis-schema')">
            <xsl:text>
            </xsl:text>
            <xsl:choose>
                <xsl:when test="contains(., 'imt_1_7.rng')">
                    <xsl:processing-instruction name="oasis-schema"> href="imt_1_8.rng" type="application/xml"</xsl:processing-instruction>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:copy-of select="." />
                </xsl:otherwise>
            </xsl:choose>
        </xsl:template>
        
        <!-- Next, the oXygen PI. Allow for typo in previous version (oxygena). -->
        <xsl:template match="processing-instruction('oxygen') | processing-instruction('oxygena')">
            <xsl:text>
            </xsl:text>
            <xsl:choose>
                <xsl:when test="contains(., 'imt_1_7.rng')">
                <xsl:processing-instruction name="oxygen"> RNGSchema="imt_1_8.rng" type="xml"</xsl:processing-instruction>
            </xsl:when>
                <xsl:otherwise>
                    <xsl:copy-of select="." />
                </xsl:otherwise>
            </xsl:choose>
            <xsl:text>
            </xsl:text>
        </xsl:template>
       
<!--       Add the missing hash before the @facs attribute.-->
        <xsl:template match="tei:div[@type='imtAnnotation']/@facs[not(starts-with(., '#'))]">
            <xsl:attribute name="facs">#<xsl:value-of select="." /></xsl:attribute>
        </xsl:template>
        
        <!-- Copy everything else as-is. -->
        <xsl:template match="node()|@*">
            <xsl:copy>
                <xsl:apply-templates select="node()|@*"/>
            </xsl:copy>
        </xsl:template>
        
        
</xsl:stylesheet>
