Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
| 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 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 February 24th, 2010, 01:06 AM
Authorized User
 
Join Date: Dec 2009
Posts: 23
Thanks: 3
Thanked 0 Times in 0 Posts
Default Remove Namespace from tag using tag

I have a XML file with namespace in the root element as below.(Other child element i have described in short to save space).

<T0020
xsi:schemaLocation="http://www.safersys.org/namespaces/T0020V1 T0020V1.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.safersys.org/namespaces/T0020V1">
<IRP_ACCOUNT> ....<IRP_ACCOUNT>
<IRP_ACCOUNT> ....<IRP_ACCOUNT>
</T0020>

It was huge file so i have applied xslt to split it into multiple file for that my xslt is as below.

Code:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:t="http://www.safersys.org/namespaces/T0020V1" version="2.0">
<xsl:output method="xml" indent="yes" name="xml" /> 
<xsl:variable name="accounts" select="t:T0020/t:IRP_ACCOUNT" /> 
<xsl:variable name="size" select="30" />
<xsl:template match="/">
<xsl:for-each select="$accounts[position() mod $size = 1]">
<xsl:variable name="filename" select="resolve-uri(concat('output/',position(),'.xml'))" /> 
<xsl:result-document href="{$filename}" method="xml">
<T0020>
<xsl:for-each select=". | following-sibling::t:IRP_ACCOUNT[position() &lt; $size]">
<xsl:copy-of select="." /> 
</xsl:for-each>	
</T0020>
</xsl:result-document>	
</xsl:for-each>
</xsl:template>
When i do Transformation my xml file are coming with name space in <T0020>
and <IRP_ACCOUNT> tag like below...

Code:
<T0020 xmlns:t="http://www.safersys.org/namespaces/T0020V1"><IRP_ACCOUNT xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.safersys.org/namespaces/T0020V1">
That was not needed.It should be
Code:
<T0020> <IRP_ACCOUNT></IRP_ACCOUNT><T0020>
when i applied like <t:T0020> in xslt it was same reflected with t prefix in
T0020 tag.

Can you please help me that how to remove name space from this tag ?

Thanks in Advance.
 
Old March 2nd, 2010, 08:14 AM
Authorized User
 
Join Date: Feb 2009
Location: Gurgaon
Posts: 31
Thanks: 6
Thanked 1 Time in 1 Post
Send a message via MSN to anil_yadav26@hotmail.com
Default

Hi,

I have run the given XSLT but its not working on my machine...Anyway you can use the attribute exclude-result-prefixes="xsl t xsi" in <xsl:stylesheet>

Regards,
Anil Yadav
 
Old March 3rd, 2010, 05:10 AM
Authorized User
 
Join Date: Dec 2009
Posts: 23
Thanks: 3
Thanked 0 Times in 0 Posts
Default

Thanks for reply...

It has remved namespace from <T20> tag,but it does not remove from <IRP_ACCOUNT> what i need to change in order to remove namespace from <IRP_ACCOUNT> ??
 
Old March 3rd, 2010, 05:38 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

The IRP_ACCOUNT element is copied from the input, so it will be in the same namespace as in the input. If you want to change the namespace of this element, you can't use xsl:copy-of, you need to create a new element using either a literal result element or xsl:element.
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
The Following User Says Thank You to mhkay For This Useful Post:
nisargmca (March 19th, 2010)
 
Old March 18th, 2010, 08:01 AM
Authorized User
 
Join Date: Dec 2009
Posts: 23
Thanks: 3
Thanked 0 Times in 0 Posts
Default

Michael ,I am using XSLT 2.0 then <xsl:copy-of select="." copy-namespaces="no"/>
should remove namespace from my result xml document but it is not removing what should be the problem ?


Note : I have taken reference from your answer from the post
Remove root namespace help?

Thanks
Nisarg
 
Old March 18th, 2010, 08:19 AM
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

copy-namespaces="no" only removes "unused" namespaces. As Michael has already said your IRP_ACCOUNT element in your input is in the safersys namespace, so when you use <xsl:copy-of> you are also copying that namespace to the output element.
__________________
/- Sam Judson : Wrox Technical Editor -/

Think before you post: What have you tried?
The Following User Says Thank You to samjudson For This Useful Post:
nisargmca (March 19th, 2010)
 
Old March 18th, 2010, 08:32 AM
Authorized User
 
Join Date: Dec 2009
Posts: 23
Thanks: 3
Thanked 0 Times in 0 Posts
Default

Thanks Sam, i got it.I think i have to use another trick for that.
 
Old March 19th, 2010, 01:30 AM
Authorized User
 
Join Date: Dec 2009
Posts: 23
Thanks: 3
Thanked 0 Times in 0 Posts
Default

Quote:
Originally Posted by mhkay View Post
The IRP_ACCOUNT element is copied from the input, so it will be in the same namespace as in the input. If you want to change the namespace of this element, you can't use xsl:copy-of, you need to create a new element using either a literal result element or xsl:element.
Thanks Michael ,my problem is solved with the way u suggest otherwise i will continue dig into why namespace is not getting removed with copy .

Following what i have changed to xsl.


Code:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:t="http://www.safersys.org/namespaces/T0026V1" 
	exclude-result-prefixes="xsl t" version="2.0">
	<xsl:output method="xml" indent="yes" name="xml" /> 
	<xsl:param name="dir"/>
	<xsl:param name="xmlFileName"/>
	<xsl:param name="size"/>
	<xsl:variable name="accounts" select="t:T0026/t:IRP_ACCOUNT" />
	<xsl:template match="/">
		<xsl:for-each select="$accounts[position() mod $size = 1]">
			<xsl:variable name="filename" select="resolve-uri(concat($dir,'/',$xmlFileName,'_',position(),'.xml'))" /> 
			<xsl:result-document href="{$filename}" method="xml">
				<T0020>
					<xsl:for-each select=". | following-sibling::t:IRP_ACCOUNT[position() &lt; $size]">
						<IRP_ACCOUNT>
							<xsl:apply-templates/>
						</IRP_ACCOUNT>
						
					</xsl:for-each>						
				</T0020>
			</xsl:result-document>				
		</xsl:for-each>
	</xsl:template>
	<xsl:template match="*">
			<xsl:text disable-output-escaping="yes">&lt;</xsl:text>
			<xsl:value-of select="name()"/>
			<xsl:text disable-output-escaping="yes">&gt;</xsl:text>
			<xsl:apply-templates/>
			<xsl:text disable-output-escaping="yes">&lt;</xsl:text>
			<xsl:text>/</xsl:text>
			<xsl:value-of select="name()"/>
			<xsl:text disable-output-escaping="yes">&gt;</xsl:text>
	</xsl:template>
</xsl:stylesheet>
Thanks
Nisarg Mehta
 
Old March 19th, 2010, 03:17 AM
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

I always find it scary when I see disable-output-escaping. this instead.

Try

Code:
<xsl:template match="*">
  <xsl:element name="{local-name()}">
    <xsl:apply-templates />
  </xsl:element>
</xsl:templates>
__________________
/- Sam Judson : Wrox Technical Editor -/

Think before you post: What have you tried?
The Following User Says Thank You to samjudson For This Useful Post:
nisargmca (March 19th, 2010)
 
Old March 19th, 2010, 06:40 AM
Authorized User
 
Join Date: Dec 2009
Posts: 23
Thanks: 3
Thanked 0 Times in 0 Posts
Default

Sam,that's cool and short. Thanks




Similar Threads
Thread Thread Starter Forum Replies Last Post
Remove root namespace help? JohnBampton XSLT 2 March 6th, 2009 06:49 AM
Tag Search and Tag Cloud Search ilayaraja SQL Server 2005 1 April 15th, 2008 05:11 AM
HTML tag from C# or ASP.NET tag from javascript angshujit ASP.NET 2.0 Basics 3 February 16th, 2007 10:07 AM
HTML tag vs Body Tag CFGerry BOOK: Beginning CSS: Cascading Style Sheets for Web Design ISBN: 978-0-7645-7642-3 1 October 7th, 2005 07:13 AM
Trying to output a tag within a tag jaucourt XSLT 3 January 12th, 2005 11:57 AM





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