RE: [xsl] xmlns="" / XML to XHTML

Subject: RE: [xsl] xmlns="" / XML to XHTML
From: "Michael Kay" <mike@xxxxxxxxxxxx>
Date: Mon, 12 Sep 2005 23:59:07 +0100
If you've written an <html> element in the XHTML namespace, and then copy
the <h2> element from the source as a child of this, then I would expect the
xmlns="" to appear. That's because the <h2> element is in no namespace. It
must stay in the same namespace (or non-namespace) when copied, and the only
way to keep it in the non-namespace is to add an xmlns="" declaration.

I don't think you want to copy the <h2> element from the source to the
result. You need to modify the element to be in the XHTML namespace. Copying
creates an exact copy, it can't be used to change the namespace. You need a
variant of the identity transform, along the lines given,

> <xsl:template match="*">
>   <xsl:element name="{local-name()}"
> namespace="http://www.w3.org/1999/xhtml"; >
>    <xsl:apply-templates />
>   </xsl:element>
> </xsl:template>

to move the elements into the XHTML namespace.

Don't think about namespace declarations as attributes. Think of the
namespace as part of the element name. Generate elements with the correct
names (including the namespace part), and the serializer will look after the
business of putting the right namespace declarations into the output.

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


> -----Original Message-----
> From: Alexander Nichau [mailto:ml@xxxxxxxxxx]
> Sent: 12 September 2005 19:39
> To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
> Subject: [xsl] xmlns="" / XML to XHTML
>
> Hi everyone,
>
> I4m having a real confusing problem and I hope anyone can help me;
> I4m using a system which takes valid XHTML input done by the user and
> puts it out together with meta information and other stuff in
> a XML set.
>
> One example could be this one:
>
> <?xml version="1.0" ?>
> <page>
> <content>
> <topic module="content_xhtml" no="4" author="Alexander">
> <text>
> 	<h2>Heading</h2>
> 	<p>
> 	....
> 	</p>
> </text>
> </topic>
> </content>
> <meta>...</meta>
> ...
> </page>
>
>
> Now I convert this xml set using XSLT1.0 to XHTML (XSL shortened):
> <xsl:stylesheet version="1.0" xmlns="http://www.w3.org/1999/xhtml";
> xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
> xmlns:fo="http://www.w3.org/1999/XSL/Format";>
>
> <xsl:output method="xml" encoding="utf-8" standalone="yes"
> doctype-public="-//W3C//DTD XHTML 1.0 Strict//EN"
> doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd";
> indent="yes" omit-xml-declaration="yes" />
>
> ...
>
> <xsl:param name="content" select="/page/content/topic"/>
> ...
>
> <xsl:when test="$module = 'content_xhtml'">
>       <xsl:call-template name="content_xhtml"/>
> </xsl:when>
>
> <xsl:template name="content_xhtml">
>    <xsl:param name="content" select="/page/content/topic"/>
>    <xsl:copy-of select="$content/text/child::*" />
> </xsl:template>
>
>
> This results in a html page snippet:
>      <h2 xmlns="">Heading</h2>
>              <p xmlns="">....
>                <br />
> ....
> </p>
>
> So the xsl processors has added the attribute xmlns="" to the outer
> elements (h2,p), which results in an invalid XHTML page
> according to the
> w3c validator. This attribute isn4t added to the inner HTML-elements
> e.g. <br /> or <strong>
>
> I4ve been searching the web since about a week and I found an example
> which gets me rid of this attribute:
>
> <xsl:template match="text">
>    <xsl:apply-templates mode="import" />
> </xsl:template>
>
> <xsl:template mode="import" match="*">
>   <xsl:element name="{local-name()}"
> namespace="http://www.w3.org/1999/xhtml"; >
>    <xsl:apply-templates />
>   </xsl:element>
> </xsl:template>
>
>
> Unlikely, following the second example, ends up in an
> XHTML-Output where
>   inner elements got this xmlns="".
>
> So my question is, how should I modify my XSL-Transformation
> in order to
> get valid XSL-Output?
> E.g. how can I modify the second example to iterate through
> every inner
> element? Or am I on the wrong way and should solve this problem
> completely different?
>
>
> Finally one word about the software that produces this output:
>
> System: PHP Version 5.0.3 with libxml 2.6.9 and libxslt 1.1.12:
> dom
> DOM/XML enabled
> DOM/XML API Version 20031129
> libxml Version 2.6.9
> HTML Support enabled
> XPath Support enabled
> XPointer Support enabled
> Schema Support enabled
> RelaxNG Support enabled
>
> xsl
> XSL enabled
> libxslt Version 1.1.12
> libxslt compiled against libxml Version 2.6.17
> EXSLT enabled
> libexslt Version 1.1.12
>
>
> I hope someone can help me-
>
> Big thanks in advance,
>
> Alex

Current Thread