I saw the following line in an XML file:
xmlns:android="http://schemas.android.com/apk/res/android"
I have also seen xmlns
in many other XML files that I've come across.
What is it?
An XML namespace is a collection of names that can be used as element or attribute names in an XML document. The namespace qualifies element names uniquely on the Web in order to avoid conflicts between elements with the same name.
the xmlns attribute specifies the xml namespace for a document. This basically helps to avoid namespace conflicts between different xml documents, if for instance a developer mixes xml documents from different xml applications.
No, it shouldn't matter. Though there might be implementations out there that do not use dedicated XML libraries and instead parse the content strict. Such consumers might break.
How xmlns attribute is different from targetNamespace attribute? The target namespace is used for the internal referencing in the xml document to make sure it is valid as declared from the schema. The xmlns attribute actually declares the namespace of a particular element or group of elements.
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.
<person> <name>Rob</name> <age>37</age> <homecity> <name>London</name> <lat>123.000</lat> <long>0.00</long> </homecity> </person>
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:
<personxml:person xmlns:personxml="http://www.your.example.com/xml/person" xmlns:cityxml="http://www.my.example.com/xml/cities"> <personxml:name>Rob</personxml:name> <personxml:age>37</personxml:age> <cityxml:homecity> <cityxml:name>London</cityxml:name> <cityxml:lat>123.000</cityxml:lat> <cityxml:long>0.00</cityxml:long> </cityxml:homecity> </personxml:person>
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.
<personxml:person xmlns:personxml="http://example.com/same/url" xmlns:mycityxml="http://example.com/same/url" />
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
<person xmlns="http://www.your.example.com/xml/person"> <name>Rob</name> <age>37</age> <homecity xmlns="http://www.my.example.com/xml/cities"> <name>London</name> <lat>123.000</lat> <long>0.00</long> </homecity> </person>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With