Dot-two.xslt

From TEIWiki
Revision as of 02:06, 2 July 2006 by Syd (talk | contribs) (tidy up some whitespace)
Jump to navigation Jump to search

This stylesheet actually does two things:

  • converts <TEI.2> to <TEI> and <teiCorpus.2> to <teiCorpus>; and
  • adds the appropriate namespace declaration to the root elment

Note that the root element is usually <TEI> or <teiCorpus>, but does not have to be.

This stylesheet takes a single optional parameter 'version', which is what will be used as the value of the version= attribute of <TEI> or <teiCorpus>. There is no default. If the parameter is not specified, no version= attribute is output.

<?xml version="1.0" encoding="UTF-8"?>
<!-- Tiny XSLT 1.0 stylesheet that reads in a P4 TEI (XML) file -->
<!-- and writes out the same with <teiCorpus.2> and <TEI.2> elements -->
<!-- changed to <teiCorpus> and <TEI>; the root element is given the -->
<!-- appropriate namespace declaration. -->

<!-- Copyleft 2006 Syd Bauman and the Text Encoding Initiative Consortium -->

<!-- Note that if you are using this stylesheet in a series of -->
<!-- transformations in order to go from P4 to near-P5, stylesheets -->
<!-- before this one probably can match on the local name only (e.g., -->
<!-- 'div'), stylesheets after this one must match on the qualified name -->
<!-- (e.g., 'tei:div'). -->

<!-- The output of this stylesheet is not P5-comnpliant XML; it's just -->
<!-- one step closer than the input. -->

<!-- Written 2006-05-05 by Syd Bauman -->

<xsl:stylesheet version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:tei="http://www.tei-c.org/ns/1.0">

  <xsl:import href="Copy-All.xsl"/>
  
  <xsl:output method="xml"
    encoding="UTF-8"
    cdata-section-elements="eg"/>
  
  <!-- Get the the version number, if any, of the eventual output -->
  <!-- P5 document. -->
  <xsl:param name="version"/>
  
  <!-- special template for the root element — it gets a namespace declaration -->
  <!-- and maybe a version= attribute -->
  <xsl:template match="/*">
    <xsl:variable name="newName">
      <xsl:choose>
        <xsl:when test="local-name(.)='TEI.2' or local-name()='teiCorpus.2'">
          <xsl:value-of select="substring-before(local-name(.),'.2')"/>
        </xsl:when>
        <xsl:otherwise><xsl:value-of select="local-name(.)"/></xsl:otherwise>
      </xsl:choose>
    </xsl:variable>
    <xsl:element name="{$newName}">
      <xsl:attribute name="xmlns">http://www.tei-c.org/ns/1.0</xsl:attribute>
      <!-- copy over all my attributes -->
      <xsl:copy-of select="attribute::*"/>
      <!-- if I am a <TEI.2> or a <teiCorpus.2> ... -->
      <xsl:if test="local-name()='TEI.2' or local-name()='teiCorpus.2'">
        <!-- ... and the user specified a version as a parameter ...-->
        <xsl:if test="not($version='')">
          <!-- ... put in a version= attribute ... -->
          <xsl:attribute name="version">
            <!-- ... with the users specified value -->
            <xsl:value-of select="$version"/>
          </xsl:attribute>
        </xsl:if>
      </xsl:if>
      <!-- apply templates for all my content -->
      <xsl:apply-templates select="./*|./text()|./processing-instruction()|./comment()"/>
    </xsl:element>
  </xsl:template>
  
  <!-- special template for child TEI.2 elements -->
  <xsl:template match="/teiCorpus.2/TEI.2">
    <xsl:element name="TEI">
      <!-- copy over all my attributes & content -->
      <xsl:copy-of select="@*|./*|./text()|./processing-instruction()|./comment()"/>
    </xsl:element>
  </xsl:template>
</xsl:stylesheet>

Syd 06:50, 26 May 2006 (BST)