ChatGPT解决这个技术问题 Extra ChatGPT

What does "xmlns" in XML mean?

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?

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. :)

R
Rob Levine

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.

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>

+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.
@Rob Levine "Attributes can be namespaced but are generally not." What about Android ?
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?
@WORMSS - 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.
This microsoft document link :"msdn.microsoft.com/en-us/library/aa468565.aspx" explains the namespace in XML very very well.
M
Mads Hansen

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/


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?
@Patrick 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.
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.
@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.
then how tags works? I mean <LinearLayout works without prefix?
M
Morfidon

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.


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? Cheers
@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.
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. Cheers
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.
@brennanyoung it's exactly as you have said :)
Y
Yuriy Vasylenko

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.


P
Peter Mortensen

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.


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.