Re: [xsl] XSL to XSL stylesheet: namespace question

Subject: Re: [xsl] XSL to XSL stylesheet: namespace question
From: Jeni Tennison <mail@xxxxxxxxxxxxxxxx>
Date: Wed, 14 Feb 2001 16:05:45 +0000
Hi Viewga,

> could anyone give me an insight on how to make correct transform
> from xsl to xsl.
>
> Here is an example (the bad thing is that it makes ns0:stylesheet
> xmlns:ns0="http://www.w3.org/1999/XSL/Transform"; and thus xsl goes
> as undeclared ns):

The prefix that you use for a namespace doesn't logically matter -
'ns0' is just as valid a prefix as 'xsl' and if a stylesheet uses that
prefix, then it will still be a usable stylesheet.

The reason that this new namespace prefix is created is because you've
specified the namespace for the 'stylesheet' element with the
'namespace' attribute on xsl:element, rather than letting the
processor expand the name given by the 'name' attribute.  If you use
just:

   <xsl:element name="xsl:stylesheet">
      <xsl:attribute name="version">1.0</xsl:attribute>
      <xsl:element name="xsl:output">
         <xsl:attribute name="indent">yes</xsl:attribute>
      </xsl:element>
      <xsl:apply-templates />
   </xsl:element>

then you have a better chance of getting the namespace prefix that you
want (i.e. 'xsl').

However, because using xsl:element and xsl:attribute all the time is a
little long-winded, there's another way of doing this using literal
result elements. The usual pattern for creating a stylesheet with a
stylesheet is to declare a namespace that acts as an alias for the
XSLT namespace within the stylesheet, and then use an
xsl:namespace-alias instruction to tell the processor that this
aliasing is going on.

So, your stylesheet would look something like:

<xsl:stylesheet version="1.0"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
                xmlns:adt="http://somewhere.domain/ADT";
                xmlns:oxsl="http://www.w3.org/1999/XSL/TransformAlias";>

<xsl:output indent="yes" version="1.0" media-type="text/xsl" />

<xsl:namespace-alias stylesheet-prefix="oxsl" result-prefix="xsl" />

<xsl:template match="adt:root">
   <oxsl:stylesheet version="1.0">
      <oxsl:output indent="yes" />
      <xsl:apply-templates />
   </oxsl:stylesheet>
</xsl:template>

<xsl:template match="*|@*">
   <xsl:copy>
      <xsl:apply-templates select="@*|node()"/>
   </xsl:copy>
</xsl:template>

</xsl:stylesheet>

Note that processors often keep the stylesheet prefix (e.g. 'oxsl')
for the elements that you create like this rather than using the
prefix of the namespace that you're mapping to (e.g. 'xsl').  If you
want the output to use 'xsl' as the prefix, then the stylesheet has to
use something aside from 'xsl' as the prefix for its instructions.

Also note that any *copied* elements in the XSLT namespace will
probably use their original prefix from the source document (e.g.
'xsl'), so you may end up with two namespace declarations, with
different prefixes, for the XSLT namespace.

None of this matters to the processor, but it might matter to you in
terms of readability of the result.

I hope that helps,

Jeni

---
Jeni Tennison
http://www.jenitennison.com/



 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Current Thread