[xsl] Default namespace in result document

Subject: [xsl] Default namespace in result document
From: Phil Endecott <spam_from_xslt_list@xxxxxxxxxxxx>
Date: Wed, 07 Dec 2005 18:35:10 +0000
(I had some problem sending this earlier; appologies if you see it twice.)

Dear All,

I have been using XSLT successfully for a few years now but have never
tried to make namespaces work, mainly because at least one component in
the system has not supported them properly.  I've now tried using them
for the first time and although I get a result document that is correct
it's not quite what I had expected.  Here's an example:

[XML input:]

<?xml version="1.0"?>
<foo xmlns="http://example.com/foo";>


<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";

<xsl:output method="xml" indent="yes"/>

  <xsl:template match="/">
      <xsl:apply-templates mode="copy"/>

  <xsl:template match="*" mode="copy">
      <xsl:apply-templates mode="copy"/>


So the stylesheet contains literal result elements in namespace foo, and also copies elements from the input XML that are in namespace foo. I was hoping to get something like this:

<?xml version="1.0"?>
<root xmlns="http://example.com/foo";>

i.e. the default namespace is defined on the root element, and no
namespace things are used anywhere else.  But instead, using libxslt
version 1.1.15, I get this:

<?xml version="1.0"?>
<foo:root xmlns:foo="http://example.com/foo";>
  <foo xmlns="http://example.com/foo";>

This is not ideal because it makes files much larger than they need to
be.  I guessed that there would be some xsl:output attribute to select a
default namespace but I can't see anything that does exactly this.  I
can't help feeling that I've missed something obvious.  Can someone
suggest what I need to do?

(I assume that this is also an issue for people trying to generate
"HTML-compatible XSLT" - not something I have tried to do yet...)

Many thanks,


Current Thread