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 March 30th, 2009, 11:31 AM
Friend of Wrox
 
Join Date: Feb 2009
Posts: 119
Thanks: 25
Thanked 3 Times in 3 Posts
Default copying namespace question

I have the following input xml

Code:
<ProjectX>
    <Agent_Class>xNETports</Agent_Class>
</ProjectX>
and I need this output xml

Code:
<ProjectX xmlns="http://www.mynamespace.com">
      <Agent_Class>xNETports</Agent_Class>
</ProjectX>
I am using this xsl
Code:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
      xmlns:xs="http://www.w3.org/2001/XMLSchema"
      exclude-result-prefixes="xs"
      version="2.0">
    <xsl:output method="xml" indent="yes"></xsl:output>
    <xsl:template match="ProjectX">
        <ProjectX xmlns="http://www.mynamespace.com">
            <xsl:apply-templates select="@*|node()" />
        </ProjectX>
    </xsl:template>
    <xsl:template match="*" >
        <xsl:copy-of select="."></xsl:copy-of>
    </xsl:template>
</xsl:stylesheet>
but i get the following xml instead

Code:
<ProjectX xmlns="http://www.mynamespace.com">
      <Agent_Class xmlns="">xNETports</Agent_Class>
</ProjectX>
how do i remove the xmlns="" from the Agent_Class element??
 
Old March 30th, 2009, 11:39 AM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

In your input XML all elements are no namespace, in your desired result all elements are in that namespace http://www.mynamespace.com. So your stylesheet needs to transform all elements to the new namespace e.g.
Code:
<xsl:template match="*">
  <xsl:element name="{local-name()}" namespace="http://www.mynamespace.com">
    <xsl:apply-templates select="@* | node()"/>
  </xsl:element>
</xsl:template>
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog
The Following User Says Thank You to Martin Honnen For This Useful Post:
JohnBampton (March 30th, 2009)
 
Old March 30th, 2009, 11:52 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

>how do i remove the xmlns="" from the Agent_Class element?

You need to think about the result tree produced by the transformation, not about the lexical XML produced by the serializer. The serializer is outputting the xmlns="" because in the result tree, the Agent_Class element node is in no namespace. So you need to change your code so it doesn't put the Agent in a namespace. That means the Agent_Class node is not an exact copy of what's in the input (it has a different name!), so you can't create it using xsl:copy-of.
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference





Similar Threads
Thread Thread Starter Forum Replies Last Post
convert XML to XMl - Another Namespace question bonekrusher XSLT 2 July 10th, 2007 07:32 AM
Question for Chapter2 NameSpace and Fold structure lovehorse BOOK: ASP.NET Website Programming Problem-Design-Solution 1 October 16th, 2006 08:21 AM
Copying images arnabghosh PHP How-To 1 February 21st, 2006 07:49 AM
Copying files marclena General .NET 2 June 18th, 2004 08:24 AM
Copying a database Droobles SQL Server 2000 2 February 19th, 2004 09:13 AM





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