Re: [xsl] [XSLT 1.0] Replace namespace prefixes?

Subject: Re: [xsl] [XSLT 1.0] Replace namespace prefixes?
From: "G. Ken Holman" <gkholman@xxxxxxxxxxxxxxxxxxxx>
Date: Thu, 24 Dec 2009 12:37:13 -0500
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