Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old December 17th, 2009, 05:15 AM
Authorized User
 
Join Date: Dec 2009
Posts: 11
Thanks: 2
Thanked 0 Times in 0 Posts
Default Problem in replacing XML namespace using XSLT

Hello,

I need to just replace the XML namespace using XSLT. My input XML looks like as follows. I just want to replace "oldNameSpace" with "newNameSpace" and rest of XML want as it is..

Code:
<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
	xmlns:impl="oldNameSpace" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
	<soapenv:Body>
		<impl:requestData
			xmlns:impl="oldNameSpace">
			<xmlString ChangeDate="2008-12-18T14:47:11.773+01:00"
				IdentificationNumber="WDDKJ5GBXAF000229"
				OrderNumber="08 295 70821"
				ProductionNumber="1700158">
				<ServiceTool ExecutionTime="2008-12-18T14:47:11.773+01:00"
					UserID="kris" Version="1.1.1" />
			</xmlString>
		</impl:requestData>
	</soapenv:Body>
</soapenv:Envelope>
I tried with following XSL

Code:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
	xmlns:source="oldNameSpace" xmlns:desti="newNameSPace" xmlns="newNameSPace"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.0">
	<xsl:output method="xml" encoding="iso-8859-1" indent="yes" />


	<xsl:template match="source:*">
		<xsl:element name="{name()}" namespace="newNameSPace">
			<xsl:apply-templates select="@*|node()" />
		</xsl:element>
	</xsl:template>

	<xsl:template match="@*|node()">
		<xsl:copy>
			<xsl:apply-templates select="@*|node()" />
		</xsl:copy>
	</xsl:template>
</xsl:stylesheet>
but it is not giving the desired results...it is giving the following results.

Code:
<?xml version="1.0" encoding="iso-8859-1"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:impl="oldNameSpace" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
	<soapenv:Body>
		<impl:requestData xmlns:impl="newNameSPace">
			<xmlString xmlns:impl="oldNameSpace" ChangeDate="2008-12-18T14:47:11.773+01:00" IdentificationNumber="WDDKJ5GBXAF000229" OrderNumber="08 295 70821" ProductionNumber="1700158">
				<ServiceTool ExecutionTime="2008-12-18T14:47:11.773+01:00" UserID="kris" Version="1.1.1"/>
			</xmlString>
		</impl:requestData>
	</soapenv:Body>
</soapenv:Envelope>
problem with the above is that -- "<xmlString xmlns:impl="oldNameSpace" is still showing the "oldNameSpace" only.. where as there was no namespace for that element in the input

Anyone can help in this ....
Thanks a lot.

---Krishna

Last edited by kris13; December 18th, 2009 at 01:38 AM..
 
Old December 17th, 2009, 05:51 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

<xsl:copy> copies the element and all its in-scope namespaces. In XSLT 2.0 you can use <xsl:copy copy-namespaces="no"> to suppress this. In XSLT 1.0 you will have to use <xsl:element> rather than <xsl:copy>.
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
 
Old December 17th, 2009, 06:48 AM
Authorized User
 
Join Date: Dec 2009
Posts: 11
Thanks: 2
Thanked 0 Times in 0 Posts
Default

Thanks Michael !

I tried to use that also.. xsl:copy copy-namespaces="no"> and version- 2.0

but result is same... :(


if you observe the tranformed result
transformation is adding this "oldNameSpace" addtionally...how should i get rid of this...
<xmlString xmlns:impl="oldNameSpace" ......................

---Krishna
 
Old December 17th, 2009, 07:34 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Which XSLT 2.0 processor did you use?

(People sometimes make the mistake of just adding version="2.0" to the stylesheet and then running it through an XSLT 1.0 processor. This will simply cause the 1.0 processor to ignore the attributes it doesn't understand).
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
 
Old December 17th, 2009, 07:58 AM
Authorized User
 
Join Date: Dec 2009
Posts: 11
Thanks: 2
Thanked 0 Times in 0 Posts
Default

I am using the following Java Code to transform the input file

Code:
File xmlFile = new File("./xml_xsl/input.xml");
		File xsltFile = new File("./xml_xsl/transform.xsl");

		// JAXP reads data using the Source interface
		Source xmlSource = new StreamSource(xmlFile);
		Source xsltSource = new StreamSource(xsltFile);

		// the factory pattern supports different XSLT processors
		TransformerFactory transFact = TransformerFactory.newInstance();
		Transformer trans = null;
		try {
			trans = transFact.newTransformer(xsltSource);
			File f = new File("./xml_xsl/output.xml");
			trans.transform(xmlSource, new StreamResult(f));
		} catch (TransformerConfigurationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (TransformerException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
 
Old December 17th, 2009, 08:16 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

If you have Saxon on your classpath this code will pick up an XSLT 2.0 processor; if you don't, it will load the default XSLT processor from the JDK, which is a 1.0 processor (Xalan).
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
 
Old December 18th, 2009, 01:41 AM
Authorized User
 
Join Date: Dec 2009
Posts: 11
Thanks: 2
Thanked 0 Times in 0 Posts
Default

Hello Micheal,,,,

Thanks for your reply.
following is the classes used for transformation----
class com.sun.org.apache.xalan.internal.xsltc.trax.Trans formerFactoryImpl
class com.sun.org.apache.xalan.internal.xsltc.trax.Trans formerImpl
--------------------------------------------------------------------
Now i have modified the XSL as below:
Code:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
	xmlns:source="oldNameSpace" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.0">
	<xsl:output method="xml" encoding="iso-8859-1" indent="yes" />
	<xsl:template match="source:*">
		<xsl:element name="{local-name()}" namespace="newNameSPace">
			<xsl:apply-templates select="@* | node()" />
		</xsl:element>
	</xsl:template>
	<xsl:template match="@*|node()">
		<xsl:copy copy-namespaces="no">
			<xsl:apply-templates select="@*|node()" />
		</xsl:copy>
	</xsl:template>
It gives me new namespace for the "impl:requestData"
replaced nameSpace...and no prefix on the "xmlString" element ....but i ended into different problem....

Now I also need to put the prefix on <xmlString> element but not on the children of <xmlString> ......

So finally i need an output xml like this....

Code:
<?xml version="1.0" encoding="iso-8859-1"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:impl="oldNameSpace" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
	<soapenv:Body>
		<impl:requestData xmlns:impl="newNameSPace">
			<impl:xmlString xmlns:impl="oldNameSpace" ChangeDate="2008-12-18T14:47:11.773+01:00" IdentificationNumber="WDDKJ5GBXAF000229" OrderNumber="08 295 70821" ProductionNumber="1700158">
				<ServiceTool ExecutionTime="2008-12-18T14:47:11.773+01:00" UserID="kris" Version="1.1.1"/>
			</impl:xmlString>
		</impl:requestData>
	</soapenv:Body>
</soapenv:Envelope>
it would be great if you can help in this...
 
Old December 18th, 2009, 04:20 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

Just add the following:

<xsl:template match="xmlString">
<impl:xmlString>
<xsl:apply-templates select="@* | node()" />
</impl:xmlString>
</xsl:template>
__________________
/- Sam Judson : Wrox Technical Editor -/

Think before you post: What have you tried?
 
Old December 18th, 2009, 05:01 AM
Authorized User
 
Join Date: Dec 2009
Posts: 11
Thanks: 2
Thanked 0 Times in 0 Posts
Default

Hello Sam,

The template is working...but in addition it adding one attribute to one of
child element, i have highlighted that


<impl:requestData xmlns:impl="newNameSPace">
<impl:xmlString xmlns:impl="oldNameSpace" ChangeDate="2008-12-18T14:47:11.773+01:00" IdentificationNumber="WDDKJ5GBXAF000229" OrderNumber="08 295 70821" ProductionNumber="1700158">
<ServiceTool xmlns:impl="oldNameSpace" ExecutionTime="2008-12-18T14:47:11.773+01:00" UserID="kris" Version="1.1.1"/>
</impl:xmlString>
</impl:requestData>


I think this will create while validation of XML.
 
Old December 18th, 2009, 05:12 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

I don't know why whatever XSLT/XML processor you are using keeps redefining the Impl prefix, but I'm happy to let you know that it doesn't matter.

Also, not sure which namespace you want the xmlString to be in (old or new) but you should have all the tools to make any further changes you need.

Let us know if you need any more help.
__________________
/- Sam Judson : Wrox Technical Editor -/

Think before you post: What have you tried?





Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem with replacing string in XSLT nelly78 XSLT 5 September 16th, 2009 07:41 AM
to remove namespace prefix form input xml using xslt robin_stringss XSLT 4 May 17th, 2009 06:50 AM
Replacing tags within xml using xslt patelgaurav85 XSLT 0 June 19th, 2007 12:34 PM
dynamic xslt -> xslt creation namespace problem jkmyoung XSLT 2 July 15th, 2006 12:42 AM
xml validation-namespace is problem asap XML 2 April 26th, 2006 02:56 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.