Re: [xsl] empty namespace declaration being generated

Subject: Re: [xsl] empty namespace declaration being generated
From: David Bertoni <david.bertoni@xxxxxxxxx>
Date: Tue, 23 Jan 2007 18:46:26 -0800
Scott Smith wrote:
I apologize for not seeing it in the FAQ.  I did search for it before I
sent the email (did again after I saw your email), but didn't find
anything that looked like what I was seeing.  If could give me a url,
I'm happy to read it especially if after reading what's below you
believe it explains the issue.

<snip>

I'm using the latest version (2.7.0) of xalan-c, but will be using a MS
xslt library in production which I think means I'm stuck at xslt 1.0.

There is no such version of Xalan-C. The latest version is 1.10.


Here's an xsl that demonstrates the problem.

<?xml version="1.0" ?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>
	<xsl:output method="xml" indent="yes" />
	
	<xsl:template match="/">
		<stocks xmlns="http://www.fred.com/something";>
			<xsl:apply-templates select="/Info/Stock" />
		</stocks>
	</xsl:template>

	<xsl:template match="Stock" />
	<xsl:template match="Stock[Type/@Name='tickerSymbol']">
		<security>
			<xsl:attribute name="exchange">
				<xsl:value-of
select="substring-before(Name, ':')" />
			</xsl:attribute>
			<xsl:attribute name="ticker">
				<xsl:value-of
select="substring-after(Name, ':')" />
			</xsl:attribute>
		</security>
	</xsl:template>
</xsl:stylesheet>

Here's the input (there's no namespace declaration in the source file):

<?xml version="1.0" encoding="utf-8"?>
<Info>
  <Stock>
    <Type Name="category" />
    <Name>stocks</Name>
  </Stock>
  <Stock>
    <Type Name="tickerSymbol" />
    <Name>NASDAQ:BMET</Name>
    <Description>Biomet, Inc.</Description>
  </Stock>
  <Stock>
    <Type Name="tickerSymbol" />
    <Name>NYSE:JNJ</Name>
    <Description>Johnson &amp;amp; Johnson.</Description>
  </Stock>
</Info>

The generated output file contains:

<?xml version="1.0" encoding="UTF-8"?>
<stocks xmlns="http://www.fred.com/something";>
    <security xmlns="" exchange="NASDAQ" ticker="BMET"/>
    <security xmlns="" exchange="NYSE" ticker="JNJ"/>
</stocks>


Which is correct. You generate a "stocks" element in the result tree using a literal result element in your stylesheet that has a namespace URI of "http://www.fred.com/something";. Then, you generate two "security" elements in the result using a literal result in in the stylesheet that have empty namespace URIs. To preserve the expanded name of the element, the serialization process generates default namespace declarations that ensure the elements have an empty namespace URI.


You need to decide exactly what you want. If you don't want the "stocks" element in the namespace "http://www.fred.com/something";, then remove the default namespace declaration. If you want the "security" elements in the same namespace as the "stocks" element, then add the default namespace declaration to the "security" literal result element. If you want all of the literal result elements in the stylesheet in the namespace "http://www.fred.com/something";, then you can move the default namespace declaration to the stylesheet element.

Dave

Current Thread