RE: [xsl] [XSL] Adding namespaces to output?

Subject: RE: [xsl] [XSL] Adding namespaces to output?
From: "Michael Kay" <mike@xxxxxxxxxxxx>
Date: Wed, 18 Mar 2009 10:22:21 -0000
> I've encountered a little problem (or more exactly two). I 
> want to do an XSL-transformation where I want to add two 
> namespaces to the root tag of the resulting output file. I 
> want the output to look like this:
> <scxml xmlns="http://www.w3.org/2005/07/scxml";
> xmlns:xs="http://commons.apache.org/scxml";
> version="1.0"
> initialstate="INIT">
> 
> I've tried to write for example:
> <xsl:element name="scxml" namespace="http://commons.apache.org/scxml";>
> but that just gives me the output
> <cs:scxml xmlns:cs="http://commons.apache.org/scxml";>
> which obviously is wrong.

Well, no, it's not obviously wrong. The choice of prefix is supposed to be
arbitrary and in XSLT 1.0 it's essentially the job of the processor to
decide what prefix to allocate. Most processors here would give you what
you're after, but in XSLT 1.0 it's not required (it is in 2.0).

If you wrote <scxml xmlns="http://commons.apache.org/scxml";> then it's
probably more likely that the processor would respect your choice of prefix,
but even then, in XSLT 1.0 the processor can override your choice.

However, you said you wanted the scxml element to be in the namespace
http://www.w3.org/2005/07/scxml, and your instruction is trying to put it in
namespace http://commons.apache.org/scxml - I'm not sure if this is just a
typo.

> Also, I don't know how to add two different namespaces? If 
> someone could help me with these two questions I would be 
> very grateful!

The two namespaces in your example play very different roles. One is the
namespace URI of the element - part of the element's name. The rule here is
to make sure you get the name of the element right (that is, URI + local
name), and the namespace declaration will look after itself. The other
namespace is apparently (as far as the system can tell) unused, and so you
need positive action to generate it in the output. The easiest way is with a
literal result element:

<scxml xmlns="http://www.w3.org/2005/07/scxml";
       xmlns:xs="http://commons.apache.org/scxml";
       version="1.0"
       initialstate="INIT">

Or in XSLT 2.0, you can use xsl:namespace.

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

Current Thread