Jump to content

[XSL] Rename some nodes

Recommended Posts

Hello all,I am trying to replace the namespace from the nodes of a XML file, thanks to a XSL transformation. I saw a serie of samples on internet that allow to replace the node content, but not the node itself...To make it clearer, I have:

<foo:customer>   <foo:id>1234</foo:id>   <foo:name>Toto</foo:name></foo:customer>

And I would like as the result of my transformation:

<faa:customer>   <faa:id>1234</faa:id>   <faa:name>Toto</faa:name></faa:customer>

Maybe someone has already have something similar to do?Thanks, Benjamin

Link to post
Share on other sites

Is the namespace URI of the output document the same as the output of the input document?If the same, all you need to do is declare "faa" at the top of the stylesheet, having the same namespace URI, and then do:

<xsl:template match="*"><xsl:copy><xsl:apply-templates/></xsl:copy></xsl:template>

If different, again declare the "faa" prefix at the top, but this time use:

<xsl:template match="*"><xsl:element name="{local-name()}" namespace=""><xsl:apply-templates/></xsl:element></xsl:template>

(replace the value of the namespace attribute above with the exact namespace URI of "faa")

Link to post
Share on other sites

Hello boen_robot, thanks a lot for this fast reply! The namespace has to be different in the output indeed. The solution you proposed looks elegant but I still have a problem, since my transformed XML is read but a system that does not understand xmlns="faa"...My XML looks like:

<?xml version="1.0" encoding="UTF-8"?><foo:fooDocument xmlns:foo="http://www.foo.com/xml">	<foo:customer>		<foo:id>1234</foo:id>		<foo:name>Toto</foo:name>	</foo:customer></foo:fooDocument>

And now my XSL is:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:faa="http://www.faa.com/xml" xmlns:foo="http://www.foo.com/xml" version="1.0"><xsl:template match="foo:fooDocument">		<faa:faaDocument xmlns:faa="http://www.faa.com/xml">					<xsl:apply-templates select="./*|comment()|text()"/>	</faa:faaDocument>	</xsl:template> <xsl:template match="*">	<xsl:element name="{local-name()}" namespace="faa">		<xsl:apply-templates/>	</xsl:element></xsl:template></xsl:stylesheet>

And the result is:

<?xml version="1.0" encoding="UTF-8"?><faa:faaDocument xmlns:faa="http://www.faa.com/xml" xmlns:foo="http://www.foo.com/xml">	<customer xmlns="faa">		<id>1234</id>		<name>Toto</name>	</customer></faa:faaDocument>

Instead of:

<?xml version="1.0" encoding="UTF-8"?><faa:faaDocument xmlns:faa="http://www.faa.com/xml">	<faa:customer>		<faa:id>1234</faa:id>		<faa:name>Toto</faa:name>	</faa:customer></faa:faaDocument>

I am not a XSL expert but I guess that <faa:customer> and <customer xmlns="faa"> are equivalent, unfortunatly it changes something for me: I use an import tool based on the java API that returns me an error:

Errors while importing file:com.calypso.apps.common.adapter.AdapterException: Errors occured while importing.Original error:javax.xml.bind.UnmarshalException: unexpected element (uri:"faa", local:"customer"). Expected elements are <{http://www.calypso.com/xml}fii>,...

:) Maybe there is a way to format differently the output?Thanks, Benjamin

Link to post
Share on other sites

You need to put both namespace declarations at the top of the stylesheet. Otherwise, the namespace is preserved only for the elements within the template. Also, the namespace attribute needs to point to the namespace URI, not the namespace prefix:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:faa="http://www.faa.com/xml" xmlns:foo="http://www.foo.com/xml" version="1.0" xmlns:faa="http://www.faa.com/xml"><xsl:template match="foo:fooDocument">		<faa:faaDocument">					<xsl:apply-templates select="./*|comment()|text()"/>	</faa:faaDocument>	</xsl:template> <xsl:template match="*">	<xsl:element name="{local-name()}" namespace="http://www.faa.com/xml">		<xsl:apply-templates/>	</xsl:element></xsl:template></xsl:stylesheet>

Link to post
Share on other sites

Hello boen_robot,actually your solution feets well to my need, the problem was related to the attribute namespace. During my search I found something else that seems to be usefull for related issues: the element xsl:namespace-alias . I do not have enought time to explore this track.Thanks again :) Benjamin

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...