Java Reference
In-Depth Information
You may be thinking at this point that an empty element is of rather limited use, whichever way you
write it. Although by definition an empty element has no content, it can and often does contain
additional information that is provided within attributes that appear within the tag. We shall see how
we add attributes to an element a little later in this chapter. Additionally, an empty element can be used
as a marker or flag to indicate something about the data within its parent. For example, you might use
an empty element as part of the content for an <address> element to indicate that the address
corresponds to a commercial property. Absence of the <commercial/> element would indicate a
private residence.
Document Comments
When you create an XML document using an editor, it is often useful to add explanatory text to the
document. You include comments in an XML document like this:
<!-- Prepared on 14th January 2002 -->
Comments can go just about anywhere in the prolog or the document body, but not inside a start tag or
an end tag, or within an empty element tag. You can spread a comment over several lines if you wish,
like this:
<!--
Eeyore, who is a friend of mine,
has lost his tail.
-->
For compatibility with SGML from which XML is derived, the text within a comment should not
contain a sequence of two or more hyphens and it also must not end with a hyphen. A comment that
ends with ---> is not well-formed and will be rejected by an XML processor. While an XML
processor of necessity scans comments in order to distinguish them from markup and document data
they are not part of the character data within a document. XML processors need not make comments
available to an application, although some may do so.
Element Names
If we're going to be creating elements then we're going to have to give them names, and XML is very
generous in the names we're allowed to use. For example, there aren't any reserved words to avoid in
XML, as there are in most programming languages, so we do have a lot of flexibility in this regard.
However, there are certain rules, which you must follow. The names you choose for elements must
begin with a letter or an underscore and can include digits, periods, and hyphens. Here are some
examples of valid element names:
net _ price Gross-Weight _ sample clause _ 3.2 pastParticiple
In theory you can use colons within a name but since colons have a special purpose in the context of
names, as we shall see later, you should not do so. Since XML documents use the Unicode character set,
any national language alphabets defined within that set may be used for names. HTML users need to
remember that tag names in XML are case sensitive, so <Address> is not the same as <address> .
Note also that names starting with upper or lowercase x , followed by m followed by l , are reserved, so
you must not define names beginning xml or XmL or any of the other six possible sequences.
Search WWH ::




Custom Search