[xsl] Re: Problem with namespaces when transforming one namespace to another.

Subject: [xsl] Re: Problem with namespaces when transforming one namespace to another.
From: Dimitre Novatchev <dnovatchev@xxxxxxxxx>
Date: Mon, 27 May 2002 10:49:26 -0700 (PDT)
--- "Sal Mangano" <smangano at ureach dot com> wrote:
> 
> There is something I am not understanding about the behavior of
> namespaces.
> 
> Imagine I have the following XML:
> 
> <foo:someElement xmlns:foo="http://www.somewhere.com/namespace/foo";;
> xmlns:doc="http://www.somewhere.com /namespace/doc">
>   <foo:aChild>
>     <foo:aGrandChild/>
>     <foo:aGrandChild>
>       <doc:doc>This documentation should not be removed or altered in
> any way.</doc:doc>
>     </foo:aGrandChild>
>   </foo:aChild>
> </foo:someElement>
> 
> And I want to change all elements in the foo namespace to a bar
> namespace but leave all other elements untouched. . I use the
> following
> stylesheet.
> 
> 
> <xsl:stylesheet version="1.0"
> xmlns:xsl="http://www.w3.org/1999/XSL/Transform";;
>  xmlns:foo="http://www.somewhere.com/namespace/foo";;
>  xmlns:bar="http://www. somewhere.com/namespace/bar">
> 
> <xsl:output method="xml" version="1.0" encoding="UTF-8"
> indent="yes"/>
> <xsl:strip-space elements="*"/>
> 
> <xsl:template match="/ | node() | @* | comment() |
> processing-instruction()">
>   <xsl:copy>
>     <xsl:apply-templates select="@* | node()"/>
>   </xsl:copy>
> </xsl:template>
> 
> <xsl:template match="foo:*">
>   <xsl:element name="bar:{local-name()}">
>     <xsl:apply-templates/>
>   </xsl:element>
> </xsl:template>	
> 
> </xsl:stylesheet>
> 
> 
> The output is:
> 
> <?xml version="1.0" encoding="UTF-8"?>
> <bar:someElement xmlns:bar="http://www.somewhere.com/namespace/bar";;>
>    <bar:aChild>
>       <bar:aGrandChild/>
>       <bar:aGrandChild>
>          <doc:doc xmlns:doc="http:/www.somewhere.com/namespace/doc"
> xmlns:foo="http:/www.somewhere.c
> om/namespace/foo">This documentation should not be removed or altered
> in
> any way.</doc:d
> oc>
>       </bar:aGrandChild>
>    </bar:aChild>
> </bar:someElement>
> 
> Which is fine except that the doc element retains the foo namespace
> which seems to server no purpose anymore and I would prefer to be
> stripped.
> 
> Suggestions?
>  

Hi Sal,

First of all, the match pattern you're using is ambiguous:

match="/ | node() | @* | comment() | processing-instruction()"

is the same as:

match="node() | @*"


The next thing to note, is that xsl:copy copies all namespace nodes an
element has -- therefore, in case you want to get rid of some of them
(in your case of the foo namespace), then you should use something
else.

For example:

  <xsl:element name="{name()}">
    <xsl:copy-of select="namespace::*[name() != 'foo']"/>
    <xsl:apply-templates/>
  </xsl:element>

Bellow is the complete stylesheet:

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
 xmlns:foo="http://www.somewhere.com/namespace/foo";
 xmlns:bar="http://www. somewhere.com/namespace/bar"
 xmlns:doc="http://www.somewhere.com /namespace/doc">


<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>

<xsl:template match="node()[not(self::*)] | @* ">
  <xsl:copy>
    <xsl:apply-templates select="@* | node()"/>
  </xsl:copy>
</xsl:template>

<xsl:template match="*">
  <xsl:element name="{name()}">
    <xsl:copy-of select="namespace::*[name() != 'foo']"/>
    <xsl:apply-templates/>
  </xsl:element>
</xsl:template>	

<xsl:template match="foo:*">
  <xsl:element name="bar:{local-name()}">
    <xsl:apply-templates/>
  </xsl:element>
</xsl:template>	

</xsl:stylesheet>

Do note, that I have introduced a separate template matching element
nodes. In fact it will be applied on all elements that do not belong to
the foo namespace. It is exactly within this template, that elements
are stripped off their foo namespace node, in case they have such.

When applied on your original source xml document:

<foo:someElement xmlns:foo="http://www.somewhere.com/namespace/foo";
xmlns:doc="http://www.somewhere.com /namespace/doc">
  <foo:aChild>
    <foo:aGrandChild/>
    <foo:aGrandChild>
      <doc:doc>This documentation should not be removed or altered in
any way.</doc:doc>
    </foo:aGrandChild>
  </foo:aChild>
</foo:someElement>


The result of the above transformation is:

<?xml version="1.0" encoding="UTF-8"?>
<bar:someElement xmlns:bar="http://www. somewhere.com/namespace/bar">
   <bar:aChild>
      <bar:aGrandChild/>
      <bar:aGrandChild>
         <doc:doc xmlns:doc="http://www.somewhere.com
/namespace/doc">This documentation should not be removed or altered in
any way.</doc:doc>
      </bar:aGrandChild>
   </bar:aChild>
</bar:someElement>


Hope this helped.

Cheers,
Dimitre Novatchev.



__________________________________________________
Do You Yahoo!?
Yahoo! - Official partner of 2002 FIFA World Cup
http://fifaworldcup.yahoo.com

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


Current Thread