What does "xmlns" in XML mean?
Asked Answered
E

6

500

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?

Ericerica answered 25/7, 2009 at 11:18 Comment(3)
have a look : programmers.stackexchange.com/questions/122002/…Expend
Now you would have also got to know that why we get errors in the xml when we use the tags incorrectly or at the wrong place. :)Apotheosize
In short xmlns means one of two things: (1) xmlns="X" means X is a default namespace; (2) xmlns:pf="X" means that pf is a namespace prefix that can be used as a sort of abbreviation for X. See my concise and complete answer below for the rest of the storyOast
D
325

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 />

Think of the namespace prefix as a variable with a short name alias for the full namespace URI. It is the equivalent of writing <http://schemas.android.com/apk/res/android:foo /> with regards to what it "means" when an XML parser reads the document.

NOTE: You cannot actually use the full namespace URI in place of the namespace prefix in an XML instance document.

Check out this tutorial on namespaces: http://www.sitepoint.com/xml-namespaces-explained/

Disremember answered 25/7, 2009 at 11:25 Comment(7)
I always paste those URIs into a web browser just to see what the parser is looking at, but it always returns 404. Is it supposed to be a real URI that needs a standard filename tacked on to the end, or is it just a technique for making a unique id?Ahrens
@Ahrens yes, its just a URI that needs to be unique so as to indicate that it is a separate namespace from others and any potential duplicate tags will therefore be interpreted correctly. So the URI will often point to nothing.Monikamoniker
Hmm...good to know it's a namespace. I used to wonder whether specifying a URI made the html page actually access that website to determine a schema.Feed
@Patrick, URI is not the same as URL. A URL is a locator and a URI is just an identifier. You could very well choose a GUID as a URI. In fact, the ISBN that we have for books is a form of URI.Butcher
then how tags works? I mean <LinearLayout works without prefix?Titmouse
The ones that I've seen are prefixed with just "http://". From what you've said, it sounds like that's completely irrelevant, and has no bearing on whether the page uses HTTPS or not. Is that right?Nord
@Nord that is correct. Namespace URI is just a unique identifier. URLs were often used since only one person can own a domain and you can choose to host info at that URL if you want for documentation, schemas, etcDisremember
E
814

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>
Egger answered 25/7, 2009 at 11:41 Comment(13)
+1 good, helpful conceptual answer. You may want to qualify "Basically, every element (or attribute) in xml belongs to a namespace", since some elements and attributes are said to be in "no namespace". Though I understand you were giving the basics.Tandi
@Rob Levine "Attributes can be namespaced but are generally not." What about Android ?Hirza
So how is for example "your.example.com/xml/person" used? Ok I have a <person:name> tag, now what? Can you explain this as well please?Pang
@KorayTugay: In the example above, the namespace lets me disambiguate between the two Name elements. For instance, if I was doing an xpath search for people's names I might have something like //name as my xpath query, but this would return both people names and city names. But if I query //personxml:name, then I just get back the names of people. That's all there is to it really.Egger
Ah thanks, great answer and great explanation. Thanks. So simple.. Maybe it is also used with the file operating on the xml file, am I correct? Like I do not know as in <h:inputText> for jsf or <c:for each> as in JSTL?Pang
yes - so in the examples you give, inputText belongs to whatever namespace the prefix "h:" is defined as at the top of the xml document, and the same for "c:". Remember that it isn't the "h" or the "c" itself that carries any meaning, it is only what the "h" and "c" are defined as.Egger
@RobLevine how would one search for person "name" when you have used the hierarchy approach in the last example? Since there is no prefix to search with? I presume one would create a NS that has the same uri? and not be tied to the prefix that the document has (or in this case, does not have?)Godin
@Godin - Yes - you are right. When you define your query, you need to specify the namespace, probably via a prefix. In the C# System.Xml world, for example, you'd register a prefix with the namespace manager using XmlNamespaceManager.AddNamespace, and then use this prefix in your query. The fact the prefix doesn't appear in the document doesn't matter - only what namespace it maps too.Egger
Here's something that got me last night. In your <homecity xmlns=> example, the value of xmlns= has to be a uri, not an alias of it. e.g., xmlns='cityxml' doesn't work.Finella
@Terris: yes that's right. When you are declaring the namespace, you can't use an alias (if it had been previously declared), you have to use the uri (or whatever - the namespace doesn't have to a be a uri but by convention it usually is)Egger
This microsoft document link :"msdn.microsoft.com/en-us/library/aa468565.aspx" explains the namespace in XML very very well.Neoteny
@RobLevine Could you please specify or diagram where the prefixes that you mention are? How would you have several tags beginning with different prefixes, but map each of them to the same namespace?Rideout
@Rideout - if the example had the tags: <p:person xmlns:p="http://www.your.example.com/xml/person" xmlns:q="http://www.your.example.com/xml/person" /> then the prefixes p and q both map to the same namespace. It wouldn't matter if you prefixed a given element with p or q, 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.Egger
D
325

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 />

Think of the namespace prefix as a variable with a short name alias for the full namespace URI. It is the equivalent of writing <http://schemas.android.com/apk/res/android:foo /> with regards to what it "means" when an XML parser reads the document.

NOTE: You cannot actually use the full namespace URI in place of the namespace prefix in an XML instance document.

Check out this tutorial on namespaces: http://www.sitepoint.com/xml-namespaces-explained/

Disremember answered 25/7, 2009 at 11:25 Comment(7)
I always paste those URIs into a web browser just to see what the parser is looking at, but it always returns 404. Is it supposed to be a real URI that needs a standard filename tacked on to the end, or is it just a technique for making a unique id?Ahrens
@Ahrens yes, its just a URI that needs to be unique so as to indicate that it is a separate namespace from others and any potential duplicate tags will therefore be interpreted correctly. So the URI will often point to nothing.Monikamoniker
Hmm...good to know it's a namespace. I used to wonder whether specifying a URI made the html page actually access that website to determine a schema.Feed
@Patrick, URI is not the same as URL. A URL is a locator and a URI is just an identifier. You could very well choose a GUID as a URI. In fact, the ISBN that we have for books is a form of URI.Butcher
then how tags works? I mean <LinearLayout works without prefix?Titmouse
The ones that I've seen are prefixed with just "http://". From what you've said, it sounds like that's completely irrelevant, and has no bearing on whether the page uses HTTPS or not. Is that right?Nord
@Nord that is correct. Namespace URI is just a unique identifier. URLs were often used since only one person can own a domain and you can choose to host info at that URL if you want for documentation, schemas, etcDisremember
P
24

I think the biggest confusion is that xml namespace is pointing to some kind of URL that doesn't have any information. But the truth is that the person who invented below namespace:

xmlns:android="http://schemas.android.com/apk/res/android"

could also call it like that:

xmlns:android="asjkl;fhgaslifujhaslkfjhliuqwhrqwjlrknqwljk.rho;il"

This is just a unique identifier. However it is established that you should put there URL that is unique and can potentially point to the specification of used tags/attributes in that namespace. It's not required tho.

Why it should be unique? Because namespaces purpose is to have them unique so the attribute for example called background from your namespace can be distinguished from the background from another namespace.

Because of that uniqueness you do not need to worry that if you create your custom attribute you gonna have name collision.

Pera answered 17/2, 2016 at 14:23 Comment(5)
Hi Morfidon. Please, could you add in your answer a link to a reference specifying that content or the URL can be anything? What must be unique? Please also clarify the uniqueness: The name of the XML namespace or the content of the URL? CheersAdvisory
@olibre If you want to use android's namespace you should use their name. That's their unique name. They decided to put there an URL. If you are creating your own namespace then you can call it whatever you want just make sure it's unique. It's said that you should use URL that points to the place where you describe what is inside that namespace but as you can see even creators of android namespace didn't do it.Pera
Thanks Morfidon. I have finally understood that point while reading the Rob's answer. I like your answer because it highlights an important aspect that is not well known. But please try to improve your answer, clarify what part must be unique, provide references, rephrase, check the wording... Some readers may be confused by your current answer. CheersAdvisory
This is the answer I needed! So confusing to use a URI for this attribute value, because people think URI=URL, and expect that some document should reside at the location. But AFAICT it's completely ok if the URI returns a 404. It's just a unique identifier that happens to look like an address. There might be some special validators that expect to find a schema or a DTD at that location (which would make sense), but these are special cases I think. Please correct me if I am wrong here.Slotter
@Slotter it's exactly as you have said :)Pera
T
14

xmlns - xml namespace. It's just a method to avoid element name conflicts. For example:

<config xmlns:rnc="URI1" xmlns:bsc="URI2">
  <rnc:node>
      <rnc:rncId>5</rnc:rncId>
  </rnc:node>

  <bsc:node>
      <bsc:cId>5</bsc:cId>
  </bsc:node>
</config>

Two different node elements in one xml file. Without namespaces this file would not be valid.

Towelling answered 8/6, 2015 at 20:13 Comment(0)
P
4

You have name spaces so you can have globally unique elements. However, 99% of the time this doesn't really matter, but when you put 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.

Pandiculation answered 26/7, 2009 at 9:40 Comment(1)
for trivial xml it might me ignored, for everything else namespaces are extremely important. + I really dont see the connection between semantic web and namespace. The semantic web is a concept, namespace is part of the XML standard, you are mixing interface definition and implementation details.Saxtuba
O
0

New, concise and complete answer to an old, commonly asked question...

xmnls supports XML Namespaces, providing a standard way of naming XML elements and attributes so that separately developed vocabularies can be combined without naming conflicts.

Purpose of XML namespaces

XML namespaces enable XML schema authors to differentiate otherwise identical element and attribute names in independently developed schemas. Basing namespace names on URIs leverages uniqueness and ownership aspects of domain names as a means of preventing naming collisions (uniqueness) and establishing naming authority (ownership).

Purpose of xmlns

xmlns is used in two distinct ways in XML namespaces:

  • Default namespace declaration

    <element xmlns="http://www.example.org">
    

    declares that element and all of its descendent elements are in the http://www.example.org namespace by default.

  • Namespace prefix declaration

    <ex:element xmlns:ex="http://www.example.org">
    

    declares that ex:element and

    • its attributes
    • its descendent elements
    • its descendent elements' attributes

    that are prefixed with ex: are in the http://www.example.org namespace.

Bottom line

Using xmlns via a default namespace declaration or namespace prefix declarations, enables you to safely mix your XML with XML from the http://www.example.org namespace without worries of naming collisions.

See also

Oast answered 14/8, 2023 at 14:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.