Re: [xsl] should be a simple problem

Subject: Re: [xsl] should be a simple problem
From: Jeni Tennison <jeni@xxxxxxxxxxxxxxxx>
Date: Thu, 13 Dec 2001 10:16:33 +0000
Hi Tom,

> I am new to xslt. Much of the explanation of xslt (books, websites)
> discuss namespace from the .xsl perspective and doesn't give
> examples where the -IN xml document uses namespaces. My specific
> problem is that when I add a namespace, the examples break.

In your first example, the programme, opera etc. elements were all in
*no namespace* as you had no default namespace declaration (and they
weren't prefixed).

In your second example, the programme, opera etc. elements were all in
the namespace 'http://www.emilygraham.com/java/other/Opera' as that
was the namespace specified by the default namespace declaration.

When expressions or patterns select or match elements and attributes,
they select or match them according to *both* their local name (e.g.
'programme', 'opera') and their namespace.  As far as an XSLT
processor is concerned, the two following programme elements are
completely different:

  <programme>
    ...
  </programme>
  
  <programme xmlns="http://www.emilygraham.com/java/other/Opera";>
    ...
  </programme>

I sometimes use the extended representation {namespace-uri}local-name
to make this clearer. In the first example, the programme element is
{}programme. In the second example, the programme element is actually
{http://www.emilygraham.com/java/other/Opera}programme.

When you use an element or attribute name in an XPath and you don't
give a prefix to that element or attribute name, the XSLT processor
*always* looks for elements/attributes in no namespace. So:

  /programme/composer

is equivalent to:

  /{}programme/{}composer

To process your second example, you need to have the equivalent of:

  /{http://www.emilygraham.com/java/other/Opera}programme
    /{http://www.emilygraham.com/java/other/Opera}composer

To do this, you need to have a prefix in your XSLT stylesheet that
is associated with the namespace
'http://www.emilygraham.com/java/other/Opera'. You do this in the same
way as you do in normal documents, with an xmlns:xx attribute:

<xsl:stylesheet version="1.0"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
                xmlns:op="http://www.emilygraham.com/java/other/Opera";>
...
</xsl:stylesheet>

Within the stylesheet, the 'op' prefix is associated with the
'http://www.emilygraham.com/java/other/Opera' namespace. So you can
do:

  /op:programme/op:composer

to get the XPath that you need.

To put it into simple steps - if you add a default namespace to your
instance document you need to:

 - declare the namespace with a prefix in the stylesheet
 - change all the paths in the stylesheet so that they use the prefix

One of the requirements for XSLT 2.0 is that you should be able to
easily change the default namespace used when an element (or
attribute?) name in a path doesn't have a prefix, so that adding a
namespace to a document doesn't involve a massive change in the
stylesheet.

I hope that helps,

Jeni

---
Jeni Tennison
http://www.jenitennison.com/


 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Current Thread