Difference between revisions of "Dot-two.xslt"
Jump to navigation
Jump to search
Stuartyeates (talk | contribs) |
|||
| (6 intermediate revisions by 4 users not shown) | |||
| Line 1: | Line 1: | ||
| − | |||
This stylesheet actually does two things: | This stylesheet actually does two things: | ||
* converts <TEI.2> to <TEI> and <teiCorpus.2> to <teiCorpus>; and | * 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 | Note that the root element is usually <TEI> or <teiCorpus>, but does | ||
not have to be. | not have to be. | ||
| Line 13: | Line 12: | ||
<!-- Tiny XSLT 1.0 stylesheet that reads in a P4 TEI (XML) file --> | <!-- 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 --> | <!-- and writes out the same with <teiCorpus.2> and <TEI.2> elements --> | ||
| − | <!-- changed to <teiCorpus> and <TEI>; the root element | + | <!-- 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 --> | <!-- Copyleft 2006 Syd Bauman and the Text Encoding Initiative Consortium --> | ||
| Line 24: | Line 26: | ||
<!-- (e.g., 'tei:div'). --> | <!-- (e.g., 'tei:div'). --> | ||
| − | <!-- The output of this stylesheet is not P5- | + | <!-- The output of this stylesheet is not P5-compliant XML; it's just --> |
<!-- one step closer than the input. --> | <!-- one step closer than the input. --> | ||
<!-- Written 2006-05-05 by Syd Bauman --> | <!-- 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" | <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" | ||
| − | xmlns:tei="http://www.tei-c.org/ns/1.0" | + | xmlns:tei="http://www.tei-c.org/ns/1.0" version="1.0"> |
| − | + | ||
| − | + | <xsl:output method="xml" encoding="UTF-8" cdata-section-elements="eg"/> | |
| − | <xsl:output method="xml" | + | |
| − | |||
| − | |||
| − | |||
<!-- Get the the version number, if any, of the eventual output --> | <!-- Get the the version number, if any, of the eventual output --> | ||
<!-- P5 document. --> | <!-- P5 document. --> | ||
<xsl:param name="version"/> | <xsl:param name="version"/> | ||
| − | + | ||
| − | <!-- | + | <!-- generic copy of elements, into new namespace --> |
| − | <xsl:template match="@*| | + | <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:copy> | ||
| − | <xsl:apply-templates | + | <xsl:apply-templates/> |
</xsl:copy> | </xsl:copy> | ||
</xsl:template> | </xsl:template> | ||
| − | + | ||
<!-- special template for the root element — it gets a namespace declaration --> | <!-- special template for the root element — it gets a namespace declaration --> | ||
<!-- and maybe a version= attribute --> | <!-- and maybe a version= attribute --> | ||
| Line 56: | Line 66: | ||
<xsl:value-of select="substring-before(local-name(.),'.2')"/> | <xsl:value-of select="substring-before(local-name(.),'.2')"/> | ||
</xsl:when> | </xsl:when> | ||
| − | <xsl:otherwise><xsl:value-of select="local-name(.)"/></xsl:otherwise> | + | <xsl:otherwise> |
| + | <xsl:value-of select="local-name(.)"/> | ||
| + | </xsl:otherwise> | ||
</xsl:choose> | </xsl:choose> | ||
</xsl:variable> | </xsl:variable> | ||
| − | <xsl:element name="{$newName}" | + | <xsl:element name="{$newName}" namespace="http://www.tei-c.org/ns/1.0"> |
| − | |||
<!-- copy over all my attributes --> | <!-- copy over all my attributes --> | ||
<xsl:copy-of select="attribute::*"/> | <xsl:copy-of select="attribute::*"/> | ||
| Line 75: | Line 86: | ||
</xsl:if> | </xsl:if> | ||
<!-- apply templates for all my content --> | <!-- apply templates for all my content --> | ||
| − | <xsl:apply-templates select="./*|./text()|./processing-instruction()|./comment()"/> | + | <xsl:apply-templates |
| + | select="./*|./text()|./processing-instruction()|./comment()"/> | ||
</xsl:element> | </xsl:element> | ||
</xsl:template> | </xsl:template> | ||
| − | + | ||
<!-- special template for child TEI.2 elements --> | <!-- special template for child TEI.2 elements --> | ||
<xsl:template match="/teiCorpus.2/TEI.2"> | <xsl:template match="/teiCorpus.2/TEI.2"> | ||
| − | <xsl:element name="TEI"> | + | <xsl:element name="TEI" namespace="http://www.tei-c.org/ns/1.0"> |
<!-- copy over all my attributes & content --> | <!-- copy over all my attributes & content --> | ||
| − | <xsl:copy-of select="@*|./*|./text()|./processing-instruction()|./comment()"/> | + | <xsl:copy-of |
| + | select="@*|./*|./text()|./processing-instruction()|./comment()"/> | ||
</xsl:element> | </xsl:element> | ||
</xsl:template> | </xsl:template> | ||
</xsl:stylesheet> | </xsl:stylesheet> | ||
</nowiki></pre> | </nowiki></pre> | ||
| + | [[User:Syd|Syd]] 06:50, 26 May 2006 (BST) | ||
| + | [[Category:P4toP5]] | ||
| + | [[Category:XSLT]] | ||
Latest revision as of 08:14, 14 March 2010
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)