RE: [xsl] Adding *xmlns* attribute dynamically

Subject: RE: [xsl] Adding *xmlns* attribute dynamically
From: "Michael Kay" <mike@xxxxxxxxxxxx>
Date: Fri, 12 Jan 2007 11:38:57 -0000
The general rule here is: create your elements and attributes in the right
namespace, and the namespace declarations will look after themselves. For
example if your stylesheet says:

<xsl:element name="{$x}" namespace="{$uri}">

where $x is an unprefixed name, then the system will output a declaration of
the default namespace without any special action on your part.

There are a couple of cases where this might not be good enough:

(a) for cosmetic reasons, you might want the namespace declaration to appear
on the xs:schema element, rather than on the elements where it is actually
needed

(b) you might want to declare a namespace prefix that isn't actually used in
any element and attribute names, for example a prefix that is only used in
QName-valued attribute content.

For these situations XSLT 2.0 has an xsl:namespace instruction, which works
like xsl:attribute except that it generates a namespace node rather than an
attribute node. The namespace node will convert into a namespace declaration
when the document is serialized.

There's no direct equivalent in XSLT 1.0 but there's a well-known workaround
involving xsl:copy: to create a namespace node that binds prefix $p to uri
$u, do:

<xsl:variable name="dummy">
  <xsl:element name="{$p}dummy" namespace="{$u}"/>
</xsl:variable>

<xsl:copy-of select="xx:node-set($dummy)//namespace::*"/>

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



> -----Original Message-----
> From: Kishore Bankupelle [mailto:brkkis@xxxxxxxxx] 
> Sent: 12 January 2007 10:40
> To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
> Subject: [xsl] Adding *xmlns* attribute dynamically
> 
> Hi,
> 
> I am new to XSL and writing a XSL to transform list of XSDs 
> to another list of XSD.
> 
> The issue is, i want to assign a dynamic value for "xmlns" 
> attribute in <xs:schema>. From the mailing archieves i see 
> that i cannot add "xmlns" as an attribute. If i try below
> 
> *<xs:schema>
>     <xsl:attribute name="xmlns">
>        <xsl:value-of select="$ReqResName" />
>   </xsl:attribute>
> </xs:schema>
> *
> where "ReqResName" is a variable(<xsl:variable>), i am 
> getting the below error.
> 
> *Illegal value used for attribute name: name*
> 
> I believe this is because of adding "xmlns" as an attribute. 
> If you add the attribute "xmlns" directly to the tag, you 
> cant change the attribute value dynamically. So,
> 
> *<xs:schema version="1.0" xmlns="????????">some child 
> elemnsts</xs:schema>*
> 
> The above example does not give you a way to change the value 
> represented by "??????" dynamically. I mean to say we cant 
> access any XSL variable in the place of "????????".
> 
> So if i want to change the value of "xmlns" attribute in 
> "<xs:schema>", what is the solution?
> 
> Thanks in advance.
> 
> Regards,
> Kishore

Current Thread