[XSL-LIST Mailing List Archive Home] [By Thread] [By Date]

[xsl] Can exclude-result-prefixes alter the qualified name of an element?


Subject: [xsl] Can exclude-result-prefixes alter the qualified name of an element?
From: Carl Worth <cworth@xxxxxxxxxx>
Date: Fri, 28 Jan 2005 14:56:25 -0500

I've got an application in which the input tree (in namespace "input"
say) is allowed to contain sub-trees to appear directly in the output
tree (in namespace "output"). After the transformation, only nodes
from the output namespace will appear in the result.

As an example, I might have as input:

	<?xml version="1.0" ?>
	<i xmlns="input" xmlns:o="output">
	  <o:e/>
	</i>

and desired output:

	<?xml version="1.0" ?>
	<o xmlns="output">
	  <e/>
	</o>

The transformation I came up with is:

	<?xml version="1.0" ?>
	<xsl:stylesheet version="1.0"
	                xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
	                xmlns:i="input"
	                xmlns:o="output"
	                exclude-result-prefixes="i o"
	                >
	  <xsl:template match="i:i">
	    <o:o xmlns="output">
	      <xsl:apply-templates/>
	    </o:o>
	  </xsl:template>

	  <xsl:template match="o:e">
	    <xsl:copy-of select="."/>
	  </xsl:template>

	</xsl:stylesheet>

An earlier version did not have the namespace declaration on the
literal <o:o> element, nor did it have the exclude-result-prefixes
attribute. That earlier version gave me a valid and correct result[*]:

	<?xml version="1.0"?>
	<o:o xmlns:o="output" xmlns:i="input">
	  <o:e/>
	</o:o>

But this is not as clean as what I set out for originally. It's got an
extraneous namespace declaration and unnecessary use of a prefix for
what could be the default namespace.

If I take the stylesheet as above, but using just a value of "o" for
exclude-result-prefixes, I get quite close to the desired result:

	<?xml version="1.0"?>
	<o xmlns="output" xmlns:i="input">
	  <e/>
	</o>

That's perfect except for one extraneous namespace declaration. And
the XSLT specification explicitly says that getting rid of something
like this is just what exclude-result-prefixes is designed for. But
when I use exclude-result-prefixes="i o", (in other words, using the
stylesheet exactly as quoted above), I get the following unexpected
result:

	<?xml version="1.0"?>
	<o xmlns="output">
	  <e xmlns="input"/>
	</o>

By simply excluding an unused namespace, the qualified name of the "e"
element has changed(!) from a namespace of "output" to "input", the
very namespace I was trying to exclude.

Is this a bug in this implementation, or can someone explain to me how
this behavior is justified by the specification?

-Carl

[*] All example output cited here was produced with xsltproc:

	$ xsltproc --version
	Using libxml 20616, libxslt 10111 and libexslt 809
	xsltproc was compiled against libxml 20614, libxslt 10111 and libexslt 809
	libxslt 10111 was compiled against libxml 20614
	libexslt 809 was compiled against libxml 20614


[**] The following resources on namespaces were quite helpful:

	http://www.dpawson.co.uk/xsl/sect2/N5536.html#d6408e184

Particularly the explanations of namespace nodes and the behavior of
copy-of with respect to namespace nodes.


Current Thread
Keywords