1. Criteria for being well-formed
Here are the main criteria for an XML document to be well-formed:
- Begin the document with an XML declaration
- There must be at least one element (i.e. the root element)
- Each element must contain both a beginning and an end tag
- The root element must contain all of the other elements
- The elements must be nested properly
- Attribute names within a tag must be unique
- Attribute values must be enclosed within quote symbols.
All of the above is familiar to me. So far so good. |
2. Using XML Namespaces
In some cases one may be using files from different sources and these sources may use the same names for some tags causing confusion over which definition should be used. In order to prevent such a situtation from arising, the concept of a namespace was introduced. A namespace is a way of adding a prefix to a tag's name to ensure that it is unique within the XML file that is using tags from more than one source. Simple. Now for the procedure for doing this.
Note: A Uniform Resource Indicator (URI) is very similar to a URL but is intended to be a more general idea. At the moment the two might be considered to be identical.
xmlns:prefix="URI" (read as XML namespace ...)
Example:
xmlns:hr="http://www.ajax/human_resources"
This creates a prefix hr that may then be used to refer to any of the tags used by that source.
Here is an example of a line in an XML document that is using this prefix:
<hr:address> (address was the original tag name)
One may create a default namespace by not using a prefix. This then allows one to use the original tag names without having to add a prefix.
Example:
xmlns:"http://www.ajax/human_resources"
"Namespaces aren't used solely to avoid tag (and attribute) name conflicts - using a namespace also indicates to an XML processor what XML application you're using." [p. 105]
QUIZ
1. To be well-formed, what's the least number of elements an XML document can contain?
One.
2. Why is the following XML document not well-formed?
<?xml version = "1.0" standalone = "yes"?>
<employee>
<name>Frank</name>
<position>Chef</position>
</employee>
<employee>
<name>Ronnie</name>
<position>Chef</position>
</employee>
Answer: There is no root element.
3. Why is the following XML document not well-formed?
<?xml version = "1.0" standalone = "yes"?>
<employee>
<kitchen_staff>
<name language=en>Frank</name>
<new_hire />
<position language=en>Chef</position>
</employee
Answer: The two uses of the language attribute failed to put quotes around the value en .
4. How can you create a namespace named service whose URI is
http://www.superduperbigco.com/customer_service ?
Answer: xmlns:service="http://www.superduperbigco.com/customer_service"
5. How could you set the default namespace in a set of XML elements to the URI
http://www.superduperbigco.com/customer_returns
Answer: xmlns:"http://www.superduperbigco.com/customer_returns"
|