- Dissecting an XML Document Type Definition
- Using Document Type Definition Notation and Syntax
- Understanding Literals
- Declaring a NOTATION
- Creating ATTLIST Declarations
- Using Special XML Datatype Constructions
- Understanding the Difference Between Well-Formed and Valid XML
- Learning How to Use External DTDs and DTD Fragments
- Altering an XML DTD
- Getting Down to Cases
Creating ATTLIST Declarations
ATTLIST is a container for the attributes associated with an ELEMENT. It has a straightforward syntax but complex alternatives that make it challenging to know every one:
<!ATTLIST element-name attribute-name-1 datatype default-data attribute-name-2 datatype default-data ... >
The following subsections define which datatypes can be used in an attribute list. For non-validating parsers, these datatypes are somewhat beside the point because the parser won't know anything about them. If your interest lies in downstream processing of XML documents using non-validating parsers, you can skip this whole section without much loss.
There are many more datatypes allowed in attributes than there are in content models. Where a content model allows only for PCDATA, attributes can take fairly specific datatypes. The XML attribute datatype defines in general terms what sort of information an entity can contain and how it will be structured. The attribute datatype is taken from the following list:
CDATACharacter Data is a string.
IDA tokenized unique ID within a document. Although you're permitted to declare a default value, there's really no point in doing so. Also, because you need access to the DTD to determine whether there is an ID contained in the element, it's worthless to non-validating processors. Most tools that depend on IDs posit a theoretical ID with the name "id" that exists within every element and uses it whether it's been declared or not.
IDREFA tokenized reference to a unique ID. See ID.
IDREFSA tokenized list of references to unique IDs within a document. See ID.
ENTITYA tokenized reference to an entity which refers to a notation. This is another way of referring to unparsed data.
ENTITIESA tokenized list of references to entities which refer to notations. This is another way of referring to unparsed data.
NMTOKENA tokenized reference to a NAME token.
NMTOKENSA tokenized reference to a list of NAME tokens.
( attr1 | attr2 | ... )An enumerated list of possible attribute values. Many XML authoring environments display these as a drop-down or pop-up selection list for ease of use.
Because multiple attributes can exist in a single element, an ATTLIST can get complex. They all obey the same set of rules, however, so careful study should set you right after any initial confusion.
The following subsections go into greater detail about the contents of attributes, starting with the default value syntax.
Specifying Default Data in an Attribute
The default-data placeholder in the attribute list template earlier in this section can be either #REQUIRED, #IMPLIED, or a default value; or #FIXED and a constant value.
These are working examples:
<!ATTLIST elementname attr1 #REQUIRED attr2 #IMPLIED attr3 "default-value" attr4 #FIXED "constant-value">
Both #REQUIRED and #IMPLIED don't take a default value because they would be meaningless in context. If the attribute is #REQUIRED, it has to be there whether there's a default value or not. If the attribute is #IMPLIED, the attribute is not necessary and need not be present.
If ever there was a confusing term, #IMPLIED is it. It sounds almost like "default." You might naively think that the "implied" value was the default. But it isn't. Implied is there to tell the processor that it needn't bother looking for an attribute because it might not be there. The choice of words was an unfortunate lapse from clarity, nothing more.
IMPLIED is a holdover from the days of SGML, when straightforward description was sometimes sacrificed for a precise "logical" term whose origins lie hidden in the dusty mind of a programmer from the ancient days. In my opinion, the word is a simple malapropism because a somewhat related and often confused term, "inferred," describes precisely what the processor should do with an "implied" attribute, which is to infer the attribute's existence or non-existence based on the presentation of the element in the XML document itself.
Attributes of Type CDATA
This is really simple. It's a character string. The following example defines an attribute named string whose value is CDATA and a default value for that string:
<!ATTLIST name string CDATA "string stuff">
Note that, as always, non-validating parsers won't read the definition or default value of external DTDs.
Attributes of Type ID
These refer to unique IDs within your document and furnish an easy way to ensure that there is an available name to form the other end of a link. Although this is a clever thing to do, in practice most browsers are probably not going to validate and will never see the ID definition. Most current pointer tools impute the presence of an attribute of type ID named id or name whether it's in the DTD or not, thereby letting them generate links without tears.
The following code defines an ID attribute named node and then shows how the attribute might be used in an XML document.
DTD declaration: <!ATTLIST name node ID #REQUIRED> XML document instance: <name node="A1234"
A safer course would be to define the ID attribute with the name id, so non-validating parsers can infer its intent just as HTML browsers do the name attribute. In the following section, you'll see two ways to do this.
Attributes of Type IDREF
This is a way to refer to the other end of an id/idref pair. The IDREF points to the id.
DTD declaration: <!ATTLIST anyname pointer IDREF #REQUIRED> XML document instance: <anyname pointer="A1234"
In HTML terms, this is the rough equivalent of the href attribute that points to a named anchor.
IDs are so handy it's a shame to have to plug them in everywhere, especially when you don't have write permissions on the source document. The next generation of linking mechanisms, XML XLinks and XPointers, will allow you to automatically use the document structure itself to obtain nearly the same result in the display document.
In an ideal world, IDs are the most likely artifacts to remain stationary, or relatively so, in a document. For this reason, they make good targets for pointers. The URL#namevalue fragment syntax used in HTML to point to locations within the document also works in XML, except that the target must be an ID.
The corollary to this is that both ID and name attributes should be declared for every possible target, because legacy user agents will find the element containing the indicated name attribute and XML-aware user agents will find the ID. You could, theoretically, collapse the two into one attribute by declaring a name attribute to be of type ID if you knew that the document would be viewed using validating XML parsers only. Unfortunately, the ID tag is also used in HTML, in a type of schizophrenia resulting from trying to keep the old syntax and create a new one that seemed more self-explanatory. So, it's best to declare two attributes with the same value. ID-type attributes must be unique within a document and name attributes should duplicate the ID-type attribute.
So ideally, one would define the following attributes in any element that might be used as the target of a hyperlink:
<!ATTLIST anylink id ID #IMPLIED name NMTOKEN #IMPLIED other-attributes... >
If you wanted to enforce the presence of the two link attributes on an element, you could define them as required like this:
<!ATTLIST anylink id ID #REQUIRED name NMTOKEN #REQUIRED other-attributes... >
Please note that the name attribute can't be of type ID, although it should duplicate the content of the id attribute, since the parser would enforce ID attribute uniqueness, preventing you from declaring a second instance of an attribute of type ID. In use, you could instantiate the target of a hyperlink like this:
<anylink id="A1234" name="A1234">anytargetcontent</anylink>
If the name of the anylink element were chosen to be a, both HTML-only browsers and XML-aware browsers would find the same location.
<a id="A1234" name="A1234">anytargetcontent</a>
This is the strategy used by XHTML, since it's designed in part to be accessible to as many legacy HTML browsers as possible as well as providing the benefits of extensibility for XML-aware browsers.
Attributes of Type ENTITY
This is how you would use an attribute of type ENTITY to refer to a notation:
<!NOTATION gif PUBLIC "..."> <!ENTITY gifpicture SYSTEM "mypicture.gif" NDATA gif> <!ELEMENT picture EMPTY> <!ATTLIST picture src ENTITY #REQUIRED>
In an XML document proper you would use the picture tag defined in the DTD like this:
This is one more way that XML requires you to be a multimedia guru. Although I'm sure there's an important distinction between the many ways available to do this, the real question is whether it should be done at all. See my comments on notations in general earlier in this chapter.
I hate to harp on this and I promise not to do it after this chapter, but forcing the XML processor to know about all the possible multimedia types is not a robust way of doing things. In the first place, it means that you can't insert an image or multimedia file within a document unless you can guarantee that all your users will be using validating XML parsers. This seems excessively limiting. The W3C XML working group seems to have ignored the example of browser plug-ins and server-side redirection, which would have made it possible to negotiate with the user agent to ascertain its capabilities and supply an appropriate multimedia file if possible. If not, the user agent would know that it had to find an appropriate helper on its own, just as most now do with unrecognized MIME content types. A better mechanism should have been provided. Content negotiation between the browser and the server would have been the modern method of providing this flexibility, because different user environments might require entirely different file formats.
That said, the convention does work, even if it's awkward, and it may be replaced in the future with a simple mechanism that ignores the existing machinations in favor of some other method of identifying content type. The W3C standards for persistent URIs strongly discourage tying locations to data type and access methods, which the existing notation syntax violates.
Because the datatype might change in future, W3C currently recommends that content negotiation take place between the server and the user agent to determine what data types are supported and what types are available. In that case, you should refer to a notation as a bare name, like "myimage", and let the server figure out what kind of image is available for that particular agent and supply it, telling the agent what it supplied so there can be no mistake. It will all come out in the wash, as they say, so rest assured that this will undoubtedly change sometime in future.
Attributes of Type NMTOKEN
The contents of an attribute are constrained to look like a name token as defined in the XML Recommendation. In other words, it consists of one Unicode/ISO letter, ideograph, underscore, or colon followed by letters, ideographs, digits, periods, hyphens, underscores, or colons repeated as often as you like in any combination. This means simply that a name can't start with a digit, period, or hyphen, although it may contain any of these plus underscores or colons after the initial character but no other typographical symbols like asterisks, slashes, strokes, ampersands, or the like. The phrase "letters and ideographs" includes the characters one would ordinarily use to indicate words in a given language. For example, in Chinese these might be single characters indicating complete words; in Japanese, characters indicating complete syllables; and in many languages, individual letters indicating consonants and vowels which are combined to form words. The potential combinations of these extend the meaningfulness of names to include almost the entire population of the world literate in any human language.
As stated previously, using colons is strongly discouraged because they're easily mistaken for a namespace prefix. Also, the string "xml" in any case combination is reserved.
These are NMTOKENS:
name new_name _name.name-suffix name.suffix :name luftballoons99 name1.suffix_part
and these are not:
name&more 99luftballoons .name $value name(function)
Although few texts on XML, in English at least, demonstrate the use of ideographs and non-Roman scripts in an element name due to the difficulty of typesetting them, among other reasons, the capability exists and will be used extensively in those areas that need this capability. Figure 3.3 shows an example of a Japanese language XML document available on the Web from the FujiXerox site at http://www.fxis.co.jp/DMS/sgml/xml/charset/utf-8/weekly.xml.
Figure 3.3 An example of a Japanese language document marked up with meaningful Japanese XML tags.
The link to this example was kindly supplied by the Chinese XML Now! site of Academia Sinica at http://www.ascc.net/xml/. The site contains numerous links to information of interest to developers of Chinese XML documents, test suites to confirm the capability to handle Chinese input properly, and examples of Chinese language XML documents.
Attributes of Type NOTATION
These have to match the NMTOKEN definition and are given in a simple alternation list that defines the enumeration of possible types. One of these can be chosen but not more than one. A default value can be specified but need not be. They look like this in use:
<!ATTLIST name type (gif87a|gif89a) >
The same NOTATION enumeration would look like this when defined with a default value:
<!ATTLIST name type (gif87a|gif89a) "gif89a" >
All notations must be declared before they're used in an attribute list.
Enumerated attributes look just like the previous examples and are defined using an alternation list, but don't refer to notations. They're handy for creating lists of choices. Most XML editing environments give you a little drop-down list for choosing which value you want out of the list. The following code demonstrates a simple enumerated attribute as defined in a DTD:
<!ATTLIST name choice (yes|no) >
Like NOTATION attributes, enumerated attribute values can take a default value:
<!ATTLIST name choice (yes|no) "yes" >
The above DTD snippet defines an attribute, choice, that can take the values "yes" or "no" and whose default value is "yes".