Re: [xsl] yet another namespace question (unwanted xmlns=""

Subject: Re: [xsl] yet another namespace question (unwanted xmlns=""
From: Michael Kay <mike@xxxxxxxxxxxx>
Date: Sat, 14 Aug 2010 15:37:00 +0100
The xmlns="" is indeed unnecessary, but there's no way of knowing that it's unnecessary, because of the possibility of QNames-in-content. For a literal result element like <svg:text>, the processor is required to ensure that all the namespace prefix bindings in scope for the LRE in the stylesheet are also in scope for the corresponding element in the result tree, and this includes the binding ("", ""). As Martin points out, moving the xmlns="http://www.w3.org/1999/xhtml"; to the outermost level will fix this, because this makes the binding ("", "http://www.w3.org/1999/xhtml";) in-scope for the svg:text element, and as it's already in scope for the <svg:text> element in the result document, no further namespace declaration needs to be added.

Michael Kay
Saxonica


On 14/08/2010 15:09, Birnbaum, David J wrote:
Dear XSL-List,

The input file to the transformation below is not in any namespace, and I'm generating xhtml with in-line svg. The stylesheet below is just a development test; all it does for the moment is count and print (as<svg:text>  elements) the position numbers of the nine chapter elements in the input document. The stylesheet works, but it creates xmlns="" output on the svg:text nodes, which, as far as I can tell, are unnecessary:
__

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"; xmlns:svg="http://www.w3.org/2000/svg";
     version="2.0">
     <xsl:output method="xml" indent="yes"/>
     <xsl:template match="/">
         <html xmlns="http://www.w3.org/1999/xhtml"; xmlns:svg="http://www.w3.org/2000/svg";>
             <head>
                 <title>Title goes here</title>
                 <meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>
             </head>
             <body>
                 <h1>Title goes here</h1>
                 <svg:svg width="100%" height="100%">
                     <xsl:apply-templates select="//chapter"/>
                 </svg:svg>
             </body>
         </html>
     </xsl:template>
     <xsl:template match="chapter">
         <xsl:variable name="xPos" select="position()"/>
         <svg:text x="{$xPos}%" y="50%">
             <xsl:value-of select="$xPos"/>
         </svg:text>
     </xsl:template>
</xsl:stylesheet>
__

The output (Saxon), when I feed it my xml with 9<chapter>  elements, is:
__

<?xml version="1.0" encoding="UTF-8"?>
<html xmlns="http://www.w3.org/1999/xhtml"; xmlns:svg="http://www.w3.org/2000/svg";>
    <head>
       <title>Title goes here</title>
       <meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>
    </head>
    <body>
       <h1>Title goes here</h1>
       <svg:svg width="100%" height="100%">
          <svg:text xmlns="" x="1%" y="50%">1</svg:text>
          <svg:text xmlns="" x="2%" y="50%">2</svg:text>
          <svg:text xmlns="" x="3%" y="50%">3</svg:text>
          <svg:text xmlns="" x="4%" y="50%">4</svg:text>
          <svg:text xmlns="" x="5%" y="50%">5</svg:text>
          <svg:text xmlns="" x="6%" y="50%">6</svg:text>
          <svg:text xmlns="" x="7%" y="50%">7</svg:text>
          <svg:text xmlns="" x="8%" y="50%">8</svg:text>
          <svg:text xmlns="" x="9%" y="50%">9</svg:text>
       </svg:svg>
    </body>
</html>
__

Since the<svg:text> elements all have a namespace prefix that is defined in the<html> root element, the inclusion of:

xmlns=""

while harmless with respect to what is rendered in the browser for the user, seems unnecessary. I can make it go away by changing the<chapter>  template to:
__

     <xsl:template match="chapter">
         <xsl:variable name="xPos" select="position()"/>
         <svg:text x="{$xPos}%" y="50%" xmlns="http://www.w3.org/2000/svg"; xsl:exclude-result-prefixes="#default">
             <xsl:value-of select="$xPos"/>
         </svg:text>
     </xsl:template>
__

This creates the output that I think I want, with<svg:text>  elements like:
__

<svg:text x="1%" y="50%">1</svg:text>
__

Not a big deal at the moment, perhaps, but I would prefer not to have to suppress a namespace definition that appears to be irrelevant to the output separately, explicitly, and individually for every element I create in the svg namespace. Aside from the aesthetics, as the stylesheet grows beyond being a frustrating namespace exercise into something that generates useful output, there emerges a real development and maintenance cost. What I'd like to be able to do is include a declaration only once in the stylesheet, so that it will then apply automatically to exclude the xmlns="" where it isn't needed, that is, on elements in the svg namespace.

I've read, tinkered, and googled, but I seem to keep missing the solution. Since the default namespace isn't being used and the namespace of the literal result element is specified by a prefix, is there a graceful general (global) way to prevent the stylesheet from generating the superfluous xmlns="" output on what will eventually become more complex in-line svg?

Thanks,

David
djbpitt@xxxxxxxx

Current Thread