Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > XML > XSLT
Password Reminder
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 Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old October 20th, 2010, 11:37 AM
Registered User
 
Join Date: Jun 2010
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default Adding the namespaces using XSLT

Hi All,

I had a requirement of adding the namespaces to the input XML using XSLT. I am novice to XSLT and with the knowledge from the XSLT2.0 book, i came up with following XSLT to achieve the same.

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:param name="uri" />

<xsl:template match="comment()|processing-instruction()|/">
<xsl:copy>
<xsl:apply-templates />
</xsl:copy>
</xsl:template>
<xsl:template match="*">
<xsl:element name="fnz1:{local-name()}" namespace="{$uri}">
<xsl:apply-templates />
</xsl:element>
</xsl:template>
</xsl:stylesheet>

But because of this namespace is coming on each element.

input XML is shown below and uri variable is http://www.namspace.com
<RootElement>
<element1> </element1>
<element2> </element2>
</RootElement>

<RootElement xmlns:fnz1="http://www.namspace.com">
<element1 xmlns:fnz1="http://www.namspace.com"> </element1>
<element2 xmlns:fnz1="http://www.namspace.com"> </element2>
</RootElement>


Please let me know how can i resolve this issue and i had to use only XSLT1.0.

Thanks & Regards
Siva
Reply With Quote
  #2 (permalink)  
Old October 20th, 2010, 11:51 AM
Friend of Wrox
Points: 6,676, Level: 34
Points: 6,676, Level: 34 Points: 6,676, Level: 34 Points: 6,676, Level: 34
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Nov 2007
Location: Germany
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

Which XSLT processor do you use? When I use Saxon 6.5.5 which is an XSLT 1.0 processor then I get
Code:
<?xml version="1.0" encoding="utf-8"?><fnz1:RootElement xmlns:fnz1="http://example.com">
<fnz1:element1> </fnz1:element1>
<fnz1:element2> </fnz1:element2>
</fnz1:RootElement>
as the output.
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog
Reply With Quote
  #3 (permalink)  
Old October 20th, 2010, 12:02 PM
mhkay's Avatar
Wrox Author
Points: 18,487, Level: 59
Points: 18,487, Level: 59 Points: 18,487, Level: 59 Points: 18,487, Level: 59
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

A conformant processor is perfectly entitled to include redundant namespace declarations in the output, but it's not a very smart thing to do. Find a better processor.
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
Reply With Quote
  #4 (permalink)  
Old October 20th, 2010, 12:04 PM
samjudson's Avatar
Friend of Wrox
Points: 8,687, Level: 40
Points: 8,687, Level: 40 Points: 8,687, Level: 40 Points: 8,687, Level: 40
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2007
Location: Newcastle, , United Kingdom.
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

If you notice though, the namespace in his output has a prefix, which the elements don't - therefore they aren't actually in that namespace.

So his processor is definitely not producing the correct output.
__________________
/- Sam Judson : Wrox Technical Editor -/

Think before you post: What have you tried?
Reply With Quote
  #5 (permalink)  
Old October 29th, 2010, 07:22 AM
Registered User
 
Join Date: Jun 2010
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi,

First my apologies as i did not put the correct xslt i am using. Following is the XSLT that i am using.

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:param name="nameSpaceURI" />
<xsl:template match="comment()|processing-instruction()|/">
<xsl:copy>
<xsl:apply-templates />
</xsl:copy>
</xsl:template>
<xsl:template match="input">
<xsl:element name="fnz:{local-name()}" namespace="http://www.namespace1.com">
<xsl:apply-templates />
</xsl:element>
</xsl:template>
<xsl:template match="*">
<xsl:element name="fnz1:{local-name()}" namespace="{$nameSpaceURI}">
<xsl:apply-templates />
</xsl:element>
</xsl:template>
</xsl:stylesheet>


And following is the input that i am sending and along with value for parameter namespaceURI. The input element will assume one namespace and other elements assumes another namespace.
<input>
<element1>
<element2>
</input>

With the above XSLT and input, the namespace is being redefined for each and every element within the input element like below.
<fnz:input xmlns:fnz:"http://www.namespace1.com">
<fnz1:element1 xmlns:fnz1:"http://www.namespace2.com">
<fnz1:element2 xmlns:fnz1:"http://www.namespace2.com">
</input>


Thanks & Regards
Siva
Reply With Quote
  #6 (permalink)  
Old October 29th, 2010, 07:37 AM
mhkay's Avatar
Wrox Author
Points: 18,487, Level: 59
Points: 18,487, Level: 59 Points: 18,487, Level: 59 Points: 18,487, Level: 59
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

And (let me check) what you want is for the namespace xmlns:fnz1:"http://www.namespace2.com" to be declared on the outermost element fnz:input even though it is not actually used at that level?

In XSLT 2.0, putting an "unused" namespace on an element can easily be achieved using the xsl:namespace instruction. In XSLT 1.0 you need to be a bit more devious. The technique is to create a dummy element in this namespace and then copy the namespace node:

Code:
<xsl:variable name="dummy">
  <xsl:element name="fnz1:dummy" namespace="{$namespaceURI}"/>
</xsl:variable>
<fnz:input>
  <xsl:copy-of select="$dummy//namespace::fnz1"/>
  ...
</fnz:input>
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
adding up values in xslt northwind XSLT 21 February 17th, 2017 09:07 AM
XML, Namespaces and XSLT, Oh, My! mphare XSLT 5 August 4th, 2010 02:27 PM
xslt adding text blitzer XSLT 3 May 19th, 2007 10:10 PM
xslt adding image palli2004 XSLT 2 December 30th, 2006 06:35 PM
adding elements in XSLT 1.0 spencer.clark XSLT 3 July 25th, 2005 09:41 AM



All times are GMT -4. The time now is 02:17 AM.


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