File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes XML and Related Technologies and the fly likes Changing namespace uri using xslt Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "Changing namespace uri using xslt" Watch "Changing namespace uri using xslt" New topic

Changing namespace uri using xslt

Shane Lee
Ranch Hand

Joined: Mar 19, 2005
Posts: 63
Did not think this would be a huge issue but it is!
Ok the xml i am working on i need to change the root element MsgCiceroTxn to MsgCourtLinkTxn and the namespace uri to

MY sample xml message that i am working of is:

And my xsl is:

This does not change anything not even the element name!

If i change my xsl to point to element fms:Receipt instead and change the root namespace to the old one xmlns:fms=" (Just for test purposes):
<xsl:template match="fms:Receipt">
<xsl:element name="fms:Receipt" namespace="">
<xsl:apply-templates select="@*|node()"/>

the following transformation output occurred then:

Any solution, can i just say i hate namespaces!!!

Paul Clapham

Joined: Oct 14, 2005
Posts: 19973

Didn't we do this before? Maybe it was someone else, the question comes up often enough.

That element matches a MsgCiceroTxn element which isn't in any namespace. Your MsgCiceroTxn elements are all in the default namespace, which is a namespace, so you don't match it.

Declare your default namespace URI (from the XML document) as a namespace with a prefix in your XSLT. Then match on the element with that prefix.
Shane Lee
Ranch Hand

Joined: Mar 19, 2005
Posts: 63
haha thanks paul ive been spending all day on this today. How to access the root element not knowing that yes it is in a default namespace but with no prefix, but i still needed to refer to the element name by a prefix in my xsl.

As for the other part removing the default namespace and changing it to a different namespace uri, that was really tricky. For anyone that wants xsl to change default namespace uri and its sibling(again i think this is bad xml code standard to call the same namespace uri even though it is the root default, i didnt code the xml output!) and change the root element of the xml document here you go:

Thanks again paul, namespaces are killing me.... ;-)
I agree. Here's the link:
subject: Changing namespace uri using xslt
It's not a secret anymore!