DTDs

   



DTD Components

A DTD describes these optional XML document components in any combination: elements, attributes, and entities.

Elements

An element is the basic user-defined tag. An XML document is essentially a collection of elements. An element can contain text, other elements, or even a combination of the two. The syntax for an element in an XML document is <Name> content </Name>.

These are four of the elements from our XML sample:

   <Title>Debt of Honor</Title>    <Author>Tom Clancy</Author> <BookType>&PAPERBACK</BookType>    <Price>$6.99</Price>

Our sample also contained a LineItem element, which was comprised solely of other elements:

<LineItem> <Title>Debt of Honor</Title> <Author>Tom Clancy</Author> <BookType>&PAPERBACK</BookType> <Price>$6.99</Price> </LineItem>

The LineItem element consists of other elements.

A document's DTD declares all the elements a document can contain, as well as other characteristics of each of the elements (that is, its attributes).

Once an element is defined, the main section of the document can contain one or more instances of this element

In our sample, an element named 'Title' is defined in the DTD with the construct:

<!ELEMENT Title (#PCDATA)>

The Title element can now exist in the body of the sample document as follows:

   <Title>Debt of Honor</Title>

Attributes

An element can also be described in more detail with information that varies with each instance of the element in the document. For example, suppose we want our Order element to contain an attribute named orderNumber. In any particular Order element, this attribute would hold the unique ID that identified this Order.

Attributes are defined and used in much the same manner as elements. The DTD declares which attributes exist for an element. Particular elements in the document body can then be qualified with these attributes.

In our sample, an element named 'Order' is declared in the DTD. It consists of one or more LineItems. An attribute for Order named 'orderNumber' is defined as well. It can contain any valid string and is required for each instance of Order, as specified in this partial DTD:

<!ELEMENT Order (LineItem)+> <!ATTLIST Order orderNumber CDATA #REQUIRED>

An Order element can now exist in the body of the sample document as follows:

<Order orderNumber="81332713233407"> <LineItem> . . . </LineItem> </Order>

Notice that attributes are entered inside the initial element tag

(<Order . . >).

Entities

An XML document can define its own constants or entities. These are named storage units (portions of valid XML content), defined and used by a document. An entity can contain character strings, markup commands, or even references to external documents. Here are two entities as declared in our sample DTD:

<!ENTITY HARDCOVER "Hardcover"> <!ENTITY PAPERBACK "Paperback"> 

After they are declared, entities can be used in any appropriate place in the XML document. In our sample document, we can use the entity (by name, with an ampersand as a prefix and a semicolon as a suffix) in place of the text represented by the entity. Consequently, both of these constructs are valid:

   <BookType>&PAPERBACK;</BookType>    <BookType>"Paperback"</BookType>

Entities are often used for XML content that is frequently reproduced in the document, or that varies with each instance of the document type.



   



Fundamentals of SVG Programming. Concepts to Source Code
Fundamentals of SVG Programming: Concepts to Source Code (Graphics Series)
ISBN: 1584502983
EAN: 2147483647
Year: 2003
Pages: 362

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net