!doctype Application Public "-//sun Microsystems, Inc.//Dtd J2Ee Application
!doctype Application Public "-//sun Microsystems, Inc.//Dtd J2Ee Application
System and public identifiers for the DTD that can be used to validate the document
structure.
The simplest DOCTYPE declaration identifies only the root element of the document.
<!DOCTYPE rootElement>
More often, documents that use the DOCTYPE declaration reference an external document
containing the declarations that make up the DTD. The following can be used to identify the
external DTD.
The PublicIdentifier provides a separate identifier that some XML parsers can use to
reference the DTD in place of the URIreference. This is useful if the parser is used on a system
without a network connection or where that connection would slow down processing
significantly.
DOCTYPE declarations can also include declarations directly, in what is referred to as the
internal subset. If a DOCTYPE declaration includes the entire DTD directly, without reference to
external files, it uses the following syntax.
<!DOCTYPE rootElement [
declarations
]>
If the DOCTYPE declaration includes declarations that are to be combined with external files or
the external subset, it uses the following syntax.
or
URI :
One can classify URIs as locators (URLs), or as names (URNs), or as both. A Uniform Resource Name
(URN) functions like a person's name, while a Uniform Resource Locator (URL) resembles that person's
street address. In other words: the URN defines an item's identity, while the URL provides a method for
finding it.
This entry may be useful to people confused by BizTalk's inclination to "ns0" at the drop of a hat
:-)
Someone asked me today why their XML document wasn't validating. They thought that it was
because the schema was expecting messages with a default namespace of "uri:my-namespace"
but the document they were validating instead defined all the nodes using a prefix of "ns0", with
ns0 mapping to uri:my-namespace.
One of the biggest misunderstandings in XML is namespaces. There's absolutely no difference to
an xml parser or to the interpretation of a document whether you use a prefix on every node, or a
default namespace. Prefixes are only a syntactic abbreviation. In other words:
<mydoc xmlns="uri:my-namespace">
<subnode />
</mydoc>
(Default namespace flows into the child - there is no prefix, so it uses the default
namespace)
is identical to:
<ns0:mydoc xmlns:ns0="uri:my-namespace">
<ns0:subnode xmlns:ns1="uri:my-namespace" />
</ns0:mydoc>
(The prefix ns0 maps to uri:my-namespace, and both nodes are marked with ns0)
and to:
<ns0:mydoc xmlns:ns0="uri:my-namespace">
<ns1:subnode xmlns:ns1="uri:my-namespace" />
</ns0:mydoc>
(No default namespace, but ns0 and ns1 both map to uri:my-namespace and hence have
identical meaning)
and
<ns0:mydoc xmlns:ns0="uri:my-namespace">
<subnode xmlns="uri:my-namespace" />
</ns0:mydoc>
(ns0 is defined on the parent, but we then set a default namespace for this node - and any
children, if it had any)
It turned out in the end that the problem was that the schema defined the namespace as "uri:my-
namespace" but the result document was coming back with a namespace of "uri:my-namespace/"
- the trailing slash being all important. Remember folks, namespaces are just strings - a trailing
slash may not make a difference to most Internet browsers, but they do in XML Schema.
ElementFormDefault
As a bonus, a little note on elementFormDefault.
If in your schema you use elementFormDefault="qualified" then you can validate all of the
documents above with a schema similar to (pseudo-code):
<xs:schema targetNamespace="uri:my-namespace" elementFormDefault="qualified">
<xs:element name="myDoc">
<xs:complexType>
<xs:sequence>
<xs:element name="subnode" maxOccurs="1" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
If you changed the elementFormDefault to "unqualified" then instead it would only look for the root element to be in
the namespace, and child elements would be unqualified (in the empty namespace):
<ns0:mydoc xmlns:ns0="uri:my-namespace">
<subnode/>
</ns0:mydoc>
and
<ns0:mydoc xmlns:ns0="uri:my-namespace">
<ns1:subnode xmlns:ns1=""/>
</ns0:mydoc>
(An explicitly empty prefixed namespace definition)
and
<ns0:mydoc xmlns:ns0="uri:my-namespace">
<subnode xmlns=""/>
</ns0:mydoc>
(An explicitly empty default namespace definition for the sub node, and its child nodes if it had any)
I was lucky enough to learn about Namespaces on a Developmentor training course run by Aaron Skonnard ages
ago so thanks to him for the foundations of this entry :-)
XML Namespaces
« Previous Next Chapter »
Name Conflicts
In XML, element names are defined by the developer. This often results in a conflict when trying
to mix XML documents from different XML applications.
This XML carries HTML table information:
<table>
<tr>
<td>Apples</td>
<td>Bananas</td>
</tr>
</table>
<table>
<name>African Coffee Table</name>
<width>80</width>
<length>120</length>
</table>
If these XML fragments were added together, there would be a name conflict. Both contain a
<table> element, but the elements have different content and meaning.
This XML carries information about an HTML table, and a piece of furniture:
<h:table>
<h:tr>
<h:td>Apples</h:td>
<h:td>Bananas</h:td>
</h:tr>
</h:table>
<f:table>
<f:name>African Coffee Table</f:name>
<f:width>80</f:width>
<f:length>120</f:length>
</f:table>
In the example above, there will be no conflict because the two <table> elements have different
names.
The namespace is defined by the xmlns attribute in the start tag of an element.
<root>
<h:table xmlns:h="http://www.w3.org/TR/html4/">
<h:tr>
<h:td>Apples</h:td>
<h:td>Bananas</h:td>
</h:tr>
</h:table>
<f:table xmlns:f="http://www.w3schools.com/furniture">
<f:name>African Coffee Table</f:name>
<f:width>80</f:width>
<f:length>120</f:length>
</f:table>
</root>
In the example above, the xmlns attribute in the <table> tag give the h: and f: prefixes a qualified
namespace.
When a namespace is defined for an element, all child elements with the same prefix are
associated with the same namespace.
Namespaces can be declared in the elements where they are used or in the XML root element:
<root
xmlns:h="http://www.w3.org/TR/html4/"
xmlns:f="http://www.w3schools.com/furniture">
<h:table>
<h:tr>
<h:td>Apples</h:td>
<h:td>Bananas</h:td>
</h:tr>
</h:table>
<f:table>
<f:name>African Coffee Table</f:name>
<f:width>80</f:width>
<f:length>120</f:length>
</f:table>
</root>
Note: The namespace URI is not used by the parser to look up information.
The purpose is to give the namespace a unique name. However, often companies use the
namespace as a pointer to a web page containing namespace information.
Try to go to http://www.w3.org/TR/html4/.