p2p.wrox.com Forums

Need to download code?

View our list of code downloads.


Go Back   p2p.wrox.com Forums > XML > XSLT
I forgot my password Register Now
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 p2p Programmer to Programmer discussion community. This is a community of more than 40,000 computer programmers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining our free Wrox p2p community you can post your own programming questions and respond to other programmers’ questions. Registered users also don't have to see the ads that are displayed to guests. Registration is fast, simple and absolutely free so please, join today!
Join today and post to win prizes! Post more to increase your chances of being Wrox’s top poster of the month.

Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old January 14th, 2009, 12:00 PM
Authorized User
Points: 95, Level: 1
Points: 95, Level: 1 Points: 95, Level: 1 Points: 95, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jan 2009
Posts: 23
Thanks: 2
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 ?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
  #2 (permalink)  
Old January 14th, 2009, 12:02 PM
Friend of Wrox
Points: 2,934, Level: 22
Points: 2,934, Level: 22 Points: 2,934, Level: 22 Points: 2,934, Level: 22
Activity: 94%
Activity: 94% Activity: 94% Activity: 94%
 
Join Date: Nov 2007
Location: Germany
Posts: 626
Thanks: 0
Thanked 88 Times in 88 Posts
Default

Do you use XSLT 2.0 or 1.0?
__________________
Martin Honnen
Microsoft MVP - XML
My blog
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
  #3 (permalink)  
Old January 14th, 2009, 12:07 PM
Authorized User
Points: 95, Level: 1
Points: 95, Level: 1 Points: 95, Level: 1 Points: 95, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jan 2009
Posts: 23
Thanks: 2
Thanked 0 Times in 0 Posts
Default

i'm using

XSLT 1.0
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
  #4 (permalink)  
Old January 14th, 2009, 12:25 PM
Friend of Wrox
Points: 2,934, Level: 22
Points: 2,934, Level: 22 Points: 2,934, Level: 22 Points: 2,934, Level: 22
Activity: 94%
Activity: 94% Activity: 94% Activity: 94%
 
Join Date: Nov 2007
Location: Germany
Posts: 626
Thanks: 0
Thanked 88 Times in 88 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
My blog
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
  #5 (permalink)  
Old January 14th, 2009, 12:31 PM
Authorized User
Points: 95, Level: 1
Points: 95, Level: 1 Points: 95, Level: 1 Points: 95, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jan 2009
Posts: 23
Thanks: 2
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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
  #6 (permalink)  
Old January 14th, 2009, 01:01 PM
Friend of Wrox
Points: 2,934, Level: 22
Points: 2,934, Level: 22 Points: 2,934, Level: 22 Points: 2,934, Level: 22
Activity: 94%
Activity: 94% Activity: 94% Activity: 94%
 
Join Date: Nov 2007
Location: Germany
Posts: 626
Thanks: 0
Thanked 88 Times in 88 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
My blog
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
  #7 (permalink)  
Old January 15th, 2009, 05:31 AM
mhkay's Avatar
Wrox Author
Points: 12,642, Level: 48
Points: 12,642, Level: 48 Points: 12,642, Level: 48 Points: 12,642, Level: 48
Activity: 97%
Activity: 97% Activity: 97% Activity: 97%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 3,900
Thanks: 0
Thanked 80 Times in 78 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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
  #8 (permalink)  
Old January 15th, 2009, 06:00 AM
Authorized User
Points: 95, Level: 1
Points: 95, Level: 1 Points: 95, Level: 1 Points: 95, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jan 2009
Posts: 23
Thanks: 2
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>
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
  #9 (permalink)  
Old January 15th, 2009, 06:18 AM
mhkay's Avatar
Wrox Author
Points: 12,642, Level: 48
Points: 12,642, Level: 48 Points: 12,642, Level: 48 Points: 12,642, Level: 48
Activity: 97%
Activity: 97% Activity: 97% Activity: 97%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 3,900
Thanks: 0
Thanked 80 Times in 78 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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
  #10 (permalink)  
Old January 15th, 2009, 06:30 AM
Authorized User
Points: 95, Level: 1
Points: 95, Level: 1 Points: 95, Level: 1 Points: 95, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jan 2009
Posts: 23
Thanks: 2
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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
What is ".Namespace" ?! Employee C# 2 May 14th, 2008 11:55 AM
namespace Tomi XML 0 September 14th, 2007 05: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 05:12 PM
change namespace leaving prefix unchanged lantoli XSLT 3 June 26th, 2006 08: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



All times are GMT -4. The time now is 08:53 PM.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
© 2008 Wiley Publishing, Inc