[XSL-LIST Mailing List Archive Home] [By Thread] [By Date]

Re: [xsl] How to move the namespaces onto the root element when there is a namespace prefix that is bound to different namespaces?


Subject: Re: [xsl] How to move the namespaces onto the root element when there is a namespace prefix that is bound to different namespaces?
From: Dimitre Novatchev <dnovatchev@xxxxxxxxx>
Date: Sun, 13 Jan 2013 11:37:39 -0800

> How do I move the namespace declarations up onto the root element, while
> taking in account that the XML document may contain a namespace prefix that
> is bound to different namespaces?

Obviously, an element can't have two different namespaces
declared/bound-to the same prefix.

What can be done, is to perform a two pass transformation:

 1. Pass one. Rename all prefixes that are bound to the same namespace
so that there remain no prefix bound to more than one namespace.

 2. Perform the simple copying of all namespace nodes to the top
element of the result of step 1.


Cheers,
Dimitre

On Sun, Jan 13, 2013 at 11:29 AM, Costello, Roger L. <costello@xxxxxxxxx> wrote:
>
> Hi Folks,
>
> I found this wonderful code [1] from Michael Kay to move the namespace
> declarations up onto the root element:
>
>   <xsl:template match="/*">
>     <xsl:copy>
>       <xsl:copy-of select="@*, //namespace::*, child::node()"/>
>     </xsl:copy>
>   </xsl:template>
>
> That works great, provided the XML document doesn't have the same
> namespace prefix bound to different namespaces. For example, it fails on
> this XML document:
>
> <Test xmlns="A">
>     <ns1:element xmlns:ns1="B">Hello</ns1:element>
>     <ns1:element xmlns:ns1="C">C</ns1:element>
> </Test>
>
> Notice that the namespace prefix, ns1, is bound to two different
> namespaces.
>
> Applying the XSLT program to the XML document results in this  error:
>
>     Cannot create two namespace nodes with the same
>     prefix mapped to different URIs (prefix=ns1, URI=C,
>     URI=B)
>
> How do I move the namespace declarations up onto the root element, while
> taking in account that the XML document may contain a namespace prefix that
> is bound to different namespaces?
>
> /Roger
>
> [1] http://www.mhonarc.org/archive/html/xsl-list/2009-10/msg00153.html
>



--
Cheers,
Dimitre Novatchev
---------------------------------------
Truly great madness cannot be achieved without significant intelligence.
---------------------------------------
To invent, you need a good imagination and a pile of junk
-------------------------------------
Never fight an inanimate object
-------------------------------------
To avoid situations in which you might make mistakes may be the
biggest mistake of all
------------------------------------
Quality means doing it right when no one is looking.
-------------------------------------
You've achieved success in your field when you don't know whether what
you're doing is work or play
-------------------------------------
Facts do not cease to exist because they are ignored.
-------------------------------------
Typing monkeys will write all Shakespeare's works in 200yrs.Will they write
all patents, too? :)
-------------------------------------
I finally figured out the only reason to be alive is to enjoy it.


Current Thread
Keywords