[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:37:13 -0500
Re:  [XSLT 1.0] Replace namespace prefixes?
At 2009-12-24 11:39 -0500, Costello, Roger L. wrote:
I would like an identify transform that replaces namespace prefixes with my own names.

For example, I would like to transform this:

    <attackNOW:book xmlns:attackNOW="http://www.book.org">
...
Into this:

    <bk:book xmlns:bk="http://www.book.org">
...
Or this:

    <book xmlns="http://www.book.org">
...
Is there a way to replace namespace prefixes, using XSLT 1.0?

You've asked your question incorrectly, Roger. Your question might be appropriate for a DOM forum where using the DOM one can manipulate the tree to replace information.


You should have asked "is there a way to construct a result tree with my own choice of namespace prefixes?" because in XSLT your source document is sacrosanct and read-only.

I hope the examples below help. You'll see the only difference is a single attribute.

. . . . . . . . . . Ken

T:\ftemp>type roger.xsl
<?xml version="1.0" encoding="US-ASCII"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                version="1.0">

<xsl:template match="b:*" xmlns:b="http://www.book.org">
  <xsl:element name="bk:{local-name()}" namespace="{namespace-uri(.)}">
    <xsl:apply-templates select="@*|node()"/>
  </xsl:element>
</xsl:template>

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

</xsl:stylesheet>

T:\ftemp>xslt roger.xml roger.xsl
<?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:date>2006</bk:date>
    <bk:ISBN>1-57851-777-X</bk:ISBN>
    <bk:publisher>Harvard Business School Press</bk:publisher>
    <bk:cost currency="USD">29.95</bk:cost>
</bk:book>
T:\ftemp>xslt roger.xml roger2.xsl
<?xml version="1.0" encoding="utf-8"?><book xmlns="http://www.book.org">
    <title>The Origin of Wealth</title>
    <author>Eric D. Beinhocker</author>
    <date>2006</date>
    <ISBN>1-57851-777-X</ISBN>
    <publisher>Harvard Business School Press</publisher>
    <cost currency="USD">29.95</cost>
</book>
T:\ftemp>type roger.xsl
<?xml version="1.0" encoding="US-ASCII"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                version="1.0">

<xsl:template match="b:*" xmlns:b="http://www.book.org">
  <xsl:element name="bk:{local-name()}" namespace="{namespace-uri(.)}">
    <xsl:apply-templates select="@*|node()"/>
  </xsl:element>
</xsl:template>

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

</xsl:stylesheet>

T:\ftemp>type roger2.xsl
<?xml version="1.0" encoding="US-ASCII"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                version="1.0">

<xsl:template match="b:*" xmlns:b="http://www.book.org">
  <xsl:element name="{local-name()}" namespace="{namespace-uri(.)}">
    <xsl:apply-templates select="@*|node()"/>
  </xsl:element>
</xsl:template>

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

</xsl:stylesheet>

T:\ftemp>diff roger.xsl roger2.xsl
6c6
<   <xsl:element name="bk:{local-name()}" namespace="{namespace-uri(.)}">
---
>   <xsl:element name="{local-name()}" namespace="{namespace-uri(.)}">

T:\ftemp>

--
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

PURCHASE STYLUS STUDIO ONLINE TODAY!

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.
Email
First Name
Last Name
Company
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.