Re: Redundant namespace declarations in the output tree

Things will be clearer if you distinguish between what happens during
result tree construction from what happens during output.  During result
tree construction, namespace nodes are copied into the result for
literal result elements and for xsl:copy, but not for xsl:element and
xsl:attribute.  They are not copied for xsl:attribute because an
attribute doesn't have namespace nodes (copying them could also lead to
conflicts). They are not copied for xsl:element, so as to provide a way
to the user not to copy namespace nodes.  They are copied for literal
result elements and for xsl:copy so that XSLT can be used to generate
things like XSLT which make use of namespace declarations in attributes
or content.  During output, there is always the requirement to generate
an XML document that conforms to the namespaces recommendation. Thus
namespace declarations must be added during output if necessary to
ensure that there is a declaration whenever an element or attribute uses
it (that's why it's not a problem that xsl:attribute doesn't copy
namespace nodes). Secondly, there is a requirement that if there is a
namespace node on an element, then there must be a corresponding
namespace declaration in scope on the tag.  The converse is not required
because the Namespaces declaration does not allow the undeclaring of
namespaces except for the default namespace.

One thing to be careful of is that with xsl:attribute, you sometimes
need to rename prefixes. For example, given:

 <x:foo xmlns:x="http://www.jclark.com/x1">
   <xsl:attribute name="x:bar" xmlns:x="http://www.jclark.com/x2"/>
 </x:foo>

one possible output would be

 <x:foo ns0:bar="" xmlns:x="http://www.jclark.com/x1"
xmlns:ns0="http://www.jclark.com/x2"/>

Kay Michael wrote:
> 
> SAXON currently never outputs a namespace declaration unless there is an
> element or attribute name that uses it.
> 
> My understanding of the latest spec is that on a literal result element I am
> required to output redundant namespace declarations (unless they are
> suppressed using xsl:exclude-result-prefixes), while on elements written
> using xsl:element I am allowed to output redundant namespace declarations,
> but am not required to do so: and further, there is no way for the user to
> control whether I do so or not, except by the global exclude-result-prefixes
> on xsl:stylesheet. Is this correct?
> 
> Reading xsl:copy again I see this also says that when copying an element
> node I should output all its namespace nodes including those that might only
> be needed for attributes that are not copied, and again there seems to be no
> way for the user to suppress it except at the stylesheet level.
> 
> I can see why it might sometimes make sense to output redundant namespace
> declarations (e.g. when the result document is a stylesheet) but I can't see
> why the three cases are different.
> 
> Futher, for a literal result element, the spec says that all attributes
> except those in the XSLT namespace are copied, and that all namespaces
> except those in the XSLT and extension element namespaces are copied. This
> means that if there is an attribute that uses an extension element prefix,
> the attribute is copied but its namespace is not. This must be wrong.
> 
> Mike Kay

Received on Thursday, 4 November 1999 21:26:43 UTC