Re: [xsl] strip-spaces

Subject: Re: [xsl] strip-spaces
From: "Andrew Welch" <andrew.j.welch@xxxxxxxxx>
Date: Wed, 6 Feb 2008 10:17:47 +0000
On 05/02/2008, Michael Kay <mike@xxxxxxxxxxxx> wrote:
> I haven't followed this thread closely, I thought a lot of people had been
> telling you what you did wrong. The most common reason is doing something
> like
>
> <xsl:template match="parent">
>   <div xmlns="namespace1">
>     <xsl:apply-templates select="child"/>
>   </div>
> </xsl:template>
>
> <xsl:template match="parent">
>   <p>
>     <xsl:apply-templates select="child"/>
>   </p>
> </xsl:template>
>
> and the fix is to generate the <p> element in the same namespace as its
> parent, which can be achieved using
>
> <p xmlns="namespace1">


Another example which might help (and help confirm it in my own mind):

<xsl:template match="/">
    <parent xmlns="foo">
      <xsl:apply-templates/>	
    </parent>
</xsl:template>

<xsl:template match="child">
	<child/>	
</xsl:template>

you expect the output to be:

<parent xmlns="foo">
  <child/>
</parent>

but instead the output is:

<parent xmlns="foo">
  <child xmlns=""/>
</parent>

and you want to get rid of the xmlns="" on <child>...

The reason why it's there is because <child> is in no namespace, and
the reason it is in no namespace is because in the stylesheet:

<xsl:template match="/">
  <parent xmlns="foo">
    <xsl:apply-templates/>	
  </parent>
</xsl:template>

<xsl:template match="child">
  <child/>	
</xsl:template>

...<child> is in a different scope to <parent> (even though in your
mind <parent> contains <child>) - you have to see the XSLT as XML to
know which namespace child will be in, and looking at it like that its
clear that <child> is not a descendant or <parent> and not covered by
its xmlns declaration.

The solution is to put <child> in the namespace in the
XML-that-is-the-stylesheet, which can be done by declaring the
namespace on the element itself, or by moving the namespace
declaration to common ancestor - like the root element xsl:stylesheet

Is that an accurate view of whats going on?



-- 
Andrew Welch
http://andrewjwelch.com
Kernow: http://kernowforsaxon.sf.net/

Current Thread