[xsl] Namespaces and the identity transform

Subject: [xsl] Namespaces and the identity transform
From: "James Carlyle" <james.carlyle@xxxxxxxxxxxx>
Date: Thu, 17 Oct 2002 18:01:16 +0100
I have a simple document:
<?xml version="1.0"?>
<doc>
<body>
	<div id="header">
		<h1>TakePart</h1>
	</div>
</body>
</doc>

I want to produce a strict xhtml document and use the following stylesheet:
<?xml version="1.0" ?>
<xsl:stylesheet version="1.0"
	xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
xmlns="http://www.w3.org/1999/xhtml";>
<xsl:output method="xml"
	doctype-public="-//W3C//DTD XHTML 1 Strict//EN"
	doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"/>

<xsl:template match="@*|node()">
	<xsl:copy>
		<xsl:apply-templates select="@*|node()"/>
	</xsl:copy>
</xsl:template>

<xsl:template match="doc">
	<html>
		<xsl:apply-templates select="@*|node()"/>
	</html>
</xsl:template>
</xsl:stylesheet>

What I get is
<?xml version="1.0" encoding="UTF-16"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd";>
<html xmlns="http://www.w3.org/1999/xhtml";>
	<body xmlns="">
		<div id="header"><h1>TakePart</h1></div>
	</body>
</html>

This is correct, since the html output element has the default namespace,
whereas the body element outputted through the identity transform has no
namespace.  I get the null namespace on any elements passing through the
identity transform.

How can I design an identity transform that applies the default namespace to
the output?  I prefer not to have to restructure my input, and I want all
output nodes to use the strict xhtml namespace.  I substituted the above
identity transform with the following and this seems to produce the correct
output, but is this the most efficient way of doing it?

<xsl:template match="*">
	<xsl:element name="{name()}">
		<xsl:apply-templates select="@*|node()"/>
	</xsl:element>
</xsl:template>

<xsl:template match="@*">
	<xsl:attribute name="{name()}">
		<xsl:value-of select="."/>
	</xsl:attribute>
</xsl:template>


Many thanks,

James Carlyle


 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Current Thread