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 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
 
Old October 20th, 2010, 11:51 AM
Friend of Wrox
 
Join Date: Nov 2007
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
 
Old October 20th, 2010, 12:02 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
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
 
Old October 20th, 2010, 12:04 PM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
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?
 
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
 
Old October 29th, 2010, 07:37 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
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





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





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