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 October 21st, 2004, 07:59 PM
Registered User
 
Join Date: Oct 2004
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to juaniux
Default 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
 
Old October 22nd, 2004, 01:14 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

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)
 
Old October 22nd, 2004, 12:08 PM
Registered User
 
Join Date: Oct 2004
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to juaniux
Default

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?



 
Old October 22nd, 2004, 12:16 PM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

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)
 
Old October 22nd, 2004, 04:39 PM
Registered User
 
Join Date: Oct 2004
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to juaniux
Default

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.






Similar Threads
Thread Thread Starter Forum Replies Last Post
Using param/variable to copy attributes shahbhat XSLT 2 August 27th, 2008 04:03 PM
template to copy namespaces matrixman XSLT 0 May 4th, 2007 08:21 AM
selecting nodes that lack certain attributes rmers XSLT 3 October 4th, 2006 03:44 PM
namespaces anchal C# 1 July 3rd, 2006 02:53 PM
XML DOM :modifying the existing nodes attributes i sharmasourabh54 XML 1 February 14th, 2005 05:04 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.