XML

Summary

If you're familiar with the old sitcom Newhart, you no doubt remember the two brothers who were both named Darrel. Although brothers with the same first name make for good comedy, similar names in XML documents can be problematic. I'm referring to name clashes that can occur when elements and attributes are named the same across multiple custom markup languages. This problem can be easily avoided by using namespaces, which allow you to associate elements and attributes with a unique name. Namespaces are an important part of XML because they solve the problem of name clashing among XML documents.

This tutorial introduced you to namespaces and also gave you some practical insight regarding how they are used in XML documents. You began the tutorial by learning the basics of namespaces and their significance to XML. From there you learned how namespaces are named. You then found out how to declare and use namespaces in documents. And finally, the tutorial concluded by revisiting XSD schemas and uncovering a few interesting tricks involving schemas and namespaces.

Q&A

Q.

When a name clash occurs in an XML document, why can't an XML processor resolve it by looking at the scope of the elements and attributes, as opposed to requiring namespaces?

A.

Although it is technically possible for an XML processor to resolve an element or attribute based solely on its scope, it isn't a good idea to put that much faith in the processor. Besides, there are some situations where this simply isn't possible. For example, what if the element causing the name clash is the root element in a document? Because it has a global scope, there is no way to determine the schema to which it belongs.

Q.

Do I have to use a namespace to uniquely identify the elements and attributes in my custom markup language?

A.

No. In fact, if you plan on using your XML documents internally and never sharing them with others, there really is no pressing need to declare a unique namespace. However, if you choose to incorporate multiple XML-based markup languages within a single document or application, you'll need to use namespaces to keep things straight and not confuse the XML processor.

Workshop

The Workshop is designed to help you anticipate possible questions, review what you've learned, and begin learning how to put your knowledge into practice.

Quiz

1.

Why are namespaces named after URIs?

2.

What is the general form of a namespace declaration?

3.

What is the difference between default and explicit namespace declaration?

Quiz Answers

1.

Namespaces are named after URIs because URIs are guaranteed to be unique.

2.

The general form of a namespace declaration is xmlns:Prefix="NameSpace".

3.

A default namespace declaration is useful when you want to apply a namespace to an entire document or section of a document, whereas an explicit namespace is useful whenever you want exacting control over the elements and attributes that are associated with a namespace.

Exercises

1.

Using a domain name that you or your company owns, determine a unique namespace name that you could use with the Tall Tales document from previous tutorials.

2.

Modify the Tall Tales document so that the elements and attributes defined in its schema are associated with the namespace you just created.

by BrainBellupdated
Advertisement: