[XSL-LIST Mailing List Archive Home] [By Thread] [By Date]

RE: [xsl] removing a null namespace


Subject: RE: [xsl] removing a null namespace
From: "Michael Kay" <mike@xxxxxxxxxxxx>
Date: Fri, 29 Apr 2005 13:42:44 +0100

You need to rename the elements in the source document: the namespace
component of the element names was null, and you want it to be
http://www.w3.org/1999/xhtml. xsl:copy-of does an exact copy, it can't be
used to make changes such as renaming the elements. So you need a variant of
the identity transform that renames all elements:

<xsl:template match="*" mode="rename">
  <xsl:element name="{local-name()}"
namespace="http://www.w3.org/1999/xhtml">
    <xsl:copy-of select="namespace::*"/>  <!-- you may not need this line
-->
    <xsl:copy-of select="@*"/>
    <xsl:apply-templates mode="rename"/>
  </xsl:element>
</xsl:template>

Michael Kay
http://www.saxonica.com/


> -----Original Message-----
> From: Gavin Montague [mailto:lists@xxxxxxxxxxxxxxx] 
> Sent: 29 April 2005 13:04
> To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
> Subject: [xsl] removing a null namespace
> 
> Hi,
> 
> I'd be grateful if someone could give me an answer/better solution to 
> my problem....
> 
> I have a document which contains, in part, some fragments of xhtml.  
> I'm using copy-of to pass these elements through to the final xhtml 
> document unaltered but I find they are having a null 
> namespace added.  
> Here are some cut down's that will hopefully explain what I mean...
> 
> /////////////////////////  xsl ///////////////////////////
> <?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-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"
>      doctype-public="-//W3C//DTD XHTML 1.0 Strict//EN" indent="yes"
> />
> <xsl:template match="/">
> <html lang="en" xml:lang="en">
> <head>
> 	  <meta http-equiv="Content-Type" 
> content="application/xhtml+xml; 
> charset=UTF-8" />
> 		<xsl:apply-templates select="document/metadata" />
> 		<link rel="stylesheet" type="text/css" 
> href="/resources/sample.css" />
> 	</head>
> 	<body>
> 	<div id="container">
> 		<xsl:apply-templates 
> select="document/content/markdown" />				
> 		</div>
> 		</body>
> </html>
> </xsl:template>
> 
> <xsl:template match="markdown">
> <xsl:copy-of  select="child::*"/>
> </xsl:template>
> </xsl:stylesheet>
> /////////////////////////  output ///////////////////////////
> <?xml version="1.0" encoding="UTF-8"?>
> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" 
> "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
> <html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
>    <head>
>      <meta http-equiv="Content-Type" content="application/xhtml+xml; 
> charset=UTF-8"/>
>      <link rel="stylesheet" type="text/css" 
> href="/resources/sample.css"/>
>    </head>
>    <body>
>      <div id="container">
> 
>        <h1 xmlns="">header</h1>
>        <p xmlns="">Lorem <em>ipsum</em> dolor sit amet, consectetuer 
> adipiscing elit. Sed at justo id eros mollis lobortis. Donec 
> dignissim. 
> Aenean arcu. Nulla ultrices. Ut nec urna sed turpis elementum 
> vehicula. 
> Donec pretium mollis urna. Pellentesque pulvinar dui et erat. 
> Donec sit 
> amet dolor. Proin non dolor non nulla ullamcorper bibendum. 
> Sed tempor 
> ligula quis risus. Praesent a sapien nec leo imperdiet 
> ultricies. Etiam 
> ac justo. Maecenas euismod, dui ac aliquam tincidunt, erat sem 
> hendrerit purus, eu dignissim sem sem sed odio. Suspendisse at leo. 
> Phasellus vitae mauris et arcu malesuada aliquam. Mauris et pede. 
> Vestibulum et augue.</p>
>      </div>
>    </body>
> </html>
> //////////////////////////
> The xml document (not included here, but the relevant section is just 
> plain <h1>header</h1><p>lipusm....</p>.
> 
> As I understand it doing a copy-of will include the namespace of the 
> elements and, as there is no namespace in the original xml, 
> this is "".
> 
> My problem is, how can I stop this being carried over into the 
> transformed document?  I can't use exclude-result-prefixes to exclude 
> "", unless I'm mistaken.
> 
> Any help would be gratefully received.
> 
> Thanks,
>     Gavin


Current Thread
Keywords