Making a namespace for yourself
|
|
|||
|
|
Last week we delved into SOAP and casually tossed out the term "namespace." While we didn't make much of it at the time, the fact is that namespaces are a crucial component of XML and, by extension, of SOAP and Web services.
You see, one of the problems of XML is its tremendous flexibility. For example, in the case of library data, the tag "name" could mean an author's name or a publisher's name, depending on the intention of the XML data set it appears in. One library's XML description of a book might be:
<libraryitem>
<title>The Truth Machine</title>
<publisher>
<name>Del Rey Books</name>
</publisher>
<author>
<name>James L. Halperin</name>
</author>
</libraryitem>
If you don't know the structure of a libraryitem, handing the XML data to a computer is likely to cause problems with understanding the attribute name.
So to make our intentions clear, we'll group the tags (properly called "elements") we use in an XML document under a name that provides, in effect, a category for all the tags under it. To define the architecture of the elements, we'll create a definition of the category using a Document Type Definition (DTD) or the more powerful XML Schema (XSD) specification.
An XSD schema is easier to understand than a DTD because it is written in XML. It also is far more comprehensive in its ability to characterize how the content of an XML file is structured. We'll visit XSD next week, as it is a huge topic.
So let us declare a namespace:
<vii:libraryitem xmlns:vii = "http://www.gibbs.com/demoschema">
What this defines is where the definition of the namespace "vii" can be found and that the element libraryitem is part of that namespace.
You may have noticed that the URL for the namespace definition points to a subdirectory that may contain a DTD or XSD. If there is no definition file, the result is merely declaring a unique identity, in this case, for the namespace "vii."
Any reference to another namespace also named "vii" but linked to a different URL would define a different namespace. Now, although a single XML document can reference multiple namespaces, duplicate names for namespace are not supported. So here's our file with all of the elements explicitly defined as belonging to the "vii" namespace:
<vii:libraryitem xmlns:vii = "http://www.gibbs.com/demoschema>
<vii:title>The Truth Machine</vii:title>
<vii:publisher>
<vii:name>Del Rey Books</vii:name>
</vii:publisher>
<vii:author>
<vii:name>James L. Halperin</vii:name>
</vii:author>
<vii:libraryitem>
As long as the only namespace in use is "vii" we could omit all references to it in all the elements between the first and last line (in other words, except for the first and last line, the content would be identical with the first example we showed).
Of course, if we had two or more schemas we could reference multiple namespaces with different names. Thus:
<libraryitemxmlns:vii = "http://www.gibbs.com/demoschema
xmlns:gac = "http://www.vitallyimportantinformation.com/schema>
So if we refer to elements such as "vii:name" and "gac:name" it's obvious the schema for each use of "name" is different.
Remember that an XML tag is made up of:
<element_name attribute_name = "attribute_value">element_value
</tag_name>
We even can define attributes as belonging to specific namespaces, different from the namespaces of the elements that possess the attributes, thus:
<vii:temperature gac:unit="Celsius">36</vii:temperature>
Next week, XSD. Whoopee!
RELATED LINKS
Comments and suggestions to gh@gibbs.com.
Gibbs Forum
The place to discuss Gibbs's columns.
Check out this week's edition of
Backspin for more musings from Gibbs.
Web services research page
Information and background on emerging standards such as XML, SOAP, WSDL, UDDI and more.
Web Applications newsletter
Stay current on new products and features, and learn how to get the most out of your apps.
Web services quandary
Tomorrow's business-to-business e-comm requires navigating the maze of conflicting Web services standards.
Network World Electronic Commerce Issue, 02/18/02.
Assembling a top-of-the-line Web services model
Ford Credit wins Network World's 2002 E-comm Innovator Award with an open-standards infrastructure that saves the company carloads of money.
Network World Electronic Commerce Issue, 02/18/02.

