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 18th, 2009, 05:35 AM
Authorized User
 
Join Date: Dec 2009
Posts: 11
Thanks: 2
Thanked 0 Times in 0 Posts
Default

Hello Sam,

i really appreciate your prompt replies... Many thanks for that....

OK... i'll give my input.xml , transformation file and desired output.xml...
You please let me know how should i change my transformation file ...


Code:
<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>

transformation file

Code:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"

	xmlns:ser="newNameSpace" 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>

	<xsl:template match="xmlString">
		<ser:xmlString >
			<xsl:apply-templates select="@* | node()" />
		</ser:xmlString>
	</xsl:template>
</xsl:stylesheet>

desired transformed result.

Code:
<?xml version="1.0" encoding="iso-8859-1"?>
<soapenv:Envelope xmlns:ns0="newNameSpace" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:impl="oldNameSpace" xmlns:xsd="http://www.w3.org/2001/XMLSchema" >
	<soapenv:Body>
		<ns0:requestData xmlns:ns0="newNameSpace">
			<ns0: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"/>
			</ns0:xmlString>
		</ns0:requestData>
	</soapenv:Body>
</soapenv:Envelope>
 
Old December 18th, 2009, 06:00 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

I think Sam failed to spot that the inner definition of xmlns:impl is not a redundant redefinition, it is binding impl to a different namespace URI.

If you copy an element from the source then it will retain its name - that is, its "expanded name" comprising namespace URI plus local name. If you want to change its name (that is, either part of the name) you need to use xsl:element rather than xsl:copy.

The important thing is to think in terms of what are the expanded-names of the elements. Don't worry about namespace declarations - they will look after themselves if you get the names of the elements right.
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
The Following User Says Thank You to mhkay For This Useful Post:
kris13 (December 22nd, 2009)
 
Old December 18th, 2009, 06:09 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

I've ran the input you've given there against the stylesheet in your post and I get output that matches what you desired result. The prefixes might not be exactly the same, but in XML the prefix doesn't mean much - as Michael say it is the expanded name of an element that counts (the combination of the namespace and its local name).
__________________
/- Sam Judson : Wrox Technical Editor -/

Think before you post: What have you tried?
The Following User Says Thank You to samjudson For This Useful Post:
kris13 (December 22nd, 2009)
 
Old December 22nd, 2009, 01:22 AM
Authorized User
 
Join Date: Dec 2009
Posts: 11
Thanks: 2
Thanked 0 Times in 0 Posts
Unhappy

Hello Micheal & Sam,

Thanks for your replies... These solution worked out.... and i got proper result...and also thanks for informative explanation

In the solution I was getting an additional namespace attribute in result. Although that was not effecting the parsing of the response as you said..



As Micheal suggested one alternate solution also , in that he was mentioning that... these additional namespace will not come; i.e. using <xsl:element> instead of the <xsl:copy>, I tried with the following

---------------------------------------------------
Input xml
---------------------------------------------------
Code:
<?xml version="1.0" encoding="UTF-8"?>

<impl:requestData xmlns:impl="oldNameSpace">
	<impl: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" />
		<Component Version="1.1.1" />

	</impl:xmlString>
</impl:requestData>


---------------------------------------------------
desired result
---------------------------------------------------
Code:
<?xml version="1.0" encoding="UTF-8"?>

<impl:requestData xmlns:impl="newNameSpace">
	<xmlReport 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" />
		<Component Version="1.1.1" />
	</xmlReport>
</impl:requestData>

---------------------------------------------------
transformation file
---------------------------------------------------
Code:
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
	xmlns:ser="newNameSpace" xmlns:source="oldNameSpace"
	version="1.0">
	<xsl:output method="xml" encoding="iso-8859-1" indent="yes" />

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

	<xsl:template match="source:xmlString">
		<xmlReport>
			<xsl:element name="{local-name()}" namespace="newNameSpace">
				<xsl:copy-of select="@*|node()" />
				<xsl:apply-templates select="@*|node()" />
			</xsl:element>
		</xmlReport>
	</xsl:template>
</xsl:stylesheet>

So here in result additional name space attributes; mainly for the child elements <ServiceTool> <Component> was coming while using <xsl:copy>. Although it will not effect the parsing of the document... but My XML is too large ( there are hundreds of <ServiceTool> and <Component> elements ) and these additional attributes are unnecessarily increasing the size of message.


can you please help in this....

Thanks a lot.
Krishna

Last edited by kris13; December 22nd, 2009 at 01:29 AM..
 
Old December 22nd, 2009, 04:08 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

I don't know what the xsl:copy-of is doing in your first template - it's syntactically incorrect for xsl:copy-of to have content. And in the second template, you're creating the new element as a literal result element so you don't need xsl:element as well. Also, you seem to want the result elements to be in no namespace, so why are you putting them in a namespace called "newNamespace"?
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
 
Old December 22nd, 2009, 11:39 PM
Authorized User
 
Join Date: Dec 2009
Posts: 11
Thanks: 2
Thanked 0 Times in 0 Posts
Default

I will give you a background. I am working on Middleware (ESB) and both schmeas for both input .xml ( a consumer request ) and output.xml ( service provider ) already created and they are used since quite a long time. so i don't have the control to change these schemas ... .. and i have make them compatible to talk to each other and for that i HAVE to apply the transformation on these message

Quote:
Originally Posted by mhkay View Post
I don't know what the xsl:copy-of is doing in your first template - it's syntactically incorrect for xsl:copy-of to have content.
I will modify that... i think my understanding of xsl:copy-of is wrong


Quote:
Originally Posted by mhkay View Post
And in the second template, you're creating the new element as a literal result element so you don't need xsl:element as well. Also,
I am only renaming the element <impl:xmlString> to <xmlReport> and i want children of <impl:xmlString> as it is.. so i am applying a copy on this.



Quote:
Originally Posted by mhkay View Post
you seem to want the result elements to be in no namespace, so why are you putting them in a namespace called "newNamespace"?
The very first element "<impl:requestData>" should be in a namespace and children of this should not have any namespace.. The schema is created in this way and i can't change... :-(

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

I wasn't asking you to change the design of the XML. I was asking you why you had written the XSLT code the way you did, when it seemed to bear no relation to the output you were trying to produce.
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference





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.