Re: [xsl] Namespace attribute problem with Saxon

Subject: Re: [xsl] Namespace attribute problem with Saxon
From: "Mark Wilson" <drmark@xxxxxxxxxxxxxxx>
Date: Tue, 17 Jan 2006 08:44:11 -0500
Eric,
Thank you for your very kind, and most complete, explaination. because of it, I now believe I have a much clearer grasp of the default namespace.
Mark


----- Original Message ----- From: "Eric BrC)chemier" <xsl-list@xxxxxxxxxxxxxxxxxxxx>
To: "Mark Wilson" <drmark@xxxxxxxxxxxxxxx>
Sent: Tuesday, January 17, 2006 8:24 AM
Subject: Re: [xsl] Namespace attribute problem with Saxon



On 1/16/06, Mark Wilson <drmark@xxxxxxxxxxxxxxx> wrote:
When I export my xml source document from MicrosoftWord, the schema I used
to create the document is referenced in the outermost node (...)
If I try to apply an XSLT document to this exported document, Saxon
produces nothing, but if I remove the xmlns attribute referencing the schema
from the List element, Saxon produces the expected output from the source
document.

Hi Mark,


Your problem is not specific to Saxon, and it is the normal behavior to expect.

The "xmlns attribute" is not actually an attribute. It is called a
pseudo-attribute and it has a huge effect on your document, which
still looks like the same for you, but is now totally different for
any (schema-aware) xml processor.

In technical terms, it declares the default namespace associated with
all elements without a prefix (the part before ':' in xml element
names, like pre in 'pre:name'). It is like applying a different color
to the corresponding elements in your document. You still see the
document in black and white (for now), but Saxon sees it in color, and
does not match the black elements if you write something like
<xsl:template match="List">. On the other hand, if you rewrite your
document (binding the lib prefix with your namespace)
<lib:List xmlns:lib="http://knihtisk.org/LibraryList.xsd";>
....
</lib:List>
it looks like different for you, but it is exactly the same as the
document with the default namespace declaration for an xml processor.

This is the key to understanding the solution, which is declaring a
prefix binding in the stylesheet, to indicate the namespace or "color"
of your elements:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
xmlns:lib="http://knihtisk.org/LibraryList.xsd";>
(...)
<xsl:template match="lib:List">


This difficulty to understand how to match elements in the default
namespace (no prefix in input document, but you have to use one in the
stylesheet) has been adressed by the XSLT 2.0 specification. In an
XSLT 2.0 stylesheet, you can now write:
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
xpath-default-namespace="http://knihtisk.org/LibraryList.xsd";>
(...)
<xsl:template match="List">
which still requires adding a namespace declaration, but removes the
need to add a prefix in every xpath expression to match your
document's elements.


You can get more details on these matters in:

W3C XSL Transformations (XSLT) Version 2.0
Candidate Recommendation 3 November 2005
B' 5.2 Unprefixed QNames in Expressions and Patterns
[http://www.w3.org/TR/xslt20/#unprefixed-qnames]

W3C Namespaces in XML
[http://www.w3.org/TR/REC-xml-names/]

Kind regards,

Eric

Current Thread