RE: [xsl] Namespaces and the identity transform

Subject: RE: [xsl] Namespaces and the identity transform
From: "Michael Kay" <michael.h.kay@xxxxxxxxxxxx>
Date: Tue, 29 Oct 2002 19:21:14 -0000
You're doing it right. If you want to produce an output element whose
name is computed from the name of an input element, but isn't identical
(e.g., in your case, it has the same local name but a different
namespace), then xsl:element is the way to do it.

Michael Kay
Software AG
home: Michael.H.Kay@xxxxxxxxxxxx
work: Michael.Kay@xxxxxxxxxxxxxx 

> -----Original Message-----
> From: owner-xsl-list@xxxxxxxxxxxxxxxxxxxxxx 
> [mailto:owner-xsl-list@xxxxxxxxxxxxxxxxxxxxxx] On Behalf Of 
> James Carlyle
> Sent: 17 October 2002 18:01
> To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
> Subject: [xsl] Namespaces and the identity transform
> 
> 
> 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
> 


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


Current Thread