[xsl] Is exclude-result-prefixes wise in XHTML-to-XHTML transformations?

Subject: [xsl] Is exclude-result-prefixes wise in XHTML-to-XHTML transformations?
From: "James J. Ramsey" <jjramsey_6x9eq42@xxxxxxxxx>
Date: Wed, 10 Mar 2004 15:48:45 -0800 (PST)
If I understand correctly--which is a non-trivial
"if"--there is technically a catch-22 in doing an XSL
transformation in which both the source and result
document use the same XML dialect with the same
non-null namespace, i.e. a transform from XHTML to
XHTML. If I were doing a transform from XHTML to some
XML dialect with a different namespace, I could add
xmlns:xhtml="http://www.w3.org/1999/xhtml"; to the
xsl:stylesheet element so that the XSLT processor
could "see" the XHTML elements in the source document,
and also add exclude-result-prefixes="xhtml" to the
xsl:stylesheet element to keep the namespace nodes
with the URI "http://www.w3.org/1999/xhtml"; out of the
result tree and unwanted namespace declarations like
xmlns:xhtml="http://www.w3.org/1999/xhtml"; out of the
serialized result document. If I am doing an XHTML to
XHTML transform, and I don't want to resort to using
patterns like "*[(name() = 'div') and (@class =
'div_I_want')]", then I need to add
xmlns:xhtml="http://www.w3.org/1999/xhtml"; to the
xsl:stylesheet element, but this will often lead to
spurious namespace declarations like
xmlns:xhtml="http://www.w3.org/1999/xhtml"; in the
serialized output. Since the result tree is XHTML and
thus supposed to have namespace nodes with the URL
"http://www.w3.org/1999/xhtml";, putting
exclude-result-prefixes="xhtml" in the xsl:stylesheet
element ought to do Bad Things(TM). Yet this
stylesheet seems to work in xsltproc, Saxon, and Xalan
(but not Sablotron, as noted on the related thread
"Trouble with namespaces and running identity
transform on XHTML"):

<xsl:transform
   xmlns:h="http://www.w3.org/1999/xhtml";
   xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
   exclude-result-prefixes="h"
   version='1.0'>

<xsl:output method="xml"/>

<xsl:template match="h:h1|h1">
<p xmlns="http://www.w3.org/1999/xhtml";>
   <xsl:apply-templates select="@*" />
   LOOK MA! <xsl:apply-templates /> NO BRAINS!
</p>
</xsl:template>

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

<xsl:template match="@*">
   <xsl:copy-of select="." />
</xsl:template>

</xsl:transform>

What I don't get is whether this works as well as it
does because the XSLT spec says it should, or if the
XSLT processors are just smart about adding back some
of the namespace nodes that were supposed to be taken
out by setting the exclude-result-prefixes attribute.


__________________________________
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


Current Thread