Pavel Patrin
Pavel Patrin

Reputation: 1736

Elements without namespace in XML

This is a well-formed XML without default namespace.

<?xml version="1.0" encoding="UTF-8"?>
<Zoo xmlns:Staff="URN:STAFF" xmlns:Animal="URN:ANIMAL">
    <Staff:Security name="John" surname="Connor"/>
    <Animal:Dog name="Hachico"/>
    <Visitor name="Arnold" surname="Schwarzenegger"/>
</Zoo>
  1. In which namespace there are attribute name of tag Animal:Dog?
  2. In which namespace there are element Visitor?

Upvotes: 5

Views: 4900

Answers (2)

Hugo R
Hugo R

Reputation: 3113

answer to question 1: Animal:Dog in in namespace "URN:ANIMAL" which is prefixed with the label "Animal"

answer to question 2: Element "Visitor" does not belong to any namespace.

when an element is not prefixed, it can be either a) belong to a default namespace, or no namespace at all. to see if there is a default nameSpace look for xmlns="namespace" at the top of the file , notice the missing colon.

Upvotes: 0

IMSoP
IMSoP

Reputation: 97968

They are not in any namespace at all.

The relevant section of the XML Namespaces spec includes this statement:

If there is no default namespace declaration in scope, the namespace name has no value.

In the terminology of that spec, an "expanded name" consists of a pair of values, the "namespace name" and the "local name". So you could represent the <Animal:Dog /> element as something like {'URN:ANIMAL', 'Dog'}, and the <Visitor /> element as {null, 'Visitor'}.

Unprefixed attributes are a little more curious, as discussed in this related question, because they don't take on the default namespace even if one is in scope:

The namespace name for an unprefixed attribute name always has no value.

Many people will loosely interpret the attribute as being in the same namespace as the element it is on, but this is not technically the case. <Animal:Dog name="Hachico" /> is not the same as <Animal:Dog Animal:name="Hachico" />.

Upvotes: 7

Related Questions