RE: [xsl] Creating namespace attributes

Subject: RE: [xsl] Creating namespace attributes
From: "Michael Kay" <mike@xxxxxxxxxxxx>
Date: Thu, 18 Aug 2005 09:44:57 +0100
XSLT creates a result tree, not serialized XML: and in the tree model,
namespaces and attributes are quite distinct things. You need to create a
namespace node, not an attribute node.

In XSLT 2.0 this is easy, there is an xsl:namespace instruction which works
in a similar way to xsl:attribute.

In XSLT 1.0 it's more difficult. The only way to create a namespace node in
the result tree is by copying one either from the stylesheet or from a
source document. So the workaround is to create a temporary tree containing
an element in the relevant namespace, and then copy the namespace node from
that temporary tree (even this needs the xx:node-set() extension).

<xsl:template match="namespace">
<namespace>
<xsl:for-each select="*">
  <xsl:variable name="temp">
    <xsl:element name="{name(.)}:dummy" namespace="{.}"/>
  <xsl:variable>
  <xsl:copy-of select="xx:node-set($temp)//namespace::*"/>
</xsl:for-each>
</namespace>
</xsl:template>

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

> -----Original Message-----
> From: Arulraj [mailto:p_arulraj@xxxxxxxxx] 
> Sent: 18 August 2005 07:41
> To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
> Subject: [xsl] Creating namespace attributes
> 
> Hi,
> 
> I have the following xml
> XML:
> <?xml version="1.0" encoding="ISO-8859-1"?>
> <namespace>
> <bo>http://cal/h2wdms/common/bo/</bo>  
> <h2w>http://cal/h2w/common/bo/</h2w>
> </namespace>
> 
> I need a output like the following
> -----------------------------------
> <?xml version="1.0" encoding="UTF-8"?>
> <output><namespace
> xmlns:bo="http://cal/h2wdms/common/bo/";
> xmlns:h2w="http://cal/h2w/common/bo/"/></output>
> 
> Here is my XSL:
> <?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"/>
> <xsl:template match="namespace">
> <output>
>  <namespace>
> <xsl:for-each select="child::*"> 
>                 <xsl:variable name="NameSpace"
> select="name()"></xsl:variable> 
>                 <xsl:variable name="Location"
> select="."></xsl:variable>
>  		<xsl:variable
> name="NS"><xsl:text>xmlns:</xsl:text><xsl:value-of
> select="$NameSpace"></xsl:value-of></xsl:variable> 
>              <xsl:attribute name="{$NS}"><xsl:value-of
> select="$Location"/></xsl:attribute>
> </xsl:for-each>
>  </namespace>             
> 
> </output>
> </xsl:template>
> </xsl:stylesheet>
> 
> In the above XSL,<xsl:text>xmlns:</xsl:text> is not
> working.
> Any help?
> Thanks in advance
> Regards,
> Arul
> 
> 
> __________________________________________________
> Do You Yahoo!?
> Tired of spam?  Yahoo! Mail has the best spam protection around 
> http://mail.yahoo.com 

Current Thread