Reputation: 2103
While searching Web I encountered many examples of using namespaces in XML files. They mostly have this form:
<d:student xmlns:d='http://www.develop.com/student'>
<d:id>3235329</d:id>
<d:name>Jeff Smith</d:name>
<d:language>C#</d:language>
<d:rating>9.5</d:rating>
</d:student>
(this is example taken from https://msdn.microsoft.com/en-us/magazine/cc302166.aspx)
This line:
<d:student xmlns:d='http://www.develop.com/student'>
troubles me, pretty much because every example looks like this. Can it have a form of
<student xmlns:d='http://www.develop.com/student'>
So here I am declaring the same namespace identified by the same URI, but i don't want node where declaration is to have namespace. Is it correct? Long story short: is just xmlns:d='http://www.develop.com/student'
a valid declaration for d:
namespace?
Upvotes: 0
Views: 46
Reputation: 19512
d
ist not the namespace but an alias for it (that is usable as a prefix). The namespace is still http://www.develop.com/student
.
<d:student xmlns:d='http://www.develop.com/student'/>
can be read as {http://www.develop.com/student}:student
.
It is possible to define a default namespace for elements without a prefix using the xmlns attribute. So <student xmlns='http://www.develop.com/student'/>
can be read as {http://www.develop.com/student}:student
, too.
The DOM keeps the namespace and the localname in separate properties of the node objects actually. The {namespace}:localname
syntax is often used for debug outputs.
A namspace deifnition is always valid for the element node it is defined on and all its descendants (unless it is overwritten in a descendant).
Attribute nodes do not use the default namespace definition. Attributes without a prefix are always in the "empty" namespace.
Here is an example that doesn't use prefixes.
<student xmlns="http://www.develop.com/student">
<id>3235329</id>
<name>Jeff Smith</name>
<description type="xhtml">
<div xmlns="http://www.w3.org/1999/xhtml/">
...
</div>
</description>
</student>
The node names in the example (with resolved namespaces) are:
{http://www.develop.com/student}:student
{http://www.develop.com/student}:id
{http://www.develop.com/student}:name
{}:type
{http://www.w3.org/1999/xhtml/}:div
Upvotes: 0
Reputation: 944192
xmlns:d='http://www.develop.com/student'
will declare the d
namespace for that elements and all its descendants.
It will not make the element come from that namespace if it lacks d:
on the tag name. That will still use the default namespace.
i.e.
<foo xmlns="http://example.com/1">
<bar xmlns:x="http://example.com/2">
<x:baz />
</bar>
</foo>
foo
comes from /1
. bar
comes from /1
. baz
comes from /2
.
Upvotes: 1