RE: [xsl] how to add a namespace to the output xml

Subject: RE: [xsl] how to add a namespace to the output xml
From: "Michael Kay" <mike@xxxxxxxxxxxx>
Date: Thu, 19 Oct 2006 18:49:07 +0100
Forget about namespace declarations, and think about namespaces as part of
the element name. When you create an element, its name (that is, local name
and namespace) are determined by the instruction that creates it. If you
create an element in a different namespace from its parent, the system won't
magically change it to be in the same namespace as its parent. Using a
literal result element like <somename/> creates an element whose local name
is somename and whose namespace is determined by the default namespace
that's in scope in the stylesheet - not by the namespace of its parent
element once added to the result tree.

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

> -----Original Message-----
> From: scarleton@xxxxxxxxx [mailto:scarleton@xxxxxxxxx] On 
> Behalf Of Sam Carleton
> Sent: 19 October 2006 18:14
> To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
> Subject: Re: [xsl] how to add a namespace to the output xml
> 
> On 10/19/06, Michael Kay <mike@xxxxxxxxxxxx> wrote:
> > You could write it in the stylesheet as:
> >
> > <SummaryViewDataSet
> >      xmlns="http://www.miltonstreet.com/SummaryViewDataSet.xsd";>
> >      <!-- more stuff here, of course --> </SummaryViewDataSet>
> 
> Michael,
> 
> I tried that, for some reason the next element, which is 
> <ScheduleLines> ends up getting an empty xmlns attribute.  
> Here is the complete XSLT:
> 
> <?xml version="1.0" encoding="UTF-8"?>
> <xsl:stylesheet version="1.0" 
> xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>
> 
>  <xsl:output method="xml" encoding="UTF-8" indent="yes"/>
> 
> 	<xsl:template match="/Report">
> 		<SummaryViewDataSet
> xmlns="http://www.miltonstreet.com/SummaryViewDataSet.xsd";>
> 			<xsl:apply-templates 
> select="schedule/schGrps/schGrp/schLine"/>
> 		</SummaryViewDataSet>
> 	</xsl:template>
> 	
> 	<xsl:template match="schLine">
>     <ScheduleLines>
>         <schGrpOID><xsl:value-of select="../@objectID"/></schGrpOID>
>         <schLineOID><xsl:value-of select="@objectID"/></schLineOID>
> 
>         <xsl:if test="../@stdOID">
>           <stdOID><xsl:value-of select="../@stdOID"/></stdOID>
>         </xsl:if>
> 
>         <schGrpType><xsl:value-of select="../@type"/></schGrpType>
>         <vialPosition><xsl:value-of 
> select="@position"/></vialPosition>
> 
>         <xsl:if test="@sampleID">
>           <sampleID><xsl:value-of select="@sampleID"/></sampleID>
>         </xsl:if>
> 
>         <xsl:if test="results/result[1]">
>           <result><xsl:value-of 
> select="results/result[1]/average"/></result>
>           <stdDev><xsl:value-of 
> select="results/result[1]/stdDev"/></stdDev>
>           <rsd><xsl:value-of select="results/result[1]/rsd"/></rsd>
>         </xsl:if>
> 
>         <xsl:if test="metadata/methodDoc">
>           <methodOID><xsl:value-of 
> select="metadata/methodDoc"/></methodOID>
>           <methodVersion><xsl:value-of
> select="metadata/methodDoc/@version"/></methodVersion>
>         </xsl:if>
> 
>         <xsl:if test="metadata/calDoc">
>           <calOID><xsl:value-of select="metadata/calDoc"/></calOID>
>           <calVersion><xsl:value-of
> select="metadata/calDoc/@version"/></calVersion>
>           <calRevision><xsl:value-of
> select="metadata/calDoc/@revision"/></calRevision>
>         </xsl:if>
> 
>         <blankOID><xsl:value-of 
> select="metadata/blankDoc"/></blankOID>
>         <blankVersion><xsl:value-of
> select="metadata/blankDoc/@version"/></blankVersion>
>         <blankRevision><xsl:value-of
> select="metadata/blankDoc/@revision"/></blankRevision>
>         <startTime><xsl:value-of 
> select="results/@shortStartTime"/></startTime>
>     </ScheduleLines>
> 	</xsl:template>	
> 	
> </xsl:stylesheet>
> 
> 
> And the output:
> 
> <?xml version="1.0" encoding="UTF-8"?>
> <SummaryViewDataSet 
> xmlns="http://www.miltonstreet.com/SummaryViewDataSet.xsd";>
> <ScheduleLines xmlns="">
> <schGrpOID>2</schGrpOID>
> <schLineOID>1</schLineOID>
> <schGrpType>SchSampleGroup</schGrpType>
> <vialPosition>4</vialPosition>
> <sampleID></sampleID>
> <methodOID>16bd8d88-7af2-4feb-8de7-0855b22eeec3</methodOID>
> <methodVersion>1</methodVersion>
> <calOID>78f0f1ec-6a4a-4d76-9955-bf48f3e7e5a4</calOID>
> <calVersion>1</calVersion>
> <calRevision>0</calRevision>
> <blankOID>242adcf2-981d-4eaf-a47d-07e1cd48b166</blankOID>
> <blankVersion>3</blankVersion>
> <blankRevision>0</blankRevision>
> <startTime>01:27 PM (-04:00)</startTime> </ScheduleLines> 
> </SummaryViewDataSet>
> 
> When I change the template to this:
> 
> <xsl:template match="/Report">
>   <SummaryViewDataSet> <!-- </SummaryViewDataSet> 
> xmlns="http://www.miltonstreet.com/SummaryViewDataSet.xsd";> -->
>     <xsl:apply-templates select="schedule/schGrps/schGrp/schLine"/>
>   </SummaryViewDataSet>
> </xsl:template>
> 
> I get this:
> 
> <?xml version="1.0" encoding="UTF-8"?>
> <SummaryViewDataSet>
> <ScheduleLines>
> <schGrpOID>2</schGrpOID>
> <schLineOID>1</schLineOID>
> <schGrpType>SchSampleGroup</schGrpType>
> <vialPosition>4</vialPosition>
> <sampleID></sampleID>
> <methodOID>16bd8d88-7af2-4feb-8de7-0855b22eeec3</methodOID>
> <methodVersion>1</methodVersion>
> <calOID>78f0f1ec-6a4a-4d76-9955-bf48f3e7e5a4</calOID>
> <calVersion>1</calVersion>
> <calRevision>0</calRevision>
> <blankOID>242adcf2-981d-4eaf-a47d-07e1cd48b166</blankOID>
> <blankVersion>3</blankVersion>
> <blankRevision>0</blankRevision>
> <startTime>01:27 PM (-04:00)</startTime> </ScheduleLines> 
> </SummaryViewDataSet>
> 
> I would post the source, but it is 2000 lines long.
> 
> Sam
> 
> P.S. Mike, I have your book, XSLT 2nd Edition, which I 
> live/died by when doing XSLT programming, thanks!
> --
> Miltonstreet Photography
> http://www.miltonstreet.com

Current Thread