[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

RE: [XSLT 1.0] Replace namespace prefixes?

Subject: RE: [XSLT 1.0] Replace namespace prefixes?
From: "G. Ken Holman" <gkholman@xxxxxxxxxxxxxxxxxxxx>
Date: Thu, 24 Dec 2009 12:52:54 -0500
RE:  [XSLT 1.0] Replace namespace prefixes?
At 2009-12-24 12:42 -0500, I wrote:
At 2009-12-24 12:36 -0500, Costello, Roger L. wrote:
However, I am interested in a general solution, i.e., for *any* XML document the XSLT transform must output an XML document that is identical to the input, except all namespace prefixes have been replaced by prefixes of the XSLT's choosing.

You should have said so! :{)}

I hope the example below helps.

I didn't accommodate attributes in the namespace. Note how the processor adds a namespace prefix of its own choice in the first example run below. I added a namespace-qualified attribute in your input.

You might wonder at the predicate qualification of the element. Since any element not in a namespace cannot have a prefix, it can simply be preserved ... the template only catches those elements that are in a namespace. Similarly for attributes.

Oh, also, remember you said "all" namespace prefixes have been replaced ... I'm assuming in my solution that in your document there is at most one namespace. My code will also work if you send it a document with no namespaces ... you just don't get any prefixes in your result because you can't have a prefix for the no-namespace.

Merry Christmas and Happy New Year to all readers!

. . . . . . . . . Ken

T:\ftemp>type roger.xml
<attackNOW:book xmlns:attackNOW="http://www.book.org">
    <attackNOW:title>The Origin of Wealth</attackNOW:title>
    <attackNOW:author>Eric D. Beinhocker</attackNOW:author>
    <attackNOW:publisher>Harvard Business School Press</attackNOW:publisher>
    <attackNOW:cost currency="USD" attackNOW:other="x">29.95</attackNOW:cost>

T:\ftemp>xslt roger.xml roger3.xsl con
<?xml version="1.0" encoding="utf-8"?><book xmlns="http://www.book.org">
<title>The Origin of Wealth</title>
<author>Eric D. Beinhocker</author>
<publisher>Harvard Business School Press</publisher>
<cost xmlns:ns0="http://www.book.org" currency="USD" ns0:other="x">29.95</cost>
T:\ftemp>xslt roger.xml roger3.xsl con use-this-prefix=bk:
<?xml version="1.0" encoding="utf-8"?><bk:book xmlns:bk="http://www.book.org">
<bk:title>The Origin of Wealth</bk:title>
<bk:author>Eric D. Beinhocker</bk:author>
<bk:publisher>Harvard Business School Press</bk:publisher>
<bk:cost currency="USD" bk:other="x">29.95</bk:cost>
T:\ftemp>type roger3.xsl
<?xml version="1.0" encoding="US-ASCII"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"

<xsl:param name="use-this-prefix"/>

<xsl:template match="*[namespace-uri(.)]">
  <xsl:element name="{$use-this-prefix}{local-name()}"
    <xsl:apply-templates select="@*|node()"/>

<xsl:template match="@*[namespace-uri(.)]">
  <xsl:attribute name="{$use-this-prefix}{local-name()}"
    <xsl:value-of select="."/>

<xsl:template match="@*|node()"><!--identity for all other nodes-->
    <xsl:apply-templates select="@*|node()"/>



-- UBL and Code List training: Copenhagen, Denmark 2010-02-08/10 XSLT/XQuery/XPath training after http://XMLPrague.cz 2010-03-15/19 XSLT/XQuery/XPath training: San Carlos, California 2010-04-26/30 Vote for your XML training: http://www.CraneSoftwrights.com/s/i/ Crane Softwrights Ltd. http://www.CraneSoftwrights.com/s/ Training tools: Comprehensive interactive XSLT/XPath 1.0/2.0 video Video lesson: http://www.youtube.com/watch?v=PrNjJCh7Ppg&fmt=18 Video overview: http://www.youtube.com/watch?v=VTiodiij6gE&fmt=18 G. Ken Holman mailto:gkholman@xxxxxxxxxxxxxxxxxxxx Male Cancer Awareness Nov'07 http://www.CraneSoftwrights.com/s/bc Legal business disclaimers: http://www.CraneSoftwrights.com/legal

Current Thread


Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
First Name
Last Name
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.