XML与名称空间有什么关系? [英] What has an namespace to do with XML?

查看:47
本文介绍了XML与名称空间有什么关系?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有些东西让我感到困惑:

Something confuses me here:

NSXMLParser方法具有namespaceURI属性:

The NSXMLParser method has a namespaceURI attribute:

- (void)parser:(NSXMLParser *)parser 
 didEndElement:(NSString *)elementName 
  namespaceURI:(NSString *)namespaceURI 
 qualifiedName:(NSString *)qName

从文档中,我无法弄清楚命名空间"的含义.有人可以举例说明XML中的名称空间是什么,为什么我要这么做吗?

From the documentation I couldn't figure out what they mean by "namespace". Can someone explain with an example what a namespace is in a XML and why I would want that?

是的,我注意到了维基百科.但这又令人困惑.在

Yep, I noticed wikipedia. But it's confusing, again. What sense does it make to put one single namespace declaration at the top of the XML file like

xmlns:xhtml="http://www.w3.org/1999/xhtml"

??同样,这只是零意义.维基百科也没有有用的例子,为什么我真的想要名称空间,更重要的是,它在XML文件中的外观.他们说这是为了解决多个同名元素(如ID)的歧义,但没有示例说明多个名称空间将如何解决这个问题.

?? Again, that makes just zero sense. Wikipedia has no useful example either to get it, why I would really want namespaces and -more importantly- how this looks in an XML file. They say it's for resolving ambiguity of multiple same-named elements like ID, but there's no example how multiple namespaces would resolve that.

推荐答案

XML名称空间在其他任何地方都像名称空间一样工作.

XML namespaces work like namespaces everywhere else.

它们提供了一种唯一区分同名元素或属性的方法.这是通过声明名称空间URI并可选地在节点名称上附加前缀来完成的.(可选)此前缀与名称空间声明一起定义.

They provide a means to uniquely distinguish equally named elements or attributes. This is done by declaring a namespace URI, and optionally attaching a prefix to the node name. This prefix is (optionally) defined along with the namespace declaration.

<!-- node without any namespace (it's in the default namespace) -->
<node>
  <child /><!-- descendants are in the parent namespace by default -->
</node>

<!-- node with explicit default namespace -->
<node xmlns="http://some/namespace/uri/">
  <child /><!-- descendants are in the parent namespace by default -->
</node>

<!-- NS declaration with prefix (node is still in the default namespace!) -->
<node xmlns:prefix="http://some/namespace/uri/">
  <child /><!-- descendants are in the parent's namespace -->
  <prefix:child><!-- explicit namespace assignment by prefix -->
    <grandchild /><!-- prefixes don't propagate, this is in the default namespace! -->
  </prefix:child>
</node>

命名空间是严格限定范围的.它们仅对节点及其后代可用.要在整个XML文档中拥有可用的名称空间,必须在顶级元素(文档元素)中声明该名称空间.

Namespaces are strictly scoped. They are available to the node and its descendants only. To have a namespace available in the entire XML document, it must be declared at the top level element (document element).

在您的情况下,在示例或< prefix:child/> :

In your case, on the example or <prefix:child />:

didEndElement = "child"
 namespaceURI = "http://some/namespace/uri/"
qualifiedName = "prefix:child"

这篇关于XML与名称空间有什么关系?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆