473,769 Members | 1,681 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to copy element between different namespaces?

I want to copy elements from one namespace to anothor, how to create the
xslt?

for example, the source data is:
<s:mail xmlns:s="urn:so urce-namespace">
<s:subject>xxxx </s:subject>
<s:from>xxxx</s:from>
<s:to>xxxx</s:to>
<s:content>
<![CDATA[xxxxxxxxx]]>
</s:content>
</s:mail>

I want to copy mail element to the target namespace,lik e this:
<t:mydata xmlns:t="urn:ta rget-namespace"
<t:mail>
<t:subject>xxxx </t:subject>
<t:from>xxxx</t:from>
<t:to>xxxx</s:to>
<t:content>
<![CDATA[xxxxxxxxx]]>
</t:content>
</t:mail>
<t:mydata>
Nov 12 '05 #1
8 1460
Knighterrant wrote:
I want to copy elements from one namespace to anothor, how to create the
xslt?


You have to recreate elements, namespace cannot be changed:

<xsl:styleshe et version="1.0"
xmlns:xsl="http ://www.w3.org/1999/XSL/Transform"
xmlns:s="urn:so urce-namespace">
<xsl:template match="@*|node( )">
<xsl:copy>
<xsl:apply-templates select="@*|node ()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="s:*">
<xsl:element name="t:{local-name()}" namespace="urn: target-namespace">
<xsl:apply-templates select="@*|node ()"/>
</xsl:element>
</xsl:template>
</xsl:stylesheet>

--
Oleg Tkachenko [XML MVP, MCAD]
http://www.xmllab.net
http://blog.tkachenko.com
Nov 12 '05 #2
Hi, Oleg Tkachenko!

*** Thanks for you help!

*** I've tried the method you told me, it works! but there is still a problem need you help.

my source xml file is like this:
&lt;?xml version="1.0" encoding="utf-8"?&gt;
&lt;s:root xmlns:s="urn:so urce-namespace"&gt;
*** &lt;s:mail&g t;
*** ** &lt;s:subject&g t;xxxx&lt;/s:subject&gt;
*** ** &lt;s:from&gt;x xxx&lt;/s:from&gt;
*** ** &lt;s:to&gt;xxx x&lt;/s:to&gt;
*** ** &lt;s:content&g t;
*** ** &lt;![CDATA[xxxxxxxxx]]&gt;
*** ** &lt;/s:content&gt;
*** &lt;/s:mail&gt;
&lt;/s:root&gt;

my transform file:
&lt;?xml version="1.0" encoding="utf-8"?&gt;

&lt;xsl:stylesh eet version="1.0"
*** *** xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
*** *** xmlns:s="urn:so urce-namespace"
*** *** xmlns="urn:targ et-namespace"&gt;

*** &lt;xsl:templat e match="/"&gt;
*** *** &lt;xsl:elem ent name="newmails" &gt;
*** *** *** &lt;xsl:appl y-templates select="//s:mail" /&gt;
*** *** &lt;/xsl:element&gt;
*** &lt;/xsl:template&gt ;

*** &lt;xsl:templat e match="s:mail"& gt;
*** *** &lt;xsl:elem ent name="newmail"& gt;
*** *** *** &lt;xsl:copy&gt ;
*** *** *** *** &lt;xsl:appl y-templates select="*"/&gt;
*** *** *** &lt;/xsl:copy&gt;
*** *** &lt;/xsl:element&gt;
*** &lt;/xsl:template&gt ;
***
*** &lt;xsl:templat e match="s:*"&gt;
*** *** &lt;xsl:elem ent name="{local-name()}" namespace="urn: target-namespace"&gt;
*** *** *** &lt;xsl:appl y-templates select="@*|node ()"/&gt;
*** *** &lt;/xsl:element&gt;
*** &lt;/xsl:template&gt ;
***
&lt;/xsl:stylesheet& gt;

the result is:
&lt;?xml version="1.0" encoding="utf-8"?&gt;
&lt;newmails xmlns="urn:targ et-namespace"&gt;
*** &lt;newmail& gt;
*** *** &lt;s:mail xmlns:s="urn:so urce-namespace"&gt;
*** *** *** &lt;subject&gt; xxxx&lt;/subject&gt;
*** *** *** &lt;from&gt;xxx x&lt;/from&gt;
*** *** *** &lt;to&gt;xxxx& lt;/to&gt;
*** *** *** &lt;content&gt; xxxxxxxxx&lt;/content&gt;
*** *** &lt;/s:mail&gt;
*** &lt;/newmail&gt;
&lt;/newmails&gt;

BUT the result I hoped is:
&lt;?xml version="1.0" encoding="utf-8"?&gt;
&lt;newmails xmlns="urn:targ et-namespace"&gt;
*** &lt;newmail& gt;
*** *** &lt;subject&gt; xxxx&lt;/subject&gt;
*** *** &lt;from&gt;xxx x&lt;/from&gt;
*** *** &lt;to&gt;xxxx& lt;/to&gt;
*** *** &lt;content&gt; xxxxxxxxx&lt;/content&gt;
*** &lt;/newmail&gt;
&lt;/newmails&gt;

I DO NOT create the "s:mail" element! Could you tell me how to elimate the "s:mail" element, and why this happened?



Oleg Tkachenko [MVP] 写道: Knighterrant wrote:
I want to copy elements from one namespace to anothor, how to create the xslt?

You have to recreate elements, namespace cannot be changed:

&lt;xsl:stylesh eet version="1.0"
****xmlns:x sl="http://www.w3.org/1999/XSL/Transform"
* xmlns:s="urn:so urce-namespace"&gt;
* &lt;xsl:templat e match="@*|node( )"&gt;
*** &lt;xsl:copy&gt ;
***** &lt;xsl:appl y-templates select="@*|node ()"/&gt;
*** &lt;/xsl:copy&gt;
* &lt;/xsl:template&gt ;
* &lt;xsl:templat e match="s:*"&gt;
*** &lt;xsl:elem ent name="t:{local-name()}" namespace="urn: target-namespace"&gt;
***** &lt;xsl:appl y-templates select="@*|node ()"/&gt;
*** &lt;/xsl:element&gt;
* &lt;/xsl:template&gt ;
&lt;/xsl:stylesheet& gt;

Nov 12 '05 #3
Knighterrant wrote:
<xsl:template match="s:mail">
<xsl:element name="newmail">
<xsl:copy>


This <xsl:copy> makes shallow copy of the context node, which is s:mail.
Get rid of <xsl:copy> if you don't need it. And you don't have to use
<xsl:element> to output elements with known name, use literal form:
<xsl:template match="s:mail">
<newmail>
<xsl:apply-templates select="@*|node ()"/>
</newmail>
</xsl:template>

--
Oleg Tkachenko [XML MVP, MCAD]
http://www.xmllab.net
http://blog.tkachenko.com
Nov 12 '05 #4
I have solved the problem. Thank you for your suggestion!
By the way, could tell me the usage of brace in detail? I don't find the description in MSDN.
&lt;xsl:elem ent name="{local-name()}" namespace="urn: target-namespace"&gt;

my transform file:
&lt;?xml version="1.0" encoding="utf-8"?&gt;

&lt;xsl:stylesh eet version="1.0"
*** *** xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
*** *** xmlns:s="urn:so urce-namespace"
*** *** xmlns="urn:targ et-namespace"&gt;

*** &lt;xsl:templat e match="/"&gt;
*** *** &lt;xsl:elem ent name="newmails" &gt;
*** *** *** &lt;xsl:appl y-templates select="//s:mail" /&gt;
*** *** &lt;/xsl:element&gt;
*** &lt;/xsl:template&gt ;

*** &lt;xsl:templat e match="s:mail"& gt;
*** *** &lt;xsl:elem ent name="newmail"& gt;
*** *** *** &lt;xsl:copy&gt ;
*** *** *** *** &lt;xsl:appl y-templates select="*"/&gt;
*** *** *** &lt;/xsl:copy&gt;
*** *** &lt;/xsl:element&gt;
*** &lt;/xsl:template&gt ;
***
*** &lt;xsl:templat e name="copy"&gt;
*** ** *&lt;xsl:eleme nt name="{local-name()}" namespace="urn: target-namespace"&gt;
*** ** *** *&lt;xsl:copy-of select="@*|text ()" /&gt;
*** ** *** *&lt;xsl:appl y-templates select="*" /&gt;
*** ** *&lt;/xsl:element&gt;
*** &lt;/xsl:template&gt ;
*** **
&lt;/xsl:stylesheet& gt;



Nov 12 '05 #5
Knighterrant wrote:
I have solved the problem. Thank you for your suggestion!
By the way, could tell me the usage of brace in detail? I don't find the
description in MSDN.


{} is an attribute value template. An expression within {} will be
evaluated and converted to string. This way you can get dynamic values
in attributes. See
http://msdn.microsoft.com/library/de...d23ebe56c1.asp

--
Oleg Tkachenko [XML MVP, MCAD]
http://www.xmllab.net
http://blog.tkachenko.com
Nov 12 '05 #6
Sorry, the message I post just now is wrong, correct one should like this:

&lt;?xml version="1.0" encoding="utf-8"?&gt;

&lt;xsl:stylesh eet version="1.0"
*** *** xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
*** *** xmlns:s="urn:so urce-namespace"
*** *** xmlns="urn:targ et-namespace"&gt;

*** &lt;xsl:templat e match="/"&gt;
*** *** &lt;xsl:elem ent name="newmails" &gt;
*** *** *** &lt;xsl:appl y-templates select="//s:mail" /&gt;
*** *** &lt;/xsl:element&gt;
*** &lt;/xsl:template&gt ;

*** &lt;xsl:templat e match="s:mail"& gt;
*** *** &lt;xsl:elem ent name="newmail"& gt;
*** ** **** &lt;xsl:for-each select="*"&gt;
*** ** *** *** *&lt;xsl:call-template name="copy" /&gt;
*** ** *** *&lt;/xsl:for-each&gt;
*** *** &lt;/xsl:element&gt;
*** &lt;/xsl:template&gt ;
***
*** &lt;xsl:templat e name="copy"&gt;
*** ** *&lt;xsl:eleme nt name="{local-name()}" namespace="urn: target-namespace"&gt;
*** ** *** *&lt;xsl:copy-of select="@*|text ()" /&gt;
*** ** *** *&lt;xsl:appl y-templates select="*" /&gt;
*** ** *&lt;/xsl:element&gt;
*** &lt;/xsl:template&gt ;
*** **
&lt;/xsl:stylesheet& gt;

Nov 12 '05 #7
Yes, you are right! I see. Thank you!

Oleg Tkachenko [MVP] 写道:
Knighterrant wrote:
<xsl:template match="s:mail">
<xsl:element name="newmail">
<xsl:copy>

This <xsl:copy> makes shallow copy of the context node, which is
s:mail. Get rid of <xsl:copy> if you don't need it. And you don't have
to use <xsl:element> to output elements with known name, use literal
form:
<xsl:template match="s:mail">
<newmail>
<xsl:apply-templates select="@*|node ()"/>
</newmail>
</xsl:template>

Nov 12 '05 #8
Oh, That's why. Thanks.

Oleg Tkachenko [MVP] 写道:
Knighterrant wrote:
I have solved the problem. Thank you for your suggestion!
By the way, could tell me the usage of brace in detail? I don't find
the description in MSDN.

{} is an attribute value template. An expression within {} will be
evaluated and converted to string. This way you can get dynamic values
in attributes. See
http://msdn.microsoft.com/library/de...d23ebe56c1.asp

Nov 12 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
5278
by: Adam Barr | last post by:
I have a tag foo that I want to copy unchanged when it is a subtag of bar, so I have a template (x is the namespace for the document): <xsl:template match="x:bar/x:foo"> <xsl:copy> <xsl:apply-templates/> </xsl:copy> </xsl:template> BUT, I discovered that someone has been mis-speling foo as foop in the
18
2434
by: Tjerk Wolterink | last post by:
i have the following rule, <xsl:template match="br"> <br/> </xsl:template> This should convert all <br/> to <br/> but, my transformer transforms it all to
9
4348
by: AA | last post by:
Hello, I need to extract an element from a xml document something like this <myXml> <Header> <Name/> <LastName/> <Age/> </Head> <Body> <Properties>
6
2589
by: Martin | last post by:
Hi, I have a xml file like the one below <?xml version="1.0" encoding="utf-8"?><e1 xmlns:e1="http://tempuri.org/Source1.xsd" e1:att1="1" e1:att2="2" e1:rest="345"/> If I try to create a schema for it with Visual Studio, I get the error "Failed to create a schema for this data file because:
4
2126
by: Raed Sawalha | last post by:
I'm trying to create Element as following name MyElement:InitialName XmlElement elem = doc.CreateElement("MyElement:InitialName"); when generate the XML the tag is truncated as ONLY "MyElement",why? Any Suggestions
7
2263
by: Harrie | last post by:
Hi group, I want to indent existing XML files so they are more readable (at least to me). At this moment I'm looking at the XML files OpenOffice.org's Writer application produces in it's zipped "SXW" format (and they're one line, probably to save space, which I find hard to read). At first I thought I was going to do it with sed/awk or something like that, but then I remembered the xsl:output element with the indent attribute of XSL and...
2
2567
by: aglaforge | last post by:
I'm attempting to write a quick piece of Javascript code that will validate if the end user of the javascript has the necessary VML attributes set in their HTML. The problem in IE is that "xmlns:v" does not appear in their attributes property or the getAttribute('xmlns:v') calls. The real kicker is that the 'xmlns' attribute does return something. The HTML Snippet would look like this: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0...
9
6480
by: Mark Olbert | last post by:
I'm trying to serialize (using XmlSerializer.Serialize) a class that I generated from an XSD schema using XSD.EXE /c. The problem I'm running into is that the root element needs to be unqualified, and the default namespace needs to be included on it as an attribute. The schema I'm using is this: <xs:schema xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns:html="http://www.w3.org/TR/REC-html40"...
6
4340
by: kluge.wolfram | last post by:
Hi, i get stucked on a transformation problem using XSLT. What i need is to copy an XML Tree to an output XML without any automatic changes. Since i used <xsl:copyor <xsl:copy-ofthere occur unwanted side effects. For example i just copied a xml were several namespace declarations are present more than one time. Then the transformation do remove the declaration at the child nodes. Another funny automatism is - if i remove a node
0
9579
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, well explore What is ONU, What Is Router, ONU & Routers main usage, and What is the difference between ONU and Router. Lets take a closer look ! Part I. Meaning of...
0
9415
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10198
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9978
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7392
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupr who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6661
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5293
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3551
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2810
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.