xsl-list
[Top] [All Lists]

Re: [xsl] default or no namespace

2010-12-28 08:59:38
Hi Andrew, All,

Interesting.  I think that I need a little more though.

Does it mean that I should do something like:

<xsl:variable name="page">
<html>
        ...
</html>
</xsl:variable>
<xsl:choose>
<xsl:when test="$cond">
<xsl:apply-templates mode="put-in-xhtml-ns" select="$page"/>
</xsl:when>
<xsl:otherwise>
<xsl:copy-of select=$page"/>
</xsl:otherwise>
</xsl:choose>

and either (please confirm which, if any):

<xsl:template mode="put-in-xhtml-ns" match="*">
<xsl:copy>
<xsl:copy-of select="@*"/>
<xsl:apply-templates mode="#current"/>
</xsl:copy>
</xsl:template>

<xsl:template mode="put-in-xhtml-ns" match="html">
<xsl:element name="html">
<xsl:namespace name="xhtml">http://www.w3.org/1999/xhtml</xsl:namespace>
<xsl:copy-of select="@*"/>
<xsl:apply-templates mode="#current"/>
</xsl:copy>
</xsl:template>

or
<xsl:template mode="put-in-xhtml-ns" match="*">
<xsl:element name="{QName('http://www.w3.org/1999/xhtml', local-name(.))}">
<xsl:copy-of select="@*"/>
<xsl:apply-templates mode="#current"/>
</xsl:copy>
</xsl:template>

Why not something simpler as just
<xsl:element name="
  {QName(if ($cond) then 'http://www.w3.org/1999/xhtml' else '', 'html')}">
  ...
</xsl:element>


What would be best?


Thank you.

Regards,
ac




On 28 December 2010 07:02, ac<ac(_at_)hyperbase(_dot_)com>  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
...
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:otherwise>
<html>
You can't do either because the namespace is applied at the xml level,
not at the xslt level.  In other words, the default namespace would
only be changed for the children of<html>  in the xml that is your
stylesheet, not in the constructed result.

The usual technique is to have an identity transform as the last step
to put the elements into the xhtml namespace (use modes if you want to
keep it all in a single transform).



--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--