In DTDs, why are namespaces given as a URL?

半世苍凉 提交于 2020-01-20 08:30:50

问题


Apparently, the namespace URL that follows xmlns in HMTL and XML pages are meaningless. And all this time I though there actually were namespaces at those addresses...

When I first read/heard about namespaces, I imagined there some large files at the URLs provided that contained a list of all the valid 'names' that could be used in instances of the document. I've come to learn I imagined wrong.

But if the URL is totally useless, what exactly is the point of a namespace? How do you know if something belongs to a namespace if it doesn't actually exist anywhere? When I'm specifying a "namespace" am I actually doing anything??

It sounds to me like it's totally arbitrary in every sense of the word.


回答1:


The essential function of a namespace name in XML is to provide a unique identifier which allows software or people who need to process XML documents to know whether a particular element encountered in the input is, or is not, one of the elements they are responsible for processing. Suppose you are a specialized piece of software designed to process elements named 'blort' and display the results in an area of the user's display. Whenever you see an element named 'blort' in the input, you must decide whether the 'blort' element you are seeing in the input is the 'blort' element you are supposed to work on, or another element with the same name but a different structure or meaning. Without namespaces, you cannot know for certain.

This problem can be solved if:

  • Everyone who invents an XML vocabulary (and cares about avoiding name conflicts) chooses a unique name for their vocabulary. For a variety of historical, technical, and stylistic reasons, we call this name a 'namespace name'.

  • We find some way to make the full name of an XML element or attribute include the namespace name. This is what some specs call the 'expanded name': a pair consisting of the namespace name and the 'local name'.

You can now distinguish between the 'blort' in the vocabulary you are designed to handle from any other 'blort' you may find (just look at their expanded names!), as long as the third criterion is met:

  • No two inventors of XML vocabularies choose the same namespace name for their vocabularies.

The usual way to ensure the uniqueness of arbitrary identifiers is to have some sort of central registry. Central registries, however, are difficult to build and maintain, and XML namespaces use URIs as a way of piggy-backing on one of the most successful of registration authorities: the one that maintains the domain-name system at the heart of the Internet. As long as inventors of namespace names follow the basic rule of assigning namespace names only in the part of URI space they own or control, different inventors will never choose the same namespace name.

Using URIs as namespace names also has the nice property that the definer of the namespace can put human- and machine-readable documentation of the namespace at the namespace URI. Some authorities recommend this as good practice, but it is not required by the W3C's Namespaces in XML recommendation, in order to keep namespace processing as simple as possible. Most of the XML vocabularies intended for public use that I'm familiar with do provide documentation at their namespace URIs, though not all do. So you find out what belongs to a namespace by finding documentation for it, issued by the responsible entities. If they provide it at the namespace URI, it's easy; if not, it's harder; in some cases the question may not be answerable in practice. Life is hard sometimes.




回答2:


The URL (actually, an URI) is not meaningless at all, it is an unique identifier. With it, you don't need a central registry of all possible namespaces. Just create an URL in a domain you control and you can be sure you have an unique identifier. (URIs are not only a way to retrieve resources on the Web, they are also identifiers.)

It is considered good practice for namespace identifiers to be actual URL with some documentation available if you give this URL to a Web browser. But it is not mandatory.



来源:https://stackoverflow.com/questions/16201678/in-dtds-why-are-namespaces-given-as-a-url

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!