[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Re: How to move the namespaces onto the root element w

Subject: Re: How to move the namespaces onto the root element when there is a namespace prefix that is bound to different namespaces?
From: Michael Kay <mike@xxxxxxxxxxxx>
Date: Sun, 13 Jan 2013 20:03:32 +0000
Re:  How to move the namespaces onto the root element w
Well, if you only want to move the first namespace declaration with any given prefix to the root, you could do something like this:

<xsl:copy-of select="@*"/>
<xsl:for-each-group select="//namespace::*" group-by="name()">
  <xsl:copy-of select="current-group()[1]"/>
</xsl:for-each-group>
<xsl:copy-of select="child::node()"/>

Michael Kay
Saxonica

(Actually current-group()[1] could be written ".", but I think it's more readable as written).

On 13/01/2013 19:29, Costello, Roger L. 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

Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.