NotesToRefs.xsl

From TEIWiki
Revision as of 01:16, 13 February 2011 by Filologanoga (talk | contribs) (New page: This is a stylesheet from the Croatiae auctores Latini TEI XML collection. For publishing under PhiloLogic, the TEI XML should conform to [http://sites.google.com/site/philologic3/encoding...)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

This is a stylesheet from the Croatiae auctores Latini TEI XML collection. For publishing under PhiloLogic, the TEI XML should conform to PhiloLogic specifications; this stylesheet reformats notes in the text, adding (empty) ref elements, creating attributes both for ref and note as required, and moving all note elements to a separate div in the back.

 
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:tei="http://www.tei-c.org/ns/1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <!-- title: ref for notes, moves notes to back div -->
    <!-- author: Neven Jovanović, Croatiae auctores Latini -->
    <!-- description: adds ref elements in front of notes elements -->
    <!-- description: adds xml:id, target and resp attributes to notes -->
    <!-- description: adds type, xml:id, target and n attributes to refs -->
    <!-- description: moves notes to a separate div in the back -->
    <!-- description: makes possible linking from refs to notes and vice versa -->
    <!-- filename: movingnotes.xsl -->
    <!-- modified: 2011-02-12 -->
    <!-- required input: tei xml file, using a CroALa subset (or similar), with notes and pbs (for linking back) -->
    <!-- expected output: tei xml file according to PhiloLogic specification -->
    <!-- known restrictions: resp hardwired to auctor; ref type should be changed to "nota" for CroALa -->
    <!-- movingnotes.xsl -->
 

    <!-- copy all... -->
    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>
    <!-- ... but the note elements, for which call the scribrefs template to edit -->
    <xsl:template match="//tei:note">
        <xsl:call-template name="scribrefs"/>
    </xsl:template>
    <!-- test if the last child is named "back" -->
        <xsl:template match="//tei:text/*[last()]">
            <xsl:choose>
                <xsl:when test="tei:back">
                    <xsl:call-template name="backref"/>
                    
                </xsl:when>
                <xsl:otherwise>
                    
                    <xsl:copy>
                        <xsl:apply-templates select="@*|node()"/>
                        <!-- create a back element -->
                        <xsl:element namespace="http://www.tei-c.org/ns/1.0" name="back">
                            <!-- create a div with a type atrribute = notes -->
                            <xsl:element name="div" namespace="http://www.tei-c.org/ns/1.0">
                                <xsl:attribute name="type">notes</xsl:attribute>
                                <!-- give it a Latin heading  -->
                                <xsl:element name="head" namespace="http://www.tei-c.org/ns/1.0">
                                    <xsl:text>Notae</xsl:text>
                                </xsl:element>
                                <!-- Philologic says a pb element should be here -->
                                <xsl:element name="pb" namespace="http://www.tei-c.org/ns/1.0">
                                    <xsl:attribute name="n">nts</xsl:attribute>
                                </xsl:element>
                                <!-- put inside it all notes from the document -->
                                <xsl:for-each select="//tei:note">
                                    <xsl:call-template name="scribnot"/>
                                </xsl:for-each>
                            </xsl:element>
                        </xsl:element>
                    </xsl:copy>
                </xsl:otherwise>
            </xsl:choose>
        </xsl:template>
    
    
<xsl:template name="scribrefs">
    <!--  which we replace with empty refs, citing carefully the TEI namespace -->
    <xsl:element name="ref" namespace="http://www.tei-c.org/ns/1.0">
        <!-- structure: typed as notae (Latin), have ids (beginning with ref),  -->
        <!--  have targets (beginning with n); -->
        <!--  notes will have ids and targets the other way around -->
        <!-- the attributes should really have xml:id; -->
        <!-- have to check if this agrees with the philosubs.pl of PhiloLogic.  -->
        <xsl:attribute name="type">nota</xsl:attribute>
        <xsl:attribute name="xml:id">
            <!-- ids begin with ref -->
            <xsl:text>r</xsl:text>
            <!-- Here we generate an identifier -->
            <!-- "if you call generate-id() more than once in one run with the same 
                node as an argument, the processor generates the same ID value each 
                time for that node." DuCharme, XML Quickly, p. 244-245. -->
            <!-- Without this feature it would be tough connecting refs with their respective notes.  -->
            <xsl:value-of select="generate-id(.)"/>
        </xsl:attribute>
        <xsl:attribute name="target">
            <xsl:text>n</xsl:text>
            <xsl:value-of select="generate-id(.)"/>
        </xsl:attribute>
        <xsl:attribute name="n">
            <xsl:number/>
        </xsl:attribute>
    </xsl:element>
</xsl:template>
<xsl:template name="backref" match="//tei:back">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
        
        
        <!-- we introduce a new div, typed as notes -->
        <xsl:element name="div" namespace="http://www.tei-c.org/ns/1.0">
            <xsl:attribute name="type">notes</xsl:attribute>
            <!-- with a Latin heading  -->
            <xsl:element name="head" namespace="http://www.tei-c.org/ns/1.0">
                <xsl:text>Notae</xsl:text>
            </xsl:element>
            <!-- inside it go all notes from the document -->
            <xsl:for-each select="//tei:note">
                <xsl:call-template name="scribnot"/>
            </xsl:for-each>
        </xsl:element>
    </xsl:copy>
</xsl:template>
    <xsl:template name="scribnot">
        <xsl:copy>
            <!-- their id begins with n, their targets with ref -->
            <!--  (vice versa compared to refs inside text) -->
            <xsl:attribute name="place">foot</xsl:attribute>
            <!-- otherwise, almost the same as with ref elements (except notes are not empty) -->
            <xsl:attribute name="xml:id">
                <xsl:text>n</xsl:text>
                <xsl:value-of select="generate-id(.)"/>
            </xsl:attribute>
            <xsl:attribute name="target">
                <xsl:text>r</xsl:text>
                <xsl:value-of select="generate-id(.)"/>
            </xsl:attribute>
            <!-- after we select and copy everything enclosed in note, -->
            <xsl:apply-templates select="@*|node()"/>
            <!-- add a carriage return, for tidiness -->
        </xsl:copy><xsl:text>
        </xsl:text>
    </xsl:template>
    
</xsl:stylesheet>