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

October 21st, 2004, 07:59 PM
|
|
Registered User
|
|
Join Date: Oct 2004
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Need to copy all nodes, attributes, and namespaces
I am using xsl:copy to make a copy of an xml document, but I need to add a namespace to a "monkey" element, in addition to whatever namespaces it already has.
How can I ensure all "monkey" elements have an added namespace?
juan
|
|

October 22nd, 2004, 01:14 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
|
|
The standard way is to create a basic copying stylesheet and then add specific templates to match any special nodes. When you say add a namespace do you mean that previously the element was in no namepsace and now it must be? In that case something like this:
Code:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<!--
Templates to match nodes that need changing go here
-->
<xsl:template match="monkey" xmlns:new="http://myNewNamespace">
<new:monkey>
<xsl:apply-templates select="node()|@*"/>
</new:monkey>
</xsl:template>
</xsl:stylesheet>
Otherwise post a small sample of your before and after xml.
--
Joe (Co-author Beginning XML, 3rd edition)
|
|

October 22nd, 2004, 12:08 PM
|
|
Registered User
|
|
Join Date: Oct 2004
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
I had the top part (standard copy) exactly the same as the one you suggest, but my node matching template was a little different. Basically I needed to insert a namespace of my own, while copying whatever namespace it already had.
You suggest:
<xsl:template match="monkey" xmlns:new="http://myNewNamespace">
<new:monkey>
<xsl:apply-templates select="node()|@*"/>
</new:monkey>
</xsl:template>
</xsl:stylesheet>
I had:
<xsl:template match="monkey">
<monkey xmlns:new="http://myNewNamespace">
<xsl:apply-templates select="node()|@*"/>
</monkey>
</xsl:template>
</xsl:stylesheet>
This latter part almost worked, but one of the namespaces' value was empty. I don't know why it wouldn't copy the namespace completely.
What does the <new:monkey... do?
|
|

October 22nd, 2004, 12:16 PM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
|
|
Quote:
quote:Originally posted by juaniux
I had the top part (standard copy) exactly the same as the one you suggest, but my node matching template was a little different. Basically I needed to insert a namespace of my own, while copying whatever namespace it already had.
You suggest:
<xsl:template match="monkey" xmlns:new="http://myNewNamespace">
<new:monkey>
<xsl:apply-templates select="node()|@*"/>
</new:monkey>
</xsl:template>
</xsl:stylesheet>
I had:
<xsl:template match="monkey">
<monkey xmlns:new="http://myNewNamespace">
<xsl:apply-templates select="node()|@*"/>
</monkey>
</xsl:template>
</xsl:stylesheet>
This latter part almost worked, but one of the namespaces' value was empty. I don't know why it wouldn't copy the namespace completely.
|
Can you show what you mean?
Quote:
quote:
What does the <new:monkey... do?
|
Creates a new element, monkey in the namespace referred to by "new" prefix.
--
Joe (Co-author Beginning XML, 3rd edition)
|
|

October 22nd, 2004, 04:39 PM
|
|
Registered User
|
|
Join Date: Oct 2004
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Mmm...I reloaded the xml and xsl and ran the transformation again, and it now works. It copies everything and inserts the new namespace in the node I specified.
If you had to name three tools for this task that come to mind, which would they be? Commercial and open source. I used Stylus, and I'm not sure I like it.
|
|
 |