Re: [xsl] Global namespace prefixes (Corrected Examples)

Subject: Re: [xsl] Global namespace prefixes (Corrected Examples)
From: Wendell Piez <wapiez@xxxxxxxxxxxxxxxx>
Date: Fri, 14 Nov 2008 15:16:09 -0500

Here's a different approach. I don't know if the requirements allow it, but basically the idea is to create a dummy attribute in the new namespace on the document element, where the namespace will then be declared, leaving it in scope lower down, and thereby relieving the need to declare it for each of the lower-level elements.


  xmlns:xsl=""; version="1.0">

  <xsl:output method='xml' version='1.0' encoding='UTF-8'

<xsl:variable name="ns" select="/dap:Dataset/@base"/>

<xsl:variable name="prefix" select="'base'"/>

   <xsl:template match="dap:Dataset" >
          <xsl:attribute name="{$prefix}:ns" namespace="{$ns}">
            <xsl:value-of select="$ns"/>
            <xsl:apply-templates />

   <xsl:template match="dap:Attribute" >
       <xsl:element name="{$prefix}:{@name}" namespace="{$ns}">
         <xsl:value-of select="." />


On the data

<Dataset xmlns=""; base="http://base.document";>
<Attribute name="Conventions" >CF-1.0</Attribute>
<Attribute name="logname" >olson</Attribute>
<Attribute name="host" >bb0001en</Attribute>

using Xalan, I get

<output xmlns:dap=""; xmlns:base="http://base.document"; base:ns="http://base.document";>

Yes, there's a bit of scat there; I don't know if that presents a problem. But no XSLT extension, and hence no extension namespace, is required.


Wendell Piez                            mailto:wapiez@xxxxxxxxxxxxxxxx
Mulberry Technologies, Inc.      
17 West Jefferson Street                    Direct Phone: 301/315-9635
Suite 207                                          Phone: 301/315-9631
Rockville, MD  20850                                 Fax: 301/315-8285
  Mulberry Technologies: A Consultancy Specializing in SGML and XML

Current Thread