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

November 15th, 2006, 05:31 PM
|
|
Registered User
|
|
Join Date: Nov 2006
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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
|
|

November 15th, 2006, 06:11 PM
|
|
Friend of Wrox
|
|
Join Date: Jul 2006
Posts: 430
Thanks: 28
Thanked 5 Times in 5 Posts
|
|
is ns0 the namespace?
|
|

November 15th, 2006, 07:02 PM
|
 |
Wrox Author
|
|
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
|
|
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
|
|

November 16th, 2006, 06:20 PM
|
|
Registered User
|
|
Join Date: Nov 2006
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Thanks Michael , so after doing <xsl:copy-of , I should call another xslt to Traverse thru each Nodes to add prefix ns0:?
|
|

November 16th, 2006, 06:38 PM
|
 |
Wrox Author
|
|
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
|
|
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
|
|

November 17th, 2006, 10:16 AM
|
|
Registered User
|
|
Join Date: Nov 2006
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
<?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
|
|

November 17th, 2006, 10:29 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
|
|
That doesn't look anything like the code Michael suggested.
--
Joe ( Microsoft MVP - XML)
|
|

November 17th, 2006, 11:24 AM
|
|
Registered User
|
|
Join Date: Nov 2006
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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
|
|

November 17th, 2006, 11:35 AM
|
 |
Wrox Author
|
|
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
|
|
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
|
|

November 17th, 2006, 04:41 PM
|
|
Registered User
|
|
Join Date: Nov 2006
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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,
|
|
 |