Re: [xsl] removing a specific namespace declaration

Subject: Re: [xsl] removing a specific namespace declaration
From: "G. Ken Holman" <gkholman@xxxxxxxxxxxxxxxxxxxx>
Date: Sat, 07 Nov 2009 20:29:07 -0500
At 2009-11-07 16:42 -0800, tom a wrote:
I'm trying to remove what I believe to be an unused namespace declaration from the output.

Unused namespace declarations are supposed to be benign to processing applications. Why is it you want to remove it?


eg:

Source:
<?xml version="1.0" encoding="ISO-8859-1"?>
<svg viewBox="0 0 360 792" xmlns="http://www.w3.org/2000/svg"; xmlns:xlink="http://www.w3.org/1999/xlink"; xmlns:foo="http://www.foo.com";>
<!-- svg content -->
</svg>


Becomes:
<?xml version="1.0" encoding="ISO-8859-1"?>
<svg  viewBox="0 0 360 792" xmlns="http://www.w3.org/2000/svg";
xmlns:xlink="http://www.w3.org/1999/xlink";>
<!-- svg content -->
</svg>

ie, the foo namespace is removed in the transformation.

I've seen many posts on removing all namespaces, but can't figure out how to remove a specific namespace.

In XSLT one neither removes all namespaces nor removes specific namespaces.


In XSLT and in XQuery one is always creating result trees from scratch using the information found in source trees.

To achieve what you want you need to reconstitute the elements and inhibit the copying of the foo namespace, rather than copying the elements, because copying an element has the effect of copying all attached namespace nodes.

I hope the example below helps.

. . . . . . . . . Ken

T:\ftemp>type toma.xml
<?xml version="1.0" encoding="ISO-8859-1"?>
<svg viewBox="0 0 360 792" xmlns="http://www.w3.org/2000/svg"; xmlns:xlink="http
://www.w3.org/1999/xlink" xmlns:foo="http://www.foo.com";>
<!-- svg content -->
</svg>


T:\ftemp>call xslt toma.xml toma.xsl
<?xml version="1.0" encoding="utf-8"?><svg xmlns="http://www.w3.org/2000/svg"; xm
lns:xlink="http://www.w3.org/1999/xlink"; viewBox="0 0 360 792">
<!-- svg content -->
</svg>
T:\ftemp>type toma.xsl
<?xml version="1.0" encoding="US-ASCII"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
version="1.0">


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

<xsl:template match="comment()|processing-instruction()|text()">
  <xsl:copy/>
</xsl:template>

</xsl:stylesheet>
T:\ftemp>

--
Upcoming:  hands-on XSLT, XQuery and XSL-FO Washington DC Nov 2009
Interested in other classes?  http://www.CraneSoftwrights.com/s/i/
Crane Softwrights Ltd.          http://www.CraneSoftwrights.com/s/
Training tools: Comprehensive interactive XSLT/XPath 1.0/2.0 video
Video lesson:    http://www.youtube.com/watch?v=PrNjJCh7Ppg&fmt=18
Video overview:  http://www.youtube.com/watch?v=VTiodiij6gE&fmt=18
G. Ken Holman                 mailto:gkholman@xxxxxxxxxxxxxxxxxxxx
Male Cancer Awareness Nov'07  http://www.CraneSoftwrights.com/s/bc
Legal business disclaimers:  http://www.CraneSoftwrights.com/legal

Current Thread