<body> |
<books> |
<book> |
<title>XSLT Programmers Reference</title> |
<author>Michael Kay</author> |
</book> |
</books> |
</body> |
</html> |
In this example, there’s no way to distinguish between the two title elements even though they are semantically different. A namespace can solve this problem by providing a unique identifier for a collection of elements and/or attributes. This is accomplished by prefixing each member element and attribute with a name, uniquely identifying them as part of that namespace. Grouping elements into a namespace allows them to be referenced easily by many XML documents and allows one XML document to reference many namespaces. XML namespaces are a form of qualifying attribute and element names. This is done within XML documents by associating them with namespaces that are identified with Universal Resource Indicators (URIs). |
A URI is a unique name recognized by the processing application that identifies a particular resource. URIs includes Uniform Resource Locators (URL) and Uniform Resource Numbers (URN). |
The following is an example of using a namespace declaration that associates the namespace |
http://www.w3.org/1999/xhtml with the HTML element. |
<html xmlns =”http://www.w3.org /1999/xhtml”> |
The xmlns keyword is a special kind of attribute that indicates you are about to declare an XML namespace. The information between the quotes is the URI, pointing to the actual namespace — in this case, a schema. The URI is a formal way to differentiate between namespaces; it doesn’t necessarily need to point to anything at all. The URI is used only to demarcate elements and attributes uniquely. The |
xmlns declaration is placed inside the element tag using the namespace. |
Namespaces can confuse XML novices because the namespace names are URIs and therefore often mistaken for a Web address that points to some resource; however, XML namespace names are URLs that don’t necessarily have to point to anything. For example, if you visit the XSLT namespace (http://www .w3.org/1999/XSL/Transform), you would find a single sentence: “This is an XML Namespace defined in the XSL Transformations (XSLT) Version 1.0 specification.” The unique identifier is meant to be symbolic; therefore, there’s no need for a document to be defined. URLs were selected for namespace names because they contain domain names that can work globally across the Internet and they are unique. |
The following code shows the use of namespaces to resolve the name conflict in the preceding example. |
<html xmlns=”http://www.w3.org/1999/xhtml”> |
<head> |
<title>Book List</title> |
</head> <body> |
<books xmlns=”http://www.wrox.com/books/xml”> |
<book> |
<title>XSLT Programmers Reference</title> <author>Michael Kay</author> |
</book> |
learn guitarphysics learnteliphonyxmlphysicsenjoylife