Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > XML > XSLT
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old November 15th, 2006, 05:31 PM
Registered User
 
Join Date: Nov 2006
Location: , , .
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default adding prefix to nodes and subnodes to output xml

I am coping nodes & subnodes of single element to output using <xsl:copy-of and i need to add prefix ns0: to those nodes using xslt

Reply With Quote
  #2 (permalink)  
Old November 15th, 2006, 06:11 PM
Friend of Wrox
Points: 1,676, Level: 16
Points: 1,676, Level: 16 Points: 1,676, Level: 16 Points: 1,676, Level: 16
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jul 2006
Location: , , .
Posts: 430
Thanks: 28
Thanked 5 Times in 5 Posts
Send a message via Yahoo to bonekrusher
Default

is ns0 the namespace?


Reply With Quote
  #3 (permalink)  
Old November 15th, 2006, 07:02 PM
mhkay's Avatar
Wrox Author
Points: 18,487, Level: 59
Points: 18,487, Level: 59 Points: 18,487, Level: 59 Points: 18,487, Level: 59
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

You're thinking in terms of the serialized output, not in terms of the nodes on the result tree. As far as the tree is concerned, you don't want to add a prefix, you want to change the names of the nodes (putting them in a different namespace). xsl:copy-of creates an identical copy, you can't make any changes such as renaming. So you need to walk the tree modifying each node to put it in a different namespace, which you typically do with

<xsl:element name="ns0:{local-name()}" namespace="the-new-uri"/>

Here "ns0" is just a hint to the processor about your preferred prefix.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
Reply With Quote
  #4 (permalink)  
Old November 16th, 2006, 06:20 PM
Registered User
 
Join Date: Nov 2006
Location: , , .
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks Michael , so after doing <xsl:copy-of , I should call another xslt to Traverse thru each Nodes to add prefix ns0:?

Reply With Quote
  #5 (permalink)  
Old November 16th, 2006, 06:38 PM
mhkay's Avatar
Wrox Author
Points: 18,487, Level: 59
Points: 18,487, Level: 59 Points: 18,487, Level: 59 Points: 18,487, Level: 59
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

No, you do that instead of the copy-of. It's a modified identity transform, a classic XSLT coding pattern:

<xsl:template match="*">
  <xsl:element name="ns0:{local-name()}" namespace="the-new-uri"/>
  <xsl:copy-of select="@*"/>
  <xsl:apply-templates/>
  </xsl:element>
</xsl:template>

gives you a copy of the tree in which the names of all elements have been changed to be in the new namespace.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
Reply With Quote
  #6 (permalink)  
Old November 17th, 2006, 10:16 AM
Registered User
 
Join Date: Nov 2006
Location: , , .
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

<?xml version="1.0" encoding="UTF-16"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:msxsl="urn:schemas-microsoft-com:xslt"
    xmlns:ns0="http://test/2004-08-02" >
  <xsl:output omit-xml-declaration="yes" method="xml" version="1.0" />

  <xsl:template match="/">
    <xsl:apply-templates select="/ns0:Order" />
  </xsl:template>

  <xsl:template match="/ns0:Order">
    <ns1:KOrder>
       <Order>
      <xsl:copy-of select="./@*" />
      <xsl:copy-of select="./*" />
      </Order>

    </ns1:KOrder>
  </xsl:template>
</xsl:stylesheet>

Yes I have already tried the above lines of code but it did not work. I have attached the xslt above. I wanted to copy all nodes and subnodes under <Order> element in the input and add namespace prefix ns0: to Output all copied nodes and subnodes

Reply With Quote
  #7 (permalink)  
Old November 17th, 2006, 10:29 AM
joefawcett's Avatar
Wrox Author
Points: 9,763, Level: 42
Points: 9,763, Level: 42 Points: 9,763, Level: 42 Points: 9,763, Level: 42
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Exeter, , United Kingdom.
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

That doesn't look anything like the code Michael suggested.

--

Joe (Microsoft MVP - XML)
Reply With Quote
  #8 (permalink)  
Old November 17th, 2006, 11:24 AM
Registered User
 
Join Date: Nov 2006
Location: , , .
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Yes Joe , I meant I have already tried the michael code before posting this issue here, it did not work or I donot know whether I am missing something and then I changed to the code above which is working i.e copies all nodes and subnodes.
Joe , do I need to use michael code in for-each looping thru each node in input?
Can you help me in modifying the xslt above

Reply With Quote
  #9 (permalink)  
Old November 17th, 2006, 11:35 AM
mhkay's Avatar
Wrox Author
Points: 18,487, Level: 59
Points: 18,487, Level: 59 Points: 18,487, Level: 59 Points: 18,487, Level: 59
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

If my code didn't work, then either it was wrong or you applied it wrongly. Either way, chucking it to one side without any attempt at debugging, writing your own code which doesn't address the requirement at all, and then asking for more help without giving any clues as to where you hit problems, is hardly a way forward.

It's a characteristic of good programmers that they solve problems, they don't give up and try something else.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
Reply With Quote
  #10 (permalink)  
Old November 17th, 2006, 04:41 PM
Registered User
 
Join Date: Nov 2006
Location: , , .
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Michael
your code should be right, might be I am applying it wrongly. when I used your code before it was copying only the text not the text. I have backup code using c# which is working .
But I wanted to do it in xslt.
I will try to figure out myself.
Thanks for the help

Michael thanks for the help,

Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Adding and Sorting nodes georgemeng XSLT 4 December 1st, 2008 12:13 PM
Adding Prefix and Namespace to XML Lerxt XSLT 6 November 8th, 2007 03:14 PM
Adding nodes to JTree sriharshareddyk Java Basics 0 May 4th, 2007 01:18 PM
Counting nodes on output bonekrusher XSLT 6 February 26th, 2007 08:19 PM
Adding nodes to grandparent of current spencer.clark XSLT 2 August 12th, 2005 09:29 AM



All times are GMT -4. The time now is 08:53 PM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.