[xsl] [XSLT 1.0] Strip unused namespaces, but retain namespace declarations for elements with QName values?

Subject: [xsl] [XSLT 1.0] Strip unused namespaces, but retain namespace declarations for elements with QName values?
From: "Costello, Roger L." <costello@xxxxxxxxx>
Date: Wed, 30 Mar 2011 13:50:32 -0400
Hi Folks,

I want to transform this:

--------------------------------------------
<t:Test xmlns:t="http://www.test.org";
        xmlns:unused="http://www.unused.org";
        xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/";>

    <t:faultcode>SOAP:client</t:faultcode>

</t:Test>
--------------------------------------------

to this:

------------------------------------
<Test xmlns="http://www.test.org";
      xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/";>

  <faultcode>SOAP:client</faultcode>

</Test>
------------------------------------

That is, I want to:

1. Replace namespace-qualified elements with a default namespace, e.g.,

   Change this: <t:Test xmlns:t="..."

   to this: <Test xmlns="..."


2. Delete unused namespace declarations, e.g.,

   Delete this: xmlns:unused="..."


3. Retain a namespace declaration if it declares a namespace prefix that is
used in an element QName value, e.g.,

   Retain this: xmlns:soap="..."

   because the prefix is used in this element's value:
<faultcode>SOAP:client</faultcode>


The following template rule does (1) and (2) but fails to implement (3):

    <xsl:template match="*[namespace-uri()]">

        <xsl:element name="{local-name()}"
                     namespace="{namespace-uri()}">
            <xsl:apply-templates select="@* | node()"/>
        </xsl:element>

    </xsl:template>

Any ideas? I am using XSLT 1.0

/Roger

Current Thread