[xsl] Setting the namespace in a document root element

Subject: [xsl] Setting the namespace in a document root element
From: Michael Burek <mburek@xxxxxxxx>
Date: Wed, 18 Oct 2006 19:39:31 -0600
I have a question that has been puzzling me and some of my colleagues. I've spent some time searching FAQs etc, but
haven't found this variant. If the answer can be found in an FAQ or mailing list somewhere a pointer would be appreciated. I haven't found the right jumble of search words to get me to information I need.


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)

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