RE: [xsl] Setting the namespace in a document root element

Subject: RE: [xsl] Setting the namespace in a document root element
From: "Michael Kay" <mike@xxxxxxxxxxxx>
Date: Thu, 19 Oct 2006 10:26:47 +0100
> In the output document below I want the xmlns:gco="http:// 
> www.isotc211.org/2005/gco namespace declaration to appear in 
> the MD_Metadata root element, not in the CharacterString 
> child element(s)

As it happens this came up in another thread yesterday. The simple answer is
to use literal result elements rather than xsl:element. LRE's copy all
in-scope namespaces from the stylesheet (unless excluded using
exclude-result-prefixes), xsl:element doesn't. In any case, LRE's in my view
are easier to write and easier to read.

So change:

<xsl:element name="MD_Metadata" >

to:

<MD_Metadata xmlns:gco="http://www.isotc211.org/2005/gco";>

This then raises the question of why your <xsl:namespace> instruction didn't
do the trick. It ought to, and the output I get from Saxon 8.8 in fact
matches your desired output.

Michael Kay
http://www.saxonica.com/




> 
> Input file:
> 
> <?xml version="1.0" encoding="UTF-8"?>
> <catalog xmlns="http://www.unidata.ucar.edu/namespaces/thredds/
> InvCatalog/v1.0"
>       xmlns:xlink="http://www.w3.org/1999/xlink";
>       xmlns:xsi="http:// www.w3.org/2001/XMLSchema-instance"
>       xsi:schemaLocation="http://www.unidata.ucar.edu/namespaces/
> thredds/InvCatalog/v1.0
>       http://www.unidata.ucar.edu/schemas/thredds/InvCatalog.1.0.xsd";>
> 
>       <dataset ID="ucar.scd.vets.vg.cat" name="Meteorology 
> Navigation and Surface State Parameters" harvest="true">
>           <date type="metadataHarvested" format="YYYY-MM- 
> DD">2006-08-26</date>
>       </dataset>
> </catalog>
> 
> Style sheet:
> 
> ?xml version="1.0" encoding="UTF-8"?>
> <xsl:stylesheet version="2.0" xmlns="http://www.isotc211.org/2005/gmd";
>         xmlns:gco="http://www.isotc211.org/2005/gco";
>         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";
>         xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
>         xmlns:xlink="http://www.w3.org/1999/xlink";
>         xmlns:thredds="http://www.unidata.ucar.edu/namespaces/thredds/
> InvCatalog/v1.0"
>         xsi:schemaLocation="http://www.isotc211.org/2005/gmd
> ../2005/ gmd/gmd.xsd http://www.isotc211.org/2005/gco ../gco/gco.xsd"
>         exclude-result-prefixes="thredds" >
> 
>        <xsl:output method="xml" indent="yes" encoding="UTF-8"/>
> 
>       <xsl:template match="thredds:catalog">
>           <xsl:element name="MD_Metadata" >
>               <xsl:namespace name="gco">http://www.isotc211.org/2005/
> gco</xsl:namespace>
>               <xsl:attribute name="xsi:schemaLocation">
>                      http://www.isotc211.org/2005/gmd 
> ../gmd/gmd.xsd http://www.isotc211.org/2005/gco ../gco/gco.xsd
>               </xsl:attribute>
>               <xsl:variable name="datasetId" 
> select="thredds:dataset/ @ID"/>
>               <!-- fileIdentifier -->
>                <xsl:element name="fileIdentifier">
>                   <xsl:element name="gco:CharacterString">
>                       <xsl:value-of select="$datasetId"/>
>                   </xsl:element>
>               </xsl:element>
>           </xsl:element>
>       </xsl:template>
> </xsl:stylesheet>
> 
> Output:  (edited to add spaces and CRs for clarity)
> 
> <?xml version="1.0" encoding="UTF-8"?>
> <MD_Metadata xmlns="http://www.isotc211.org/2005/gmd";  
> xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";  
> xsi:schemaLocation="http://www.isotc211.org/2005/gmd 
> ../gmd/gmd.xsd http://www.isotc211.org/2005/gco../gco/ gco.xsd">
> 
>     <fileIdentifier>
>          <gco:CharacterString xmlns:gco="http://www.isotc211.org/2005/
> gco">ucar.scd.vets.vg.cat</gco:CharacterString>
>    </fileIdentifier>
> 
> </MD_Metadata>
> 
> Desired Output:
> 
> <?xml version="1.0" encoding="UTF-8"?>
> <MD_Metadata xmlns="http://www.isotc211.org/2005/gmd";  
> xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance
>          xmlns:gco="http://www.isotc211.org/2005/gco
>          xsi:schemaLocation="http://www.isotc211.org/2005/gmd 
> ../gmd/ gmd.xsd
>                http://www.isotc211.org/2005/gco ../gco/ gco.xsd" >
> 
>      <fileIdentifier>
> 	<gco:CharacterString">ucar.scd.vets.vg.cat</gco:CharacterString>
>      </fileIdentifier>
> 
> </MD_Metadata>
> 
> 
> 
> There are many CharacterString and other gco namespace 
> elements in the real output document, so having the namespace 
> in each is undesirable.
> 
> 
> Thanks in advance,
> 
> Michael

Current Thread