Re: [xsl] namespaces problem

Subject: Re: [xsl] namespaces problem
From: Jeni Tennison <jeni@xxxxxxxxxxxxxxxx>
Date: Wed, 29 Oct 2003 17:15:07 +0000

> I'm trying to automatically generate a schema using XSLT.
> How can i please generate the attributes "targetNamespace="MyDoc" and
> xmlns:my="MyDoc" as in the example:
> ----------------------------------------------------------------------------
> <xsd:schema xmlns:xsd="";
> targetNamespace="MyDoc" xmlns:my="MyDoc" elementFormDefault="qualified">
> ----------------------------------------------------------------------------

Namespace declarations (attributes that start with xmlns) are not
really attributes, and you can't create them as if they were. Instead,
if a namespace is in-scope for the instruction that you use to create
an element, then a namespace declaration will usually be placed on the

The easiest thing to do is just use a literal result element like:

<xsd:schema xmlns:xsd="";
targetNamespace="MyDoc" xmlns:my="MyDoc"

Usually, I'd put the namespace declarations that I want to appear in
the output on the <xsl:stylesheet> element, so something like:

<xsl:stylesheet version="1.0"

<xsl:template match="/">
  <xsd:schema targetNamespace="MyDoc"

That way, they remain in-scope throughout the stylesheet, which means
that you don't run into problems with namespace un-declarations.

Note that elements generated with <xsl:element> *don't* automatically
get namespace nodes for the namespaces that are in-scope for the
instruction. This is another reason to use literal result elements
instead of generating them with <xsl:element>.



Jeni Tennison

 XSL-List info and archive:

Current Thread