RE: [xsl] Adding namespace to an XML through XSL

Subject: RE: [xsl] Adding namespace to an XML through XSL
From: "Lars Huttar" <lars_huttar@xxxxxxx>
Date: Tue, 30 Dec 2003 11:51:43 -0600
xsl:copy-of copies nodes in the source tree to equivalent
nodes in the result tree, with the same name and namespace
(and the serializer will add declarations such as xmlns="" to make
sure that Element1 is still in no namespace, just as it was in
the source tree).

In order to change the namespace during the transform, you
can use an identity transform, modified to change the namespace
of each element. E.g.:

<?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" version="1.0" encoding="UTF-8" indent="yes"/>

  <xsl:template match="*" priority="3">
    <xsl:element name="{local-name()}" namespace="Something">
      <xsl:apply-templates select="@*|node()" />
    </xsl:element>
  </xsl:template>

  <xsl:template match="@*|node()">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
  </xsl:template>
</xsl:stylesheet>

HTH,
Lars

> -----Original Message-----
> From: owner-xsl-list@xxxxxxxxxxxxxxxxxxxxxx
> [mailto:owner-xsl-list@xxxxxxxxxxxxxxxxxxxxxx]On Behalf Of Kanthi
> Sent: Tuesday, December 30, 2003 10:06 AM
> To: XSL-List@xxxxxxxxxxxxxxxxxxxxxx
> Subject: [xsl] Adding namespace to an XML through XSL
> 
> 
> Hi there - 
> 
> I am having problems adding a namespace using an xsl
> to an xml with no namespaces.  
> 
> My sample xml looks like this
> <MainElement>
>   <Element1>
>    <Element2>
>      <Element3> 
>       :
>       :
>      </Element3>
>    </Element2>
>   </Element1>
> </MainElement>
> 
> And I want to add a namespace to the <MainElement
> xmlns="Something"> without any prefix.
> 
> I am doing the following in my xsl
> <?xml version="1.0" encoding="UTF-8"?>
> <xsl:stylesheet version="1.0"
> xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
> xmlns="Something">
> <xsl:output method="xml" version="1.0"
> encoding="utf-8" indent="yes"
> omit-xml-declaration="yes"/>
> 
> <xsl:template match="MainElement">
> <MainElement xmlns="Something">
>   <xsl:copy-of select="."/>
> </MainElement>	
> </xsl:template>
> 
> </xsl:stylesheet>
> 
> But the problem is that <Element1> looks like this
> <Element1 xmlns="">
> 
> Any ideas on how to get round this?  I am not exactly
> sure how to use "xsl:namespace-alias" for this or
> whether that will help?
> 
>  
> Thanks
> Kanthi
> 
> __________________________________
> Do you Yahoo!?
> Find out what made the Top Yahoo! Searches of 2003
> http://search.yahoo.com/top2003
> 
>  XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
> 

 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Current Thread