list4xt : Mailing list for the XT users community.
[list4xt] Re: XSLT with a source with namespaces
Subject: [list4xt] Re: XSLT with a source with namespaces
From: Mike Brown (mike@skew.org)
Date: 30/06/2000 - 19:43
> Scratch that last question. I had a small mistype in the namespace path.
> It only works if the namespace URI in the source matches exactly with the
> namespace URI I put in my XSLT stylesheet. However, this seems to be a small
> problem in itself. If the party generating the source for me changes their
> namespace URI's, then my stuff will break because the URIs won't match
> anymore. Comments?
The only thing that uniquely identifies a namespace is the URI. It is the
prefixes that are meant to be interchangeable. Two elements are not
necessarily in the same namespace if they have the same prefix. I don't
always like it, either, but it does make sense; I'd hate for someone else
to come along and say that the prefix I've been using identifies their
namespace, not mine.
> Anyway, my transformation is working except I'm seeing a few anomolies in
> the output. Namely, in one of my templates I say:
>
> <?xml version="1.0"?>
>
> <xsl:stylesheet
> xmlns:xsl="<http://www.w3.org/1999/XSL/Transform>"
> version="1.0"
> xmlns:xt="<http://www.jclark.com/xt>"
> extension-element-prefixes="xt"
> xmlns:n1="x-schema:schema/schema-n1.xml"
> xmlns:n2="x-schema:schema/schema-n2.xml"
> xmlns:n3="x-schema:schema/schema-n3.xml"
> >
> ...
> <xsl:template match="n1:document">
> <n1_ourversion
> xmlns="x-schema:schema/n1_ourversion.xml"
> xmlns:n2_ourversion="x-schema:schema/n2_ourversion.xml"
> xmlns:n3_ourversion="x-schema:schema/n3_ourversion.xml"
> >
> ...
> </n1_ourversion>
> </xsl:template>
> ...
>
> but the actual output is this:
>
> ...
> <n1_ourversion
> xmlns:n1="x-schema:schema/n1.xml"
> xmlns:n2="x-schema:schema/n2.xml"
> xmlns:n3="x-schema:schema/n3.xml"
> xmlns="x-schema:schema/n1_ourversion.xml"
> xmlns:n2_ourversion="x-schema:schema/n2_ourversion.xml"
> xmlns:n3_ourversion="x-schema:schema/n3_ourversion.xml"
> >
> ...
> </n1_ourversion>
> ...
>
> The namespaces I added to the <xsl:stylesheet> element in my stylesheet are
> automatically getting inserted here and I don't know why. There are a
> couple other occurances of this type of odd thing with the namespace output.
> Why is this happening?
It is happening because namespace nodes are inherited. They are getting
picked up by the literal result element in your stylesheet. When literal
result elements are copied to the result tree, their namespace nodes go
with them.
I'll try to explain a bit further:
In the XPath/XSLT data model, xmlns[:foo] attributes in the input are
converted to namespace nodes. These nodes are copied/inherited on down the
tree. This applies to the stylesheet tree as well as the source tree and
result tree.
<foo xmlns="urn:ns0" xmlns:n1="urn:ns1">
<bar xmlns:n2="urn:ns2">
</foo>
This simple example has a tree structure like:
root
|___element 'foo'
| \__default namespace = 'urn:ns0'
| \__namespace prefix 'xml' = 'http://www.w3.org/XML/1998/namespace'
| \__namespace prefix 'n1' = 'urn:ns1'
|___text '\n '
|___element 'bar'
\__default namespace = 'urn:ns0' [inherited from ancestor]
\__namespace prefix 'xml' = 'http://www.w3.org/XML/1998/namespace'
\__namespace prefix 'n1' = 'urn:ns1' [inherited from ancestor]
\__namespace prefix 'n2' = 'urn:ns2'
Two things to note here: 1. the inheritance of namespace nodes from
ancestors, and 2. the default binding of prefix 'xml' to a namespace,
as per the Namespaces in XML W3C Recommendation.
So when you have literal result elements in your stylesheet tree, they're
going to be picking up your namespace declarations from their ancestors,
and the whole set (well, not quite the whole set.. see below) will be
copied over into the result tree. When the result tree is serialized, the
XSLT processor will create xmlns attributes where needed in order to
preserve the bindings, whether they're needed or not.
Namespace nodes for the XSLT namespace and extension namespaces are *not*
copied to the result tree for literal result elements, per the XSLT spec.
XSLT also lets you specify what namespaces you don't want the nodes for to
be copied over, via the exclude-result-prefixes attribute on the
stylesheet element. This functionality is described in section 7.1 of the
XSLT spec.
Watch what happens when you use xsl:element instead of literal result
elements. Poof! No more 'extraneous' namespace declarations.
I hope this helps.
On a side note, I feel it is a bug in XT that its HTML output method
outputs xmlns attributes at all. Namespaces are irrelevant in HTML 4.01
and lower.
- Mike
____________________________________________________________________
Mike J. Brown, software engineer at My XML/XSL resources:
webb.net in Denver, Colorado, USA http://www.skew.org/xml/
--
Mailing list for the XT users community. (http://highriskcc.com)
(mailto:list4xt-request@4xt.org?Subject=unsubscribe to unsubscribe)
Archive générée par hypermail 2b28 le 06/11/2001 - 11:46 CET
webmaster@4xt.org
|