Re: [xsl] xmlns=""

Subject: Re: [xsl] xmlns=""
From: Andrew Welch <andrew.j.welch@xxxxxxxxx>
Date: Tue, 22 Feb 2011 14:19:29 +0000
On 22 February 2011 14:08, Merrilees, David
<David.Merrilees@xxxxxxxxxxxx> wrote:
> Hi
>
> When I add a default namespace to an element, it's direct descendants
acquire an xmlns="". Does anyone know why this is, or how to avoid it?
>
> Input:
>
> <?xml version="1.0" encoding="UTF-8"?>
> <html>
>        <head>
>                <title>Vmtejte</title>
>        </head>
>        <body id="home">
>        <body>
> </html>
>
> XSL:
>
> <xsl:template match="/html">
>        <html xmlns="http://www.w3.org/1999/xhtml";>
>                <xsl:sequence select="./node()"/>
>        </html>
> </xsl:template>
>
> Output:
>
> <?xml version="1.0" encoding="UTF-8"?>
> <html xmlns="http://www.w3.org/1999/xhtml";>
>        <head xmlns="">
>                <title>Vmtejte</title>
>        </head>

In your input xml the element is:

<html>
  <head>

so both elements are in no namespace.

In your transform you have:

  <html xmlns="http://www.w3.org/1999/xhtml";>

which is an element in the xhtml namespace, and all unprefixed
elements beneath that in the result tree will also be in the xhtml
namespace.

When you copy the unprefixed head element (that is in no namespace) to
the result tree, then you have 2 unprefixed elements, one in the xhtml
namespace and one in no namespace:

<html xmlns="http://www.w3.org/1999/xhtml";>
  <head xmlns="">

So the head element must have the xmlns="" to indicate its in no
namespace, rather than the xhtml namespace.

The solution is create new a head element in the xhtml namespace - you
cant copy it from the source as its name cant change.  To create the
new head, either write literally <head> in your transform or use the
element constructor.

To make things easier until the joys of default namespaces become
clearer, either use a prefix for the xhtml namespace in your transform
eg:

<x:html xmlns:x="http://www.w3.org/1999/xhtml";>

or put your input into a namespace:

<html xmlns="whatever">

...and then its a bit easier to see whats going on.

--
Andrew Welch
http://andrewjwelch.com

Current Thread