Re: [xsl] How to remove unnecessary "xmlns:xx" attributes?

Subject: Re: [xsl] How to remove unnecessary "xmlns:xx" attributes?
From: Wolfgang Laun <wolfgang.laun@xxxxxxxxx>
Date: Wed, 10 Nov 2010 19:20:41 +0100
On 10 November 2010 17:54, Abel Braaksma <abel.online@xxxxxxxxx> wrote:
> Hi Wolfgang,
>
>>   <xsl:copy copy-namespaces = "no">
>
> You don't want the namespaces but the output:
>
>>     <?xml version="1.0" encoding="UTF-8"?>
>> <doc xmlns="p/q/r" xmlns:abc="a/b/c" xmlns:def="d/e/f" xmlns:ghi="g/h/i">
>>   <abc:bef>The quick brown fox jumps over the lazy dog</abc:bef>
>
> still contains them. Reason? You are copying nodes that belong to a certain
> namespace, not copying it would mean that the copy had changed the node
> name, which it cannot do. The only thing that copy-namespaces="no" prevents
> is adding the namespace to the node itself (and its children). The
mandatory
> namespace fixup process then makes sure that the element belongs to the
> correct namespace by applying the correct namespace prefix.
>
> If you want to keep the local part and remove the namespace part of an
> element, you'll have to use xsl:element with only a local-part. This will
> create different elements and it is likely that if your XML document is
> meant to be processed by any application that expects the elements in a
> certain namespace, that the application will fail.

The goal is not to transfer elements into another namespace. It's only
the *positions*
of the namespace nodes associating a prefix with a namespaceURI  that have to
be
tweaked.

I think that I followed Wendell's advice correctly.
copy-namespaces="no" avoids copying the
namespace nodes that are children of the copied node; this does not
influence any
of the QNames of the copied elements. If I simply omit the
<xsl:for-each-group>, xmlns:X nodes
will indeed be created by the fixup process, but they will be (again)
all over the document, at
the lowermost level where they are required, and frequently repeated;
all useless namespace
nodes are gone, however:

<!-- This relies on namespace fixup! -->
<?xml version="1.0" encoding="UTF-8"?>
<doc xmlns="p/q/r">
  <abc:bef xmlns:abc="a/b/c">The quick brown fox jumps over the lazy
dog</abc:bef>
  <para doc="jar">a para 1</para>
  <para doc="file">a para 2</para>
  <def:dir xmlns:def="d/e/f">
    <num>4123456</num>
    <num>412345</num>
    <num>41234567</num>
    <ghi:num xmlns:ghi="g/h/i">1242345678</ghi:num>
    <num>4222</num>
  </def:dir>
</doc>

But the <xsl:for-each-group> "factors" all actually used namespaces into the
top element, thereby creating a minimum of prefix associations. Fixup
won't find any place
where a namespace node has to be created.

Wendell has pointed out the risks of doing that, so I won't go into that
again.

-W

Current Thread