RE: [xsl] empty namespace declaration being generated

Subject: RE: [xsl] empty namespace declaration being generated
From: "Scott Smith" <ssmith@xxxxxxxxxxxxxxxxxx>
Date: Tue, 23 Jan 2007 23:07:03 -0700
First, you're right about Xalan (1.1; the xerces c++ library is 2.7.0).

Second, I hear what you are saying.  What I don't understand is why ALL of the
tags that pulled data from the no-namespace xml file didn't also have the
xmlns="".  In the original xsl, I pull 20 some odd data tags from the original
xml file and the only one that caused the xmlns="" was this one.

What I want is that all of the elements, attributes, etc. in the output xml
file are under the www.fred.com/something namespace.

Can someone point me to an FAQ or tutorial article that explains the affect of
different placements of the namespace declaration?

Thanks for all of the help.

Scott



________________________________

From: David Bertoni [mailto:david.bertoni@xxxxxxxxx]
Sent: Tue 1/23/2007 7:46 PM
To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
Subject: Re: [xsl] empty namespace declaration being generated



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