Java Reference
In-Depth Information
This markup is a lot less cluttered than the earlier version that used qualified names, which makes it
much easier to read. It is therefore advantageous to declare the namespace that you use most
extensively in a document as the default.
You can declare several namespaces within a single element. Here's an example of a default namespace
in use with another namespace:
<sketch xmlns="http://www.wrox.com/dtds/sketches"
xmlns:print="http://www.wrox.com/dtds/printed">
<circle radius="15" angle="0">
<color R="150" G="250" B="100"/>
<position x="30" y="50"/>
</circle>
<print:circle print:lineweight="3" print:linestyle="dashed"/>
</sketch>
Here the namespace with the prefix print contains names for elements relating to hardcopy
presentation of sketch elements. The <circle> element in the print namespace is qualified by the
namespace prefix so it is distinguished from the element with the same name in the default namespace.
XML Namespaces and DTDs
For a document to be valid you must still have a DTD and the document must be consistent with it. The
way in which a DTD is defined has no specific provision for namespaces. The DTD for a document that
uses namespaces must therefore define the elements and attributes using qualified names and must also
make provision for the xmlns attribute with or without its prefix in the markup declaration for any
element in which it can appear. Because the markup declarations in a DTD have no specific provision
for accommodating namespaces, a DTD is a less than ideal vehicle for defining the rules for markup
when namespaces are used. The XML Schema specification provides a much better solution, and
overcomes a number of other problems associated with DTDs.
However, the XML Schema specification has been finalized and approved relatively recently - in the
first half of 2001. Consequently the present JAXP implementation distributed as part of SDK 1.4 has no
specific provision for schemas so we won't go into them here.
Working with XML Documents
Right at the beginning of this chapter we introduced the notion of an XML processor as a module that is
used by an application to read XML documents. An XML processor parses the contents of a document
and makes the elements together with their attributes and content available to the application, so it is
also referred to as an XML parser . In case you haven't met the term before a parser is just a program
module that breaks text in a given language down into its component parts. A natural language
processor would have a parser that identifies the grammatical segments in each sentence. A compiler
has a parser that identifies variables, constants, operators, etc. in a program statement. An application
accesses the content of a document through an API provided by an XML parser and the parser does the
job of figuring out what the document consists of.
Search WWH ::




Custom Search