This is the mail archive of the
xsl-list@mulberrytech.com
mailing list .
Re: Re: Re: Removing namespaces from source document (long)
- From: Dimitre Novatchev <dnovatchev at yahoo dot com>
- To: xsl-list at lists dot mulberrytech dot com
- Date: Mon, 8 Apr 2002 03:25:07 -0700 (PDT)
- Subject: [xsl] Re: Re: Re: Removing namespaces from source document (long)
- Reply-to: xsl-list at lists dot mulberrytech dot com
I don't think it is a good idea to strip elements of all namespaces
they don't immediately use. This ends up with re-defining the same
namespace over and over again on many different elements -- both
difficult to understand and to change/maintain.
For example, the following xml source:
<root xmlns:good-ns="http://good.com" xmlns:bad-ns="http://bad.com">
<parent color="red">
<good-ns:child shape="square">
some text
</good-ns:child>
</parent>
<parent color="blue">
<good-ns:child shape="triangle">
some text
</good-ns:child>
</parent>
<parent color="green">
<good-ns:child shape="circle">
some text
</good-ns:child>
</parent>
</root>
will be transformed into:
<root>
<parent color="red">
<good-ns:child xmlns:good-ns="http://good.com" shape="square">
some text
</good-ns:child>
</parent>
<parent color="blue">
<good-ns:child xmlns:good-ns="http://good.com" shape="triangle">
some text
</good-ns:child>
</parent>
<parent color="green">
<good-ns:child xmlns:good-ns="http://good.com" shape="circle">
some text
</good-ns:child>
</parent>
</root>
Cheers,
Dimitre Novatchev.
"Christopher R. Maden" <crism at maden dot org> wrote:
At 00:15 8/4/02, Dimitre Novatchev wrote:
> <xsl:template match="*">
> <xsl:element name="{name()}" namespace="{namespace-uri(.)}">
> <xsl:copy-of select="namespace::*[name() != 'bad-ns']" />
> <xsl:apply-templates select="node()|@*" />
> </xsl:element>
> </xsl:template>
I was almost there...
This example still requires hard-coding of the specific namespaces that
aren't desired - in otherwords, it's a sort of blacklist filter. It's
also
possible to do this constructively:
<!-- For all elements: -->
<xsl:template match="*">
<xsl:choose>
<!-- If there is no prefix, and no default namespace in
scope: -->
<xsl:when test="substring-before(name(current()),':') = '' and
not(namespace::*[name() = ''])">
<!-- Just create the element with no namespace. -->
<xsl:element name="{name()}">
<xsl:apply-templates select="node()|@*" />
</xsl:element>
</xsl:when>
<!-- If there's no prefix, but there is a default namespace
in scope: -->
<xsl:when test="substring-before(name(current()),':') = ''">
<!-- Make the element with the default namespace. -->
<xsl:element name="{name()}" namespace="{namespace-uri(.)}">
<!-- Copy the default namespace. -->
<xsl:copy-of select="namespace::*[name*() = '']"/>
<xsl:apply-templates select="node()|@*"/>
</xsl:element>
</xsl:when>
<!-- But if there is a prefix, or a default namespace: -->
<xsl:otherwise>
<!-- Then create the element with the right namespace, -->
<xsl:element name="{name()}" namespace="{namespace-uri(.)}">
<!-- and copy the namespace actually used by this
element. -->
<xsl:copy-of
select="namespace::*
[name() =
substring-before(name(current()),':')]"/>
<xsl:apply-templates select="node()|@*" />
</xsl:element>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
__________________________________________________
Do You Yahoo!?
Yahoo! Tax Center - online filing with TurboTax
http://taxes.yahoo.com/
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list