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.
Can someone please explain what it is?
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.
Can someone please explain what it is?
There is some useful information here: http://www.w3schools.com/XML/xml_namespaces.asp
It defines an XML Namespace. In your example, the Namespace Prefix is "android" and the Namespace URI is "http://schemas.android.com/apk/res/android"
In the document, you see elements like: <android:foo />
The namespace prefix is really just like a variable with a short name. It is equivalent to writing <http://schemas.android.com/apk/res/android:foo />
Check out this tutorial on namespaces: http://www.sitepoint.com/article/xml-namespaces-explained/
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 - its 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:
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>
You have name spaces so you can have globally unique elements, however 99% of the time this doesn't really matter but when you but it in the perspective of the semantic web it starts to become important. For example you could make an xml mash-up of different schemes just by using the appropriate xmlns for example mash up friend of a friend with Vcard etc.