|
Java uses packages to avoid name clashes. Programmers can use the same name for different classes as long as they aren't in the same package. XML has a similar namespace mechanism for element and attribute names. A namespace is identified by a Uniform Resource Identifier (URI), such as http://www.w3.org/2001/XMLSchema uuid:1c759aed-b748-475c-ab68-10679700c4f2 urn:com:books-r-us The HTTP URL form is the most common. Note that the URL is just used as an identifier string, not as a locator for a document. For example, the namespace identifiers http://www.horstmann.com/corejava http://www.horstmann.com/corejava/index.html denote different namespaces, even though a web server would serve the same document for both URLs. There need not be any document at a namespace URLthe XML parser doesn't attempt to find anything at that location. However, as a help to programmers who encounter a possibly unfamiliar namespace, it is customary to place a document explaining the purpose of the namespace at the URL location. For example, if you point your browser to the namespace URL for the XML Schema namespace (http://www.w3.org/2001/XMLSchema), you will find a document describing the XML Schema standard. Why use HTTP URLs for namespace identifiers? It is easy to ensure that they are unique. If you choose a real URL, then the host part's uniqueness is guaranteed by the domain name system. Your organization can then arrange for the uniqueness of the remainder of the URL. This is the same rationale that underlies the use of reversed domain names in Java package names. Of course, although you want long namespace identifiers for uniqueness, you don't want to deal with long identifiers any more than you have to. In the Java programming language, you use the import mechanism to specify the long names of packages, and then use just the short class names. In XML, there is a similar mechanism, like this:
The element and its children are now part of the given namespace. A child can provide its own namespace, for example:
Then the first child and the grandchildren are part of the second namespace. That simple mechanism works well if you need only a single namespace or if the namespaces are naturally nested. Otherwise, you will want to use a second mechanism that has no analog in Java. You can have an alias for a namespacea short identifier that you choose for a particular document. Here is a typical example: <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <xsd:element name="gridbag" type="GridBagType"/> . . . </xsd:schema> The attribute
defines a namespace and an alias. In our example, the alias is the string xsd. Thus, xsd:schema really means "schema in the namespace http://www.w3.org/2001/XMLSchema". NOTE
You can control how the parser deals with namespaces. By default, the Sun DOM parser is not "namespace aware." To turn on namespace handling, call the setNamespaceAware method of the DocumentBuilderFactory: factory.setNamespaceAware(true); Then all builders the factory produces support namespaces. Each node has three properties:
Here is an example. Suppose the parser sees the following element: <xsd:schema xmlns:xsl="http://www.w3.org/2001/XMLSchema"> It then reports the following:
NOTE
org.w3c.dom.Node 1.4
javax.xml.parsers.DocumentBuilderFactory 1.4
|
|