[xsl] Re: Can one _generate_ namespace nodes?

Subject: [xsl] Re: Can one _generate_ namespace nodes?
From: Joseph Kesselman <keshlam@xxxxxxxxxx>
Date: Mon, 24 Feb 2003 09:35:04 -0500
>Can you provide a *small* example, please?

Smallest would be a conceptual sketch rather than an example.

Let's say we want to insert <xsl:text>foo</xsl:text> into the middle of 
our output document. One approach is to code it using  a "dummy" 
namespace, and translate that on output to the correct namespace::
        <xsl:namespace-alias stylesheet-prefix="newxsl" 
and have XSLT automatically translate the dummy namespace into the "real" 
xsl namespace. I've tended to prefer this approach for clarity reasons.

The downside of the aliasing solution is that it may produce more verbose 
output. Some XSLT processors -- Xalan in particular -- keep the original 
prefix and just change the namespace binding. This is actually nice in 
some ways, since it means you can see which names were generated by the 
aliasing mechanism. But when you combine that with a serializer which 
generates namespace declarations at the point where it discovers they were 
used without having been explicitly declared, that means the output 
... and that namespace declaration occurs on every one of these generated 
<newxsl:text/> elements.

A fix would be to make sure the newxsl: prefix is bound higher in the 
generated document, since most serializers are smart enough not to 
re-declare prefixes already having the proper value.  For example, adding 
a namespace declaration for newxsl: to the root <xsl:stylesheet> element 
would do the trick.

But in the XPath data model, namespace declarations are "namespace nodes", 
and XSLT appears to have no mechanism for explicitly generating a 
namespace node into the output document. <xsl:attribute> is explicitly 
prevented from doing so (though the rule as stated is no longer fully 
effective, given later errata published for the Namespaces REC). 

Of course there's a workaround for this case
        <xsl:element name="xsl:text" 
But  aliasing is *not* the only time we might want to explicitly add a 
namespace declaration. Since prefixes are being used in XPaths, we may 
find that in order to correctly generate an XPath in an output document we 
need to generate a declaration for one or more of the prefixes it contains 
-- and in general, XML serializers do *not* examine the content of strings 
to try to automatically bind prefixes they contain, since there's no 
general way to tell (for example) whether "nntp:"; is a Namespace Prefix or 
a URI Scheme. Here too, we need a way to assert a new namespace binding 
explicitly from the stylesheet... and I haven't yet found a way to express 
that in XSLT.

Am I missing something obvious? Or is there a gap in the XSLT design?

Joe Kesselman, IBM Next-Generation Web Technologies:
XML, XSL and more.  "may'ron DaroQbe'chugh vaj bIrIQbej" 
("Put down the squeezebox and nobody gets hurt.")

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

Current Thread