xsl-list
[Top] [All Lists]

Re: Trouble with namespaces and running identity transform on XHTML

2004-03-10 11:03:37
--- Jeni Tennison <jeni(_at_)jenitennison(_dot_)com> wrote:
Hi James,
From your description, if an element is in any
namespace (XHTML or
MathML or whatever), you want to copy it. If it's
not in any
namespace, you want to create an element with the
same local name and
in the XHTML namespace. 

Yes, that's what I want.

To do that, you should use:

  <xsl:choose>
    <xsl:when test="namespace-uri()">
      <xsl:copy>
        <xsl:apply-templates select="@*|node()" />
      </xsl:copy>
    </xsl:when>
    <xsl:otherwise>
      <xsl:element name="{name()}"
                  
namespace="http://www.w3.org/1999/xhtml";>
        <xsl:apply-templates select="@*|node()" />
      </xsl:element>
    </xsl:otherwise>
  </xsl:choose>

Having an explicitly declared namespace attribute in
xsl:element works in xsltproc, Saxon, and Xalan, but
not Sablotron, which outputs

<ns_1:html xmlns:ns_1="http://www.w3.org/1999/xhtml";>
<ns_2:head xmlns:ns_2="http://www.w3.org/1999/xhtml";>
--snip--
</ns_2:head>
</ns_1:html>

and so on. As far as I can tell from the XSLT spec,
this is annoying but legal. If I set the default
namespace to be "http://www.w3.org/1999/xhtml"; and
leave off the namespace attribute, then Sablotron
outputs what xsltproc, Saxon, and Xalan do, which is
to not add element prefixes and just have the xmlns
(pseudo?)attribute on the html element. It is hard for
me to tell from the spec if this is the behavior that
the XSLT spec demands, or if the processors are being
merciful in not generating piles of prefixes and
namespace declarations for the result elements.

The pattern "*[local-name() = 'h1']" will match any
element whose
local name (the bit after the prefix, if there is
one) is 'h1'. That
element can be in any namespace at all, it doesn't
matter. 

That was what I was aiming for.

If you only
want to match h1 elements in the XHTML namespace,
you should declare
the XHTML namespace with a prefix (e.g. 'h') and use
the pattern
"h:h1".

Of course, if I do that, then I'd have to have
exclude-result-prefixes="h" or
xsl:exclude-result-prefixes="h" somewhere in the
stylesheet to avoid having
xmlns:h="http://www.w3.org/1999/xhtml"; in the output,
and as I've noticed, that doesn't work if the default
namespace is set to "http://www.w3.org/1999/xhtml";,
which I seem to need to do to get xsl:element to
produce the right output across XSLT processors.


__________________________________
Do you Yahoo!?
Yahoo! Search - Find what you?re looking for faster
http://search.yahoo.com

 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list