Dot-two.xslt

From TEIWiki
Jump to navigation Jump to search

This stylesheet actually does two things:

  • converts <TEI.2> to <TEI> and <teiCorpus.2> to <teiCorpus>; and
  • moves all elements from no namespace to the TEI namespace.

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>; if a $version parameter was -->
<!-- specified, it is applied as an attribute on the output root -->
<!-- element. The input should not be namespaced (i.e., as with TEI -->
<!-- P4 documents, all the elements should be in no namespace); the -->
<!-- output will be in the normal TEI P5 namespace. -->

<!-- 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-compliant XML; it's just -->
<!-- one step closer than the input. -->

<!-- Written 2006-05-05 by Syd Bauman -->
<!-- Updated 2006-12-18 by Syd Bauman: -->
<!--         Create namespaces properly with namespace=; explicitly copy -->
<!--         elements into new namespace. -->

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

  <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"/>

  <!-- generic copy of elements, into new namespace -->
  <xsl:template match="*">
    <xsl:variable name="myLocNam" select="local-name()"/>
    <xsl:element name="{$myLocNam}" namespace="http://www.tei-c.org/ns/1.0">
      <xsl:apply-templates/>
    </xsl:element>
  </xsl:template>

  <!-- generic copy of everything else -->
  <xsl:template match="@*|text()|comment()|processing-instruction()">
    <xsl:copy>
      <xsl:apply-templates/>
    </xsl:copy>
  </xsl:template>

  <!-- 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}" namespace="http://www.tei-c.org/ns/1.0">
      <!-- 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" namespace="http://www.tei-c.org/ns/1.0">
      <!-- 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)