I saw the following line in an XML file:
xmlns:android=\"http://schemas.android.com/apk/res/android\"
I have also seen xmlns
i
It means XML namespace.
Basically, every element (or attribute) in XML belongs to a namespace, a way of "qualifying" the name of the element.
Imagine you and I both invent our own XML. You invent XML to describe people, I invent mine to describe cities. Both of us include an element called name
. Yours refers to the person’s name, and mine to the city name—OK, it’s a little bit contrived.
Rob
37
London
123.000
0.00
If our two XMLs were combined into a single document, how would we tell the two names apart? As you can see above, there are two name
elements, but they both have different meanings.
The answer is that you and I would both assign a namespace to our XML, which we would make unique:
Rob
37
London
123.000
0.00
Now we’ve fully qualified our XML, there is no ambiguity as to what each name
element means. All of the tags that start with personxml:
are tags belonging to your XML, all the ones that start with cityxml:
are mine.
There are a few points to note:
If you exclude any namespace declarations, things are considered to be in the default namespace.
If you declare a namespace without the identifier, that is, xmlns="http://somenamespace"
, rather than xmlns:rob="somenamespace"
, it specifies the default namespace for the document.
The actual namespace itself, often a IRI, is of no real consequence. It should be unique, so people tend to choose a IRI/URI that they own, but it has no greater meaning than that. Sometimes people will place the schema (definition) for the XML at the specified IRI, but that is a convention of some people only.
The prefix is of no consequence either. The only thing that matters is what namespace the prefix is defined as. Several tags beginning with different prefixes, all of which map to the same namespace are considered to be the same.
For instance, if the prefixes personxml
and mycityxml
both mapped to the same namespace (as in the snippet below), then it wouldn't matter if you prefixed a given element with personxml
or mycityxml
, they'd both be treated as the same thing by an XML parser. The point is that an XML parser doesn't care what you've chosen as the prefix, only the namespace it maps too. The prefix is just an indirection pointing to the namespace.
Attributes can be qualified but are generally not. They also do not inherit their namespace from the element they are on, as opposed to elements (see below).
Also, element namespaces are inherited from the parent element. In other words I could equally have written the above XML as
Rob
37
London
123.000
0.00