Re: [xsl] empty namespace added on elements E.g. <h4 xmlns="">

Subject: Re: [xsl] empty namespace added on elements E.g. <h4 xmlns="">
From: Andrew Welch <andrew.j.welch@xxxxxxxxx>
Date: Tue, 12 Jan 2010 11:56:02 +0000
Its because namespaces are scoped in the XML-that-is-the-xslt... not
the XSLT itself..

The <h4> element in your xslt is in no namespace, because its not in
scope of the <html> element where you set the default namespace.

So you have to ensure all elements added to the result tree have the
default namespace changed, so can do any of:

- move the default namespace change to a common ancestor, such as
xsl:stylesheet, but this might affect other elements such as temporary
ones in variables
- set the default namespace on the <h4> element, but this could be
untidy and leaves multiple places to update if a change is needed
- give the namespace a prefix, move it to the xsl:stylesheet, then use
the prefix for all output elements.. but of course this means using
the prefix everywhere which isnt the nicest...

cheers
andrew



2010/1/12 Robby Pelssers <robby.pelssers@xxxxxxxxx>:
> Hi all,
>
> The snippet below is an extract of a stylesheet which transforms a DITA
> map and it's topics into a xhtml page.    I stripped off most of the
> stylesheet and left in the template responsible for transforming  a
> p-topic title into a xhtml <h4>.
>
> However... I tried to validate the generated xhtml page and somehow the
> snippet below gets generated....
>
> <h4 xmlns="">4.   Marking</h4>
>
> Can someone explain what could be causing Saxon to add an empty
> namespace attribute to the <h4> tag??  I'm using Saxon 8.7.
>
>
> Kind regards,
> Robby Pelssers
>
>
> ------------------------------------------------------------------------
> ---------------
>
> <?xml version="1.0" encoding="UTF-8"?>
> <!--
>  Author: Robby Pelssers
>  This stylesheet transforms the generated DITA map/topics into 1 single
> xhtml page
> -->
>
> <xsl:stylesheet version="2.0"
>  xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
>  xmlns:xs="http://www.w3.org/2001/XMLSchema";
>  exclude-result-prefixes="xs">
>
>  <xsl:param name="contextPath"/>
>
>  <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
>
>  <xsl:template match="/">
>    <html xmlns="http://www.w3.org/1999/xhtml";>
>      <head>
>        <meta http-equiv="Content-Type"
> content="text/html;charset=UTF-8" />
>            <title>Datasheet <xsl:value-of select="Datasheet/@id"/>
> preview SPIDER</title>
>        <link type="text/css" rel="stylesheet"
> href="resource/external/css/styles.css"/>
>
>      </head>
>      <body class="yui-skin-sam">
>        <div class="preview">
>          <h2 class="p-application-title"><xsl:value-of
> select="Datasheet/@id"/></h2>
>          <xsl:apply-templates select="Datasheet/map"/>
>        </div>
>      </body>
>    </html>
>  </xsl:template>
>
>
>  <xsl:template match="p-topic/title">
>    <xsl:variable name="numberOfPrecidingTopics"
> select="count(parent::p-topic/preceding-sibling::p-topic)"/>
>    <h4><xsl:value-of select="concat($numberOfPrecidingTopics + 1, '.
> ', .)"/></h4>
>  </xsl:template>
>
>
>  <!-- copy all nodes and attributes which are not processed by one of
> available templates -->
>  <xsl:template match="@*|node()">
>    <xsl:copy>
>      <xsl:apply-templates select="@*"/>
>      <xsl:apply-templates/>
>    </xsl:copy>
>  </xsl:template>
>
> </xsl:stylesheet>
> ------------------------------------------------------------------------
> ---------------
>
>



--
Andrew Welch
http://andrewjwelch.com
Kernow: http://kernowforsaxon.sf.net/

Current Thread