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 January 14th, 2009, 12:00 PM
Authorized User
 
Join Date: Jan 2009
Posts: 36
Thanks: 3
Thanked 0 Times in 0 Posts
Question change namespace

Hi,

I have the following xml

<Data xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:soapenc
="http://schemas.xmlsoap.org/soap/encoding/"xmlns:icns="urn:NewBusiness_XXX"
xmlns:fo
="http://www.w3.org/1999/XSL/Format">
<
createdDate>11/12/2009 at 09:21:38</createdDate>
<product_code>ABC</product_code>
</Data>

I would like to change the namespace icns to urn:NewBusiness_ABC
i.e i would like to get the following.

<Data xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:soapenc
="http://schemas.xmlsoap.org/soap/encoding/"xmlns:icns="urn:NewBusiness_ABC"
xmlns:fo
="http://www.w3.org/1999/XSL/Format">
<
createdDate>11/12/2009 at 09:21:38</createdDate>
<product_code>ABC</product_code>
</Data>

I have tried various methods but nothing works any hints ?
 
Old January 14th, 2009, 12:02 PM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

Do you use XSLT 2.0 or 1.0?
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog
 
Old January 14th, 2009, 12:07 PM
Authorized User
 
Join Date: Jan 2009
Posts: 36
Thanks: 3
Thanked 0 Times in 0 Posts
Default

i'm using

XSLT 1.0
 
Old January 14th, 2009, 12:25 PM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

Here is an XSLT 1.0 stylesheet that does the trick here for me (tested with Saxon 6.5.5):
Code:
<xsl:stylesheet
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:icns="urn:NewBusiness_ABC"
  version="1.0">
  
  <xsl:template match="@* | text() | comment() | processing-instruction()">
    <xsl:copy/>
  </xsl:template>
  
  <xsl:template match="*">
    <xsl:element name="{name()}" namespace="{namespace-uri()}">
      <xsl:copy-of select="document('')/xsl:stylesheet/namespace::*[local-name() = 'icns']"/>
      <xsl:copy-of select="namespace::*[not(local-name() = 'icns')]"/>
      <xsl:apply-templates select="@* | node()"/>
    </xsl:element>
  </xsl:template>

</xsl:stylesheet>
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog
 
Old January 14th, 2009, 12:31 PM
Authorized User
 
Join Date: Jan 2009
Posts: 36
Thanks: 3
Thanked 0 Times in 0 Posts
Default

Hi Martin,

Sorry i didn't explain clearly..
the new namespace should be a dynamic one
urn:NewBusiness_ABC
where ABC is the prodoct code from the xml
 
Old January 14th, 2009, 01:01 PM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

I am not sure that is possible. With XSLT 2.0 there is an instruction http://www.w3.org/TR/xslt20/#creating-namespace-nodes to create a namespace node but I don't know how to achieve the same with XSLT 1.0.
Maybe someone else has an idea.
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog
 
Old January 15th, 2009, 05:31 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Just change this:

<xsl:element name="{name()}" namespace="{namespace-uri()}">

to compute the namespace URI that's needed, e.g.

<xsl:element name="{name()}"
namespace="urn:NewBusiness_{../productCode}">
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
 
Old January 15th, 2009, 06:00 AM
Authorized User
 
Join Date: Jan 2009
Posts: 36
Thanks: 3
Thanked 0 Times in 0 Posts
Default

Thanks Micheal,

The solution works if i have it in a seperate style sheet. But not i the following case

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi
="http://www.w3.org/2001/XMLSchema-instance"
xmlns:soapenv
="http://schemas.xmlsoap.org/soap/envelope/"xmlns:icns="urn:NewBusiness_ATV"xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
<xsl:template match="/">
<soapenv:Envelope>
<xsl:call-template name="buildSoapHeader"/>
</soapenv:Envelope>
</xsl:template>

<xsl:template name="buildSoapHeader">
<soapenv:Header>
<icns:IplusHeader>
<icns:freeFormatText>Sample Text</icns:freeFormatText>
</icns:IplusHeader>
</soapenv:Header>
</xsl:template>


<xsl:template match="@* | text() | comment() | processing-instruction()">
<xsl:copy/>
</xsl:template>


<xsl:template match="*">
<xsl:element name="{name()}" namespace="urn:TEST">
<xsl:copy-of select="document('')/xsl:stylesheet/namespace::*[local-name() = icns']"/>
<xsl:copy-of select="namespace::*[not(local-name() = 'icns')]"/>
<xsl:apply-templates select="@* | node()"/>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
 
Old January 15th, 2009, 06:18 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

You can copy a namespace node to add an extra namespace declaration to an element, but that will never change the name of the element. The name of the element (its local name and namespace URI) are determined solely by the name and namespace attributes of the xsl:element instruction. If you need to compute the namespace URI, then the computation must be done in the namespace attribute, e.g.

namespace="{document('')/xsl:stylesheet/namespace::*[local-name() = icns']}"

(though that seems a pretty odd way to compute it, since you know at the time you write your stylesheet what namespaces it declares)
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
 
Old January 15th, 2009, 06:30 AM
Authorized User
 
Join Date: Jan 2009
Posts: 36
Thanks: 3
Thanked 0 Times in 0 Posts
Default

Thanks for the reply.

But after the transformation i'm loosing the prefix icns

i get the following..

<Print_Data xmlns="urn:Test"

i want to get the resulting xml with the prefix icns please explain how i can acheive this





Similar Threads
Thread Thread Starter Forum Replies Last Post
What is ".Namespace" ?! Employee C# 2 May 14th, 2008 10:55 AM
namespace Tomi XML 0 September 14th, 2007 04:39 AM
Namespace help CamosunStudent BOOK: ASP.NET 2.0 Website Programming Problem Design Solution ISBN: 978-0-7645-8464-0 1 October 6th, 2006 04:12 PM
change namespace leaving prefix unchanged lantoli XSLT 3 June 26th, 2006 07:31 AM
How can I change the default namespace allanhu BOOK: ASP.NET Website Programming Problem-Design-Solution 1 November 1st, 2004 01:15 PM





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