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 April 17th, 2009, 08:57 AM
Registered User
Join Date: Apr 2009
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default Preserve prefix namespaces from source document


I am trying to write a xsl that renames an element. Let's say that I changed the element name at the XML Schema and want to change it in all the XML instances that I have. This would be a really simple task if there is no namespace present.
But when there is I am having the follwing problem:
-If I need to change <name:something> to <name:anything>, knowing what is the uri that is represented by name. For writing in the output somthing like: <name:something> I need to declare in the xsl the prefix name in the xsl.
But I can not know which prefixes the source is using ( each xml docuent can use a different prefix for the same url and I wouldn't want to change the original prefix).
I can obtain the prefix used in the source using fn:prefix-from-QName(fn:node-name(.)), but I am not able to declare this namespace dynamically.

Does anyone knows how to solve this problem?

Old April 17th, 2009, 09:13 AM
mhkay's Avatar
Wrox Author
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts

You haven't made your requirements clear. Are you saying you want to change the local name, but keep the prefix and namespace URI the same? If so you can try

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

and if you're lucky the system will keep the prefix, or if you want better certainty you can do

<xsl:element name="{substring-before(name(.), ':')}:newname" namespace="{namespace-uri(.)}">

Remember that prefixes don't really matter, it's only the namespace URI that's important.
Michael Kay
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
Old April 17th, 2009, 09:20 AM
Friend of Wrox
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts

What matters in your XSLT stylesheet is the namespace URI, not the prefix. So to change 'foo' elements in the namespace 'http://example.com/ns1' to 'bar' elements in the same namespace you can use e.g.

  <xsl:template match="pf:foo">
    <xsl:element name="{if (prefix-from-QName(node-name(.))) then concat(prefix-from-QName(node-name(.)), ':') else ''}bar " namespace="{namespace-uri()}">
       <xsl:apply-templates select="@* | node()"/>
  <xsl:template match="@* | node()">
      <xsl:apply-templates select="@* | node()"/>

Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog
Old April 17th, 2009, 09:28 AM
Registered User
Join Date: Apr 2009
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts

This works, thanks.

Similar Threads
Thread Thread Starter Forum Replies Last Post
preserve spaces in boundfield Ivision ASP.NET 2.0 Basics 0 April 29th, 2008 03:16 AM
Changing the Source Document mphare XSLT 2 March 14th, 2008 02:46 PM
Return to source context after document('') jkmyoung XSLT 2 June 30th, 2007 03:04 AM
Preserve Formatting on Update Squarecat Word VBA 1 September 29th, 2006 08:33 AM
JAXP without source document Hendrik XSLT 1 September 28th, 2004 03:31 AM

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