Reputation:
Why don't I have to specify the namespace for attributes of nodes in XSLT?
As an example, let's say I have an XML that looks like this:
<?xml version="1.0"?>
<timeline xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="urn:xmlsn:axpz_namespace">
<A>
<B>
<C key="in"> bval <\C>
<C ind="ra"> bra <\C>
</B>
<\A>
And my xsl file Header like this:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:mms="urn:xmlsn:axpz_namespace">
A select statement for an attribute of C would look something like:
select="./mms:B/mms:C[@key='in']"/>
select="./mms:B/mms:C[@ind='ra']"/>
But it seems like if you have to specify that B
and C
are part of the mms namespace, then you should have to specify that key
and ind
are part of that namespace as well. I.e.
select="./mms:B/mms:C[@mms:key='in']"/>
select="./mms:B/mms:C[@mms:ind='ra']"/>
While it seems obvious that if you're specifying the nodes namespace to be in mms
then it's attribute should be in there as well, it seems just as obvious that if you specify B
to be within the mms
namespace then C
should be in there as well.
This may be a trivial question, and maybe one that simply depends on the XSL reader being used, but I'm asking because it made me curious as to whether or not you could mix namespaces along various nodes. For example, something like:
select="./mms:B/othernamespace:C[@mms:key='in']"/>
I'm using the Saxon8B reader, and I'm guessing that this syntax is potentially dependent on which reader I use.
Upvotes: 5
Views: 176
Reputation: 243469
Unprefixed attributes are always in "no namespace".
And Jon Skeet's answer is like this:
"Because the W3C namespace Spec says so"
Let me explain the reasoning behind such a design decision:
The logic is that an attribute is fully defined by the element on which it appears, therefore if the elements belongs to a specific namespace, there is no need to specify that any of their attributes belong to that (or other) namespace.
To put it in other words, attributes don't need a namespace to disambiguate them as is the case for elements -- because an attribute is fully disambiguated by the element it belongs to.
Therefore, it is a bad idea when designing a new XML-based vocabulary, to define attributes as belonging to a namespace.
An exception of this common-sense rule is for attributes that are with global scope (can appear on any element). Examples of such attributes are: xml:lang
, xml:space
, ..., etc.
Upvotes: 1
Reputation: 1500055
Although xmlns=...
sets the default namespace for elements, it doesn't set one for attributes - you only get a namespace in an attribute name if you specify it explicitly.
It shouldn't depend on which XML API you use at all.
From "Namespaces in XML 1.0":
6.2 Namespace Defaulting
The scope of a default namespace declaration extends from the beginning of the start-tag in which it appears to the end of the corresponding end-tag, excluding the scope of any inner default namespace declarations. In the case of an empty tag, the scope is the tag itself.
A default namespace declaration applies to all unprefixed element names within its scope. Default namespace declarations do not apply directly to attribute names; the interpretation of unprefixed attributes is determined by the element on which they appear.
(Emphasis mine.)
Note that this isn't about XSLT or XPath really - it's about plain XML and XML namespaces.
Upvotes: 4