XML 中的“xmlns”是什么意思?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1181888/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What does "xmlns" in XML mean?
提问by user88637
I saw the following line in an XML file:
我在 XML 文件中看到以下行:
xmlns:android="http://schemas.android.com/apk/res/android"
I have also seen xmlnsin many other XML files that I've come across.
我还在xmlns我遇到的许多其他 XML 文件中看到过。
What is it?
它是什么?
采纳答案by Mads Hansen
It defines an XML Namespace.
它定义了一个XML 命名空间。
In your example, the Namespace Prefixis "android" and the Namespace URIis "http://schemas.android.com/apk/res/android"
在您的示例中,命名空间前缀是“ android”,命名空间 URI是“ http://schemas.android.com/apk/res/android”
In the document, you see elements like: <android:foo />
在文档中,您会看到以下元素: <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.
将命名空间前缀视为具有完整命名空间 URI 的短名称别名的变量。<http://schemas.android.com/apk/res/android:foo />当 XML 解析器读取文档时,它等同于写入“含义”。
NOTE:You cannot actually use the full namespace URI in place of the namespace prefix in an XML instance document.
注意:您实际上不能使用完整的命名空间 URI 代替 XML 实例文档中的命名空间前缀。
Check out this tutorial on namespaces: http://www.sitepoint.com/xml-namespaces-explained/
查看有关命名空间的本教程:http: //www.sitepoint.com/xml-namespaces-explained/
回答by Rob Levine
It means XML namespace.
它的意思是XML 命名空间。
Basically, every element (or attribute) in XMLbelongs to a namespace, a way of "qualifying" the name of the element.
基本上,XML 中的每个元素(或属性)都属于一个命名空间,这是一种“限定”元素名称的方式。
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.
想象一下,你和我都发明了我们自己的 XML。你发明 XML 来描述人,我发明我的来描述城市。我们都包含一个名为 的元素name。你的指的是人名,我的指的是城市名——好吧,有点做作。
<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 nameelements, but they both have different meanings.
如果我们的两个 XML 合并为一个文档,我们如何区分这两个名称?正如你在上面看到的,有两个name元素,但它们都有不同的含义。
The answer is that you and I would both assign a namespace to our XML, which we would make unique:
答案是你和我都会为我们的 XML 分配一个命名空间,我们将使其独一无二:
<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 nameelement means. All of the tags that start with personxml:are tags belonging to your XML, all the ones that start with cityxml:are mine.
现在我们已经完全限定了我们的 XML,对于每个name元素的含义没有歧义。所有以 开头的标签personxml:都是属于你的 XML 的标签,所有以 开头的cityxml:都是我的。
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 thanxmlns: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
personxmlandmycityxmlboth mapped to the same namespace (as in the snippet below), then it wouldn't matter if you prefixed a given element withpersonxmlormycityxml, 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 indirectionpointing 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 notinherit their namespace from the element they are on, as opposed to elements (see below).
如果您排除任何命名空间声明,则事物被视为在默认命名空间中。
如果您声明一个没有标识符的命名空间,即
xmlns="http://somenamespace",而不是xmlns:rob="somenamespace",则它指定文档的默认命名空间。实际的命名空间本身,通常是一个IRI,没有实际意义。它应该是唯一的,所以人们倾向于选择他们拥有的 IRI/URI,但它没有比这更重要的意义。有时人们会将 XML 的模式(定义)放置在指定的 IRI 中,但这只是某些人的约定。
前缀也无关紧要。唯一重要的是前缀定义为什么命名空间。几个以不同前缀开头的标签,所有这些都映射到相同的命名空间被认为是相同的。
例如,如果前缀
personxml和mycityxml都映射到相同的命名空间(如下面的代码片段所示),那么如果给给定元素添加前缀personxml或mycityxml,它们都将被 XML 解析器视为同一事物. 关键是 XML 解析器不关心您选择什么作为前缀,只关心它映射的名称空间。前缀只是指向命名空间的间接引用。<personxml:person xmlns:personxml="http://example.com/same/url" xmlns:mycityxml="http://example.com/same/url" />属性可以被限定,但通常不是。它们也不从它们所在的元素继承它们的命名空间,这与元素相反(见下文)。
Also, element namespaces are inherited from the parent element. In other words I could equally have written the above XML as
此外,元素命名空间继承自父元素。换句话说,我同样可以将上述 XML 编写为
<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>
回答by 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:
我认为最大的困惑是 xml 命名空间指向某种没有任何信息的 URL。但事实是,发明下面命名空间的人:
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.
这只是一个唯一标识符。但是,您应该将唯一的 URL 放在那里,并且可能指向该命名空间中使用的标签/属性的规范。这不是必需的。
Why it should be unique? Because namespaces purpose is to have them unique so the attribute for example called backgroundfrom your namespace can be distinguished from the backgroundfrom another namespace.
为什么它应该是独一无二的?因为命名空间的目的是让它们独一无二,所以例如来自命名空间的背景属性可以与来自另一个命名空间的背景区分开来。
Because of that uniqueness you do not need to worry that if you create your custom attribute you gonna have name collision.
由于这种独特性,您不必担心如果您创建自定义属性,您会遇到名称冲突。
回答by Yuriy
xmlns - xml namespace. It's just a method to avoid element name conflicts. For example:
xmlns - xml 命名空间。这只是一种避免元素名称冲突的方法。例如:
<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 nodeelements in one xml file. Without namespaces this file would not be valid.
node一个 xml 文件中的两个不同元素。如果没有命名空间,此文件将无效。
回答by Jon
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.
您拥有名称空间,因此您可以拥有全球唯一的元素。然而,在 99% 的情况下,这并不重要,但是当你把它放在语义网的角度来看时,它开始变得重要。
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.
例如,您可以通过使用适当的xmlns. 例如,将朋友的朋友与vCard混搭等。

