<?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"
        xmlns:svg="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
        <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.4 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.
	    4. Change date/@value to @when.
	    5. Change the appInfo tag and its contents to match changes in TEI P5.
	    6. Change the contents of rendition tags used to encode annotation categories.
	    7. Remove xsi:schemaLocation attribute.
	    8. Remove any legacy appInfo blocks.
	
            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_4_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_4.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_4.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>
        
       
        <!-- Application information: TEI has now formalized a structure for this, so we 
        have to convert our previous ad-hoc structure to the new one. -->
        <xsl:template match="tei:appInfo">
            <xsl:element name="appInfo">
                <xsl:element name="application">
                    <xsl:attribute name="ident"><xsl:value-of select="@xml:id" /></xsl:attribute>
                    <xsl:attribute name="version"><xsl:value-of select="normalize-space(tei:appDetail[@adType='appVersion'])" /></xsl:attribute>
                    <xsl:attribute name="notAfter"><xsl:value-of select="tei:appDetail[@adType='lastSave']/tei:date/@value" /></xsl:attribute>
                    <xsl:element name="label">
                        <xsl:value-of select="normalize-space(tei:appDetail[@adType='appName'])" />
                    </xsl:element>
                    <xsl:for-each select="tei:appDetail[@adType='sectionsTouched']/tei:ptr">
                        <xsl:copy-of select="." />
                    </xsl:for-each>
                    <!-- Add a new pointer to the header <application> element for the <facsimile> element. -->
                    <xsl:element name="ptr">
                        <xsl:attribute name="target">#imtImageAnnotations</xsl:attribute>
                    </xsl:element>
                </xsl:element>
            </xsl:element>
        </xsl:template>
       
        <!-- Now we need to convert the SVG stuff over to a facsimile element. -->
        <!-- First, we suppress the original div containing the SVG element and the annotations. -->
        <xsl:template match="tei:div[@type='imtAnnotatedImage']"></xsl:template>
        
        <!-- Now we intercept processing of the text element, and inject a new 
        facsimile element in front of it. -->
        <xsl:template match="tei:text">
            <xsl:text>
            </xsl:text>
            <xsl:element name="facsimile">
                <xsl:attribute name="xml:id">imtAnnotatedImage</xsl:attribute>
                <!-- Grab all the SVG info and convert it to create a facsimile structure. -->
                <xsl:variable name="svgEl" select="//svg:svg[ancestor::tei:div[@type='imtAnnotatedImage']][1]" />
                <!-- Create a single surface element. -->
                <xsl:element name="surface">
                    <!-- Create a graphic element. -->
                    <xsl:element name="graphic">
                        <xsl:attribute name="url"><xsl:value-of select="$svgEl/svg:image/@xlink:href" /></xsl:attribute>
                        <xsl:attribute name="width"><xsl:value-of select="$svgEl/svg:image/@width" />px</xsl:attribute>
                        <xsl:attribute name="height"><xsl:value-of select="$svgEl/svg:image/@height" />px</xsl:attribute>
                    </xsl:element>
                    
                    <!-- Now create zone elements for each of the rects in the svg. -->
                    <xsl:for-each select="$svgEl/svg:rect">
                        <xsl:element name="zone">
                            <!-- First, get the id attribute. -->
                            <xsl:attribute name="xml:id"><xsl:value-of select="@id" /></xsl:attribute>
                            <!-- Next, get the class attribute which points at one of the IMT
                                annotation categories. This will have to be an @n attribute 
                                now. -->
                            <xsl:attribute name="rendition"><xsl:value-of select="@class" /></xsl:attribute>
                            <!-- Now the coordinates of the zone. We have a little 
                                translation to do here, because we're moving from 
                                ltwh to ltrb. -->
                            <xsl:attribute name="ulx"><xsl:value-of select="@x" /></xsl:attribute>
                            <xsl:attribute name="uly"><xsl:value-of select="@y" /></xsl:attribute>
                            <xsl:attribute name="lrx"><xsl:value-of select="@x + @width" /></xsl:attribute>
                            <xsl:attribute name="lry"><xsl:value-of select="@y + @height" /></xsl:attribute>
                            <!-- This is an unpleasant hack; <zone> doesn't have any 
                                appropriate attribute we can use for visibility. -->
                            <xsl:if test="@visibility">
                                <xsl:attribute name="rend"><xsl:value-of select="@visibility" /></xsl:attribute>
                            </xsl:if>
                        </xsl:element>
                    </xsl:for-each>
                </xsl:element>
            </xsl:element>
            <xsl:text>
            </xsl:text>
            <!-- Now process the text element. -->
            <xsl:element name="text">
                <xsl:apply-templates />
            </xsl:element>
        </xsl:template>
        
        <!-- In the body handling, we need to create a new div and copy the annotations
            into it. -->
        <xsl:template match="tei:body">
            <xsl:element name="body">
                <xsl:element name="div">
                    <xsl:attribute name="xml:id">imtImageAnnotations</xsl:attribute>
                    <xsl:for-each select="//tei:div[@type='imtAnnotation']">
                        <xsl:element name="div">
                           <xsl:attribute name="type">imtAnnotation</xsl:attribute>
                            <xsl:attribute name="facs">#<xsl:value-of select="@n"/></xsl:attribute>
                            <xsl:copy-of select="./*" />
                        </xsl:element>
                    </xsl:for-each>
                </xsl:element>
                
                <!-- Now handle anything else that happens to be in the body. -->
                <xsl:apply-templates />
            </xsl:element>
        </xsl:template>
        
        <!-- Change date/@value to @when. -->
        <xsl:template match="tei:date/@value">
            <xsl:attribute name="when"><xsl:value-of  select="." /></xsl:attribute>
        </xsl:template>
        
        <!-- Annotation category information. This also has to be changed due to changes 
        in TEI P5. -->
        <xsl:template match="tei:tagsDecl[@xml:id='imtAnnotationCategories']">
            <xsl:element name="tagsDecl">
                <xsl:attribute name="xml:id">imtAnnotationCategories</xsl:attribute>
                <xsl:for-each select="tei:rendition">
                    <xsl:element name="rendition">
                        <xsl:copy-of select="@xml:id" />
                        <xsl:copy-of select="tei:label" />
                        <xsl:element name="code">
                            <xsl:attribute name="lang">text/css</xsl:attribute>
                            <xsl:attribute name="rend"><xsl:value-of select="tei:hi/@rend" /></xsl:attribute>
                            <xsl:value-of select="tei:hi" />
                        </xsl:element>
                    </xsl:element>
                </xsl:for-each>
            </xsl:element>
        </xsl:template>
        
        <!-- Explicitly suppress the xsi:schemaLocation attribute in the root TEI element. -->
        <xsl:template match="@xsi:schemaLocation"></xsl:template>        
        
        <!-- Remove legacy appInfo blocks. -->
        <xsl:template match="tei:appInfo[@xml:id='ImageMarkupTool1']"></xsl:template>
        
        <!-- Copy everything else as-is. -->
        <xsl:template match="node()|@*">
            <xsl:copy>
                <xsl:apply-templates select="node()|@*"/>
            </xsl:copy>
        </xsl:template>
        
        
</xsl:stylesheet>
