I am experiencing something odd with the xslt transform that may be a real issue or may simply be my forgetting something. Anything that has xsl:apply-templates attached to it results in a blank space, and I don't understand why.
The xml I am using is as follows:
<?xml version="1.0" encoding="UTF-8"?>
<TEI.2>
<teiHeader>
<fileDesc>
<titleStmt>
<title>William of Palerne: An Electronic Edition</title>
<author>Anonymous</author>
<editor>Edited by G. H. V. Bunt</editor>
<respStmt>
<resp>
<hi rend="bold">Computer Consultants and Programmers</hi>
</resp>
<name> Susan Gants, Susan Munson, Daniel Pitti, and John Unsworth.</name>
</respStmt>
</titleStmt>
</fileDesc>
</teiHeader>
</TEI>
The xslt I am applying to is is as follows:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:tei="http://www.tei-c.org/ns/1.0"
exclude-result-prefixes="xs tei"
version="2.0">
<xsl:output method="xml" encoding="utf-8" omit-xml-declaration="yes" indent="no"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>
<xsl:template match="TEI.2">
<TEI xmlns="http://www.tei-c.org/ns/1.0">
<xsl:apply-templates/>
</TEI>
</xsl:template>
<xsl:template match="teiHeader">
<xsl:apply-templates/>
</xsl:template>
</xsl:stylesheet>
I expect my result to be the original XML with TEI.2 converted to TEI and the namespace added:
<?xml version="1.0" encoding="UTF-8"?>
<TEI xmlns="http://www.tei-c.org/ns/1.0">
<teiHeader>
<fileDesc>
<titleStmt>
<title>William of Palerne: An Electronic Edition</title>
<author>Anonymous</author>
<editor>Edited by G. H. V. Bunt</editor>
<respStmt>
<resp>
<hi rend="bold">Computer Consultants and Programmers</hi>
</resp>
<name> Susan Gants, Susan Munson, Daniel Pitti, and John Unsworth.</name>
</respStmt>
</titleStmt>
</fileDesc>
</teiHeader>
</TEI>
Instead, TEI.2 changes to TEI as expected but teiHeader does not appear:
<TEI xmlns="http://www.tei-c.org/ns/1.0">
<fileDesc xmlns="">
<titleStmt>
<title>William of Palerne: An Electronic Edition</title>
<author>Anonymous</author>
<editor>Edited by G. H. V. Bunt</editor>
<respStmt>
<resp>
<hi>Computer Consultants and Programmers</hi>
</resp>
<name> Susan Gants, Susan Munson, Daniel Pitti, and John Unsworth.</name>
</respStmt>
</titleStmt>
</fileDesc>
</TEI>
I'm sure I've made an error or am overlooking something, but I cannot for the life of me figure out what it is. IF someone could tell me what's messing me up and how to rectify it I would appreciate it.
The teiHeader
does not appear because of this template:
<xsl:template match="teiHeader">
<xsl:apply-templates/>
</xsl:template>
You are matching teiHeader
, but once matched you are not copying it, but instead passing on control to its child nodes, resulting in no teiHeader
being written in the output.
Now, you could simply remove this template, and the teiHeader
will be created. However, you will see it will be output like so
<teiHeader xmlns="">
This is because in the input XML the teiHeader
does not belong to any namespace, and so the identity template is simply creating the same element in the output in no namespace either.
Even though you are creating the root TEI
element in the namespace, this won't automatically add any child elements you create to that namespace. You need to add separate templates for that.
Try this XSLT
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:tei="http://www.tei-c.org/ns/1.0"
exclude-result-prefixes="xs tei"
version="2.0">
<xsl:output method="xml" encoding="utf-8" omit-xml-declaration="yes" indent="no"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*" />
</xsl:copy>
</xsl:template>
<xsl:template match="TEI.2">
<TEI xmlns="http://www.tei-c.org/ns/1.0">
<xsl:apply-templates/>
</TEI>
</xsl:template>
<xsl:template match="*" priority="2">
<xsl:element name="{local-name()}" namespace="http://www.tei-c.org/ns/1.0">
<xsl:apply-templates select="node()|@*" />
</xsl:element>
</xsl:template>
</xsl:stylesheet>
The priority="2"
is used to ensure the template gets higher priority than the identity template.
Also note how it does <xsl:apply-templates select="node()|@*" />
and not <xsl:apply-templates />
because the latter only selects nodes, and not attibrutes.