Re: [xsl] Re: Removing namespaces from source document (long)

Subject: Re: [xsl] Re: Removing namespaces from source document (long)
From: "Christopher R. Maden" <crism@xxxxxxxxx>
Date: Mon, 08 Apr 2002 01:12:06 -0700
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

At 00:15 8/4/02, Dimitre Novatchev wrote:
>   <xsl:template match="*">
>     <xsl:element name="{name()}" namespace="{namespace-uri(.)}">
>       <xsl:copy-of select="namespace::*[name() != 'bad-ns']" />
>       <xsl:apply-templates select="node()|@*" />
>     </xsl:element>
>   </xsl:template>

I was almost there...

This example still requires hard-coding of the specific namespaces that 
aren't desired - in otherwords, it's a sort of blacklist filter.  It's also 
possible to do this constructively:

<!-- For all elements: -->
<xsl:template match="*">
   <xsl:choose>

     <!-- If there is no prefix, and no default namespace in
          scope: -->
     <xsl:when test="substring-before(name(current()),':') = '' and
                     not(namespace::*[name() = ''])">

       <!-- Just create the element with no namespace. -->
       <xsl:element name="{name()}">
         <xsl:apply-templates select="node()|@*" />
         </xsl:element>
     </xsl:when>

     <!-- If there's no prefix, but there is a default namespace
          in scope: -->
     <xsl:when test="substring-before(name(current()),':') = ''">

       <!-- Make the element with the default namespace. -->
       <xsl:element name="{name()}" namespace="{namespace-uri(.)}">

         <!-- Copy the default namespace. -->
         <xsl:copy-of select="namespace::*[name*() = '']"/>
         <xsl:apply-templates select="node()|@*"/>
       </xsl:element>
     </xsl:when>

     <!-- But if there is a prefix, or a default namespace: -->
     <xsl:otherwise>

       <!-- Then create the element with the right namespace, -->
       <xsl:element name="{name()}" namespace="{namespace-uri(.)}">

         <!-- and copy the namespace actually used by this
              element. -->
         <xsl:copy-of
           select="namespace::*
                     [name() =
                        substring-before(name(current()),':')]"/>
         <xsl:apply-templates select="node()|@*" />
         </xsl:element>
     </xsl:otherwise>
   </xsl:choose>
</xsl:template>

~Chris
- -- 
Christopher R. Maden, Principal Consultant, crism consulting
DTDs/schemas - conversion - ebooks - publishing - Web - B2B - training
<URL: http://crism.maden.org/consulting/ >
PGP Fingerprint: BBA6 4085 DED0 E176 D6D4  5DFC AC52 F825 AFEC 58DA
-----BEGIN PGP SIGNATURE-----
Version: PGP Personal Privacy 6.5.8

iQA/AwUBPLFQ1qxS+CWv7FjaEQJaUgCgsqtKsA/c2a2vLgDRwt/SjInoLcEAniPG
jBUAPVXYcG8iLIAq8JEz1Gkb
=vHKz
-----END PGP SIGNATURE-----


 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Current Thread