Subject: Re: default or no namespace
From: Piet van Oostrum <piet@xxxxxxxxxxxxxx>
Date: Tue, 28 Dec 2010 10:40:00 -0400
|
ac wrote:
> Hi,
>
> With XSLT2, I have an element that can be either in no namespace or
> in the default namespace, based on some condition, as in <html>
> ...
> </html>
>
> or
> <html xmlns="http://www.w3.org/1999/xhtml">
> ... </html>
>
> Without success, I tried things like: <html> <if test="$cond">
> <xsl:namespace name="">http://www.w3.org/1999/xhtml</xsl:namespace>,
> but cannot output a namespace node for the default namespace when the
> element is in no namespace <!-- or <xsl:namespace
> name="xmlns">http://www.w3.org/1999/xhtml</xsl:namespace>, but then
> xmlns name is not acceptable,
> or even
> <xsl:namespace>http://www.w3.org/1999/xhtml</xsl:namespace>, but the
> name attribute is required -->
> </if>
> ... </html>
>
> The following may also have been nice, if it created a namespace node
> rather than an xmlns attribute: <html xmlns="if ($cond) then
> 'http://www.w3.org/1999/xhtml' else '' ">
> ... </html>
>
>
> I understand that I could probably put the body of the html element
> in a template (e.g. html-body), define all the (tons of) required
> parameters, and do something like:
>
> <xsl:choose> <xsl:when test="$cond"> <html
> xmlns="http://www.w3.org/1999/xhtml"> <xsl:call-template
> name="html-body"> <xsl:with-param name="xxx" select="abc"/>
> ...
> </xsl:call-template>
> </html>
> </xsl:when>
> <xsl:otherwise>
> <html>
> <xsl:call-template name="html-body">
> <xsl:with-param name="xxx" select="abc"/>
> ... </xsl:call-template> </html> </xsl:otherwise>
> </xsl:choose>
>
> But, especially as this would seem like a common case, I am sincerely
> hoping that there is a better way. How should this be better
> handled?
>
What about this?
<xsl:variable name="namespace">
<xsl:if test="$cond">
<xsl:text>http://www.w3.org/1999/xhtml</xsl:text>
</xsl:if>
</xsl:variable>
<xsl:element name="html" namespace="{$namespace}">
... Content ...
</xsl:element>
--
Piet van Oostrum
Cochabamba. URL: http://pietvanoostrum.com/
Nu Fair Trade woonartikelen op http://www.zylja.com
|