Sams Teach Yourself XML in 21 Days

Sams Teach Yourself XML in 21 Days

By Steven Holzner

Summary

Today's discussion describes how to use attributes and entities with DTDs. You saw that you declare attributes in a DTD by using an <!ATTLIST> element.

You can assign various default values to attributes when you declare them. You can assign an explicit default value—#IMPLIED to make the attribute optional, #FIXED to give it a fixed value, and #REQUIRED to make an attribute required —enclosed in quotation marks,.

You can also declare the type of an attribute in an <!ATTLIST> element. The various attribute types are CDATA, which means character data; ENTITY, which means you can assign an entity name to an attribute; ENTITIES, which you can use to assign a list of entity names to an attribute; an enumeration, which specifies a list of possible values for an attribute; an ID value; an IDREF value, which holds the ID of another element; IDREFS, which holds multiple ID values; NMTOKEN, which can hold text made up of XML name characters; NMTOKENS, which can hold multiple NMTOKEN items; and NOTATION, which holds the name of a notation (that is, a format description).

In this discussion you also saw how to work with entities. You saw that you can declare entities by using the <!ENTITY> element. And you saw that entity is XML's term for a data item, and when you declare entities in a DTD, you can refer to them in the rest of the XML document.

In general, entity references in an XML document will be replaced by the entity they stand for when the entity is text. However, you can also create unparsed entities that will not be parsed by the XML processor; for example, external image files in binary format are usually unparsed external entities.

DTDs were introduced when XML was, and they haven't really grown since that time. They're still the technique of choice for validating XML documents with much of the software out there, but the real growth these days is in XML schemas. XML schemas give you the ability to pinpoint the format of your data in a way that DTDs can't, and you'll be hearing more about XML schemas over the next two days.

Share ThisShare This

Informit Network