tags:

views:

1505

answers:

7

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?

+3  A: 

There is some useful information here: http://www.w3schools.com/XML/xml_namespaces.asp

Vijay Mathew
+1  A: 

It's the xml namespace

Samuel Carrijo
A: 

It's called a namespace.

It is explained here:

http://en.wikipedia.org/wiki/XML_namespace

Time Machine
A: 

It stands for XML Namespace or Via Google

Wayne
+11  A: 

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/

Mads Hansen
+17  A: 

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"&gt;
    <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 null (or default) namespace.
  • If you declare a namespace without the the identifier, i.e. xmlns="http://somenamespace, rather than xmlns:rob="somenamespace", it specifies the default namespace for the document
  • The actual namespace itself, often a url, is of no real consequence. It should be unique, so people tend to chose a url that they own, but it has no greater meaning than that. Sometimes people will place the schema (definition) for the xml at the url specified, 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. Severakl tags beginning with different prefixes, all of which map to the same namespace are considered to be the same.
  • Attributes can be namespaced 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"&gt;
    <name>Rob</name>
    <age>37</age>
    <homecity xmlns="http://www.my.example.com/xml/cities"&gt;
        <name>London</name>
        <lat>123.000</lat>
        <long>0.00</long>
    </homecity>
</person>
Rob Levine
+1 for writing an essay.
Noldorin
That kind of answer is what's making this site so good! Having the answer instead of pointing to it is what makes this site better than Google!
Brad Bruce
It's pretty redundant in many cases. We don't need to rewrite every single piece of information on the internet. I think some amount of emphasis should be put on googling around just a little bit before some questions are asked. "What rendering engines do today's browsers use?" Is a great example of this, if you type in the browsers name + rendering engine, you get an answer, first hit, for each browser. In quite a few cases it's obvious they're too lazy to read 2-3 paragraphs to get the information they need. They need pre-chewed food like babies.
Sneakyness
A: 

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.

Jon