Re: [xsl] namespace inheritance

Subject: Re: [xsl] namespace inheritance
From: "J.Pietschmann" <j3322ptm@xxxxxxxx>
Date: Tue, 01 Oct 2002 21:59:43 +0200
Venkateshwar Bommineni wrote:
Hi,
 I have trouble with following xsl snippet. All child elements
inheriting namespace declaration from parent element. How can i suppress
that.

Actually, you seem to have exactly the opposite problem you belive to have.

> <Members xmlns="htp://www.foo.com/namespace/v1">
> 	<Member xmlns="">l.fname1</Member>

The "Members" element is in the namespace identified by
"htp://www.foo.com/namespace/v1", and you use the default
name space for it. Its child elements Are in no namespace,
and the processor generates XML which resets the assignment
of the default namespace to "no namespace", this is what
the xmlns="" does. This is 100% correct.
It seems you dont want to have the default namespace reset,
or equivalently, you want to have all elements in the same
namespace. If this is the case, just ensure all the elements
are created in the same namespace. The most simple way is to
declare the assignment of the default name space at the XSLT
top level and use literal result elements consistently:

 <?xml version="1.0"?>
 <xsl:stylesheet version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
  xmlns="htp://www.foo.com/namespace/v1"/>

 <xsl:template match="/Test">
   <Members>
     <xsl:apply-templates select="People"/>	
   </Members>
 </xsl:template>	

 <xsl:template match="People">
   <xsl:for-each select="Person">
     <Member>
       <xsl:value-of select="concat(substring(LName,1,1),'.',FName)"/>
     </Member>
   </xsl:for-each>
  </xsl:template>

</xsl:stylesheet>

Of course, you can still use <xsl:element>, just keep in mind
to always specify the desired namespace.

It is recommended to avoid assigning the default namespace,
there are a few unpleasant traps which can be avoided by
consistently using prefixed QNames for elements, for example
 <xsl:stylesheet version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
  xmlns:foo="htp://www.foo.com/namespace/v1"/>

 <xsl:template match="/Test">
   <foo:Members>
     <xsl:apply-templates select="People"/>	
   </foo:Members>
 </xsl:template>	

 <xsl:template match="People">
   <xsl:for-each select="Person">
     <foo:Member>
       <xsl:value-of select="concat(substring(LName,1,1),'.',FName)"/>
     </foo:Member>
   </xsl:for-each>
  </xsl:template>

</xsl:stylesheet>

J.Pietschmann


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



Current Thread