RE: [xsl] getting rid of namespaces

Subject: RE: [xsl] getting rid of namespaces
From: "Michael Kay" <mike@xxxxxxxxxxxx>
Date: Mon, 30 Jul 2007 09:01:01 +0100
If you create elements in the correct namespace, the namespace declarations
will take care of themselves. Your problem is that you want the <html>
element to be in no namespace, but you have generated it in the XHTML
namespace. This appears to be because you generated it by copying an element
from the source document. <xsl:copy-of> will always copy the expanded name
of an element unchanged from the source to the result; if you want to rename
the element you need to use <xsl:element>.

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

> -----Original Message-----
> From: David J Birnbaum [mailto:djbpitt+xml@xxxxxxxx] 
> Sent: 29 July 2007 23:11
> To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
> Subject: [xsl] getting rid of namespaces
> 
> Dear XSL List,
> 
> I'm trying to get rid of a pesky namespace declaration, and 
> I've tried to follow the rules (as I ... er ... understand 
> them), but without success. Can someone please advise me on 
> where my blind spot lies? 
> Here's a sample XML document instance:
> 
> <?xml version="1.0" encoding="UTF-8"?>
> <root>
>     <p>A paragraph in no namespace</p>
>     <html xmlns="http://www.w3.org/1999/xhtml";>
>         <head>
>             <title>Title in html namespace</title>
>         </head>
>         <body>
>             <p>A paragraph in the html namespace</p>
>         </body>
>     </html>
> </root>
> 
> Here's the XSLT:
> 
> <?xml version="1.0" encoding="UTF-8"?>
> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
>     xmlns:html="http://www.w3.org/1999/xhtml"; version="2.0" 
> exclude-result-prefixes="#all">
>     <xsl:template match="/">
>         <html>
>             <head>
>                 <title>Test of removing namespaces</title>
>             </head>
>             <body>
>                 <xsl:apply-templates/>
>             </body>
>         </html>
>     </xsl:template>
>     <xsl:template match="p">
>         <p>
>             <xsl:apply-templates/>
>         </p>
>     </xsl:template>
>     <xsl:template match="html:html/html:body/html:*">
>         <xsl:copy-of select="." copy-namespaces="no"/>
>     </xsl:template>
>     <xsl:template match="html:html/html:head"/> </xsl:stylesheet>
> 
> I specify exclude-result-prefixes="#all" on the 
> <xsl:stylesheet> node and I specify copy-namespaces="no" on 
> the <xsl:copy-of> instruction that copies all elements in the 
> body the html document that is embedded in my XML source. The 
> first of these strategies makes the namespace declaration on 
> the paragraph in no namespace go away (so far, so good), but 
> the second does not suppress the unwanted namespace 
> declaration on the embedded <html:p>. Here's the output of a 
> Saxon 8.9.0.4J transformation (I've edited out some unwanted 
> white space):
> 
> <html>
>    <head>
>       <meta http-equiv="Content-Type" content="text/html; 
> charset=UTF-8">
>       <title>Test of removing namespaces</title>
>    </head>
>    <body>
>         <p>A paragraph in no namespace</p>
>         <p xmlns="http://www.w3.org/1999/xhtml";>A paragraph 
> in the html 
> namespace</p>
>    </body>
> </html>
> 
> What should I be doing to get rid of the xhtml namespace declaration?
> 
> Thanks!
> 
> Best,
> 
> David
> djbpitt+xml@xxxxxxxx

Current Thread