Namespace nodes in XSLT and XPath

Subject: Namespace nodes in XSLT and XPath
From: Michael Fuller <msf@xxxxxxxxxxxxxxx>
Date: Wed, 28 Jun 2000 19:01:54 +1000
I'm trying to work out when and where namespace nodes are supposed
to be added to an XSLT result tree. Unfortunately, I'm finding that
it's not clear exactly what is supposed to go on when using various
combinations of xsl:element, xsl:attribute, and result tree fragments.

Where XSLT 7.1.1 "Literal Result Elements" is explicit about what
namespace nodes are included in a Result Tree when a literal elements
is instantiated; 7.1.2 "Creating Elements with xsl:element" and 7.1.3
"Creating Attributes with xsl:attribute" are not. (Possibly, their silence
is deliberate, and prescriptive.)

A simple example.

 <xsl:stylesheet version="1.0" xmlns:xsl="";>
 <xsl:template match="/">
     <xsl:element name="foo" namespace=""/>

According to XSLT 7.1.2, '' (the string that resulted
from instantiating the value of the namespace attribute as an AVT) is to be
    "used as the namespace URI of the expanded-name of the element
    to be created."
Should this be interpreted as:
    '' is to be used as the "string value" (per XPath
    5.4) of a namespace node that is to be added to the element to be
If so, then running the above against a simple source document,
such as "<doc/>", should generate:

  <out><foo xmlns=""/></out>

[Alternatively, XSLT 7.1.2 doesn't *say* to create a namespace node in the
result tree from the 'namespace' attribute, just that it should be used as
the namespace URI of the created element...  in which case it generate:
... but doesn't seem reasonable.]

But what about:

 <xsl:stylesheet version="1.0" xmlns:xsl="";>
 <xsl:template match="/">
   <out xmlns="";>
     <xsl:element name="foo" namespace=""/>

Should this generate:
  <out xmlns="";><foo/></out>
  <out xmlns="";><foo xmlns=""/></out>

Clearly, from XSLT 7.1.1, the <out> element must have a copy of
the xmlns=""; namespace node. However, the
first example would suggest that so, too, should the <foo> element...

[[Note that they are *not* equivalent, as far as XPath is concerned.
Consider count()-ing the namespace axis; alternatively, consider 
the result tree that should be generated by:

 <xsl:stylesheet version="1.0" xmlns:xsl="";>
 <xsl:template match="/">
   <out xmlns=""; xmlns:bar="";>
     <xsl:element name="foo" namespace=""/>
What would '//foo/namespace::*[2]' identify?
The namespace '' or the namespace ''?]]

Lastly, what happens when pasting result tree fragments into a result tree?
 From where are the namespace nodes taken when pasting a variable into a
tree? From the original context? from the context from which the variable
is pasted? For example, consider:

 <?xml version="1.0"?> 
 <xsl:stylesheet version="1.0" xmlns:xsl="";>
 <xsl:variable name="var1" xmlns="";>
     <xsl:element name="bar"/>
 <xsl:variable name="var2" xmlns:qux="";>
     <xsl:element name="qux:bar"/>
 <xsl:variable name="var3" xmlns:guts="";>
     <xsl:element name="guts:bar"/>
 <xsl:template match="/">
     <out1 xmlns=""; xmlns:qux="";>
      Var 1: <xsl:copy-of select="$var1"/>
      Var 2: <xsl:copy-of select="$var2"/>
      Var 3: <xsl:copy-of select="$var3"/>

XT produces:
<?xml version="1.0" encoding="utf-8"?>
     <out1 xmlns:qux=""; xmlns="";>
          Var 1: <bar xmlns=""/>
          Var 2: <qux:bar xmlns=""/>
          Var 3: <guts:bar xmlns:guts=""; xmlns=""/></out1>.

which seems to involve examining the dynamic context in which the variable
is pasted in order to determine which namespace nodes to insert into
the result tree...

Multimedia Databases Group, RMIT, Australia.

 XSL-List info and archive:

Current Thread