[xsl] Global namespace prefixes (Corrected Examples)

Subject: [xsl] Global namespace prefixes (Corrected Examples)
From: Nathan Potter <ndp@xxxxxxxxxxxxxxxxxxxx>
Date: Thu, 13 Nov 2008 15:20:54 -0800
(I post this earlier and bungled it. My examples where broken, and I managed to attach it to an existing thread. Here's a better effort.)


Greetings,


The crux of my question is this:

Is there a way to get namespace prefix declarations into the root element of the result tree through the XSLT API? Or must each namespace prefix declared in the root element of the result tree be present in the xsl:stylesheet element of the transform?



I have the situation that I need to use (create elements in) a namespace whose name is contained in, but not a declared namespace of, the input document for my XSLT.



Input:

<?xml version="1.0" encoding="UTF-8"?>
<Dataset xmlns="http://xml.opendap.org/ns/DAP/3.2#"; base="http://base.document ">
<Attribute name="Conventions" >CF-1.0</Attribute>
<Attribute name="logname" >olson</Attribute>
<Attribute name="host" >bb0001en</Attribute>
</Dataset>




XSLT:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
xmlns:dap="http://xml.opendap.org/ns/DAP/3.2#";
>
<xsl:output method='xml' version='1.0' encoding='UTF-8' indent='yes'/>


   <xsl:template match="dap:Dataset" >
        <output>
            <xsl:apply-templates />
        </output>
   </xsl:template>

<xsl:template match="dap:Attribute" >
<xsl:element name="{@name}" namespace="{/dap:Dataset/@base}/ att#"><xsl:value-of select="." /></xsl:element>
</xsl:template>


</xsl:stylesheet>



And it works. Output:

<?xml version="1.0" encoding="UTF-8"?>
<output xmlns:dap="http://xml.opendap.org/ns/DAP/3.2#";>
<ns0:Conventions xmlns:ns0="http://base.document/att#";>CF-1.0</ ns0:Conventions>
<ns1:logname xmlns:ns1="http://base.document/att#";>olson</ ns1:logname>
<ns2:host xmlns:ns2="http://base.document/att#";>bb0001en</ns2:host>
</output>



However this bit of creating a new prefix for every element - even though the namespace is the same - is a bit of a drag from a readability standpoint (I end up with hundreds of these prefixes).


From what I can tell the only way to get a single, global, namespace prefix into the result tree is for it to be declared in the xsl:stylesheet element of the transform.

Is that true?

If so, then I think that means I need to modify the xsl:stylesheet document for each document processed.

Is that a reasonable design pattern?


Nathan



============================================================ Nathan Potter Oregon State University, COAS ndp at coas.oregonstate.edu 104 Ocean. Admin. Bldg. 541 737 2293 voice Corvallis, OR 97331-5503 541 737 2064 fax

Current Thread