Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
847 views
in Technique[技术] by (71.8m points)

xml - In DTDs, why are namespaces given as a URL?

Apparently, the namespace URL that follows xmlns in HTML 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...