Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

XML DTDs : Page 5

Valid XML documents follow a set of rules defined in a associated DTD. This Document Type Definition defines elements, attributes, and relationships between elements.


advertisement

Making Elements
Elements are the basic building blocks of XML. You define elements in a DTD; you use them in a document. A basic element definition looks like this:

<!ELEMENT DESCRIPTION (#PCDATA, DEFINITION)*>

Element Declaration
Each element begins with an element declaration, <!ELEMENT. This announces that you are defining an element.



Element Name
After the declaration is the element's name. The way the name appear in the element definition is exactly the way it must be used in the XML document. Capitalization counts!

Element Rule
After the name comes a rule that describes what the element can contain. Through this description, the element take on hierarchal relationships with each other.

Although the basic bits of the rules are simple, they can be grouped and combined to create quite complex definitions.

This table summarizes the element rule definitions.

Contents
Elements can contain text, other elements, a combination of text and other elements, or they may be empty.

Text. Elements can contain textual data.

Other Elements. Elements can contain only other specified elements and no text. The contained element are called children of the containing element. The containing element is the parent of the child elements.

Combination. Element can contain a mix of textual data and other specific elements.

Empty. Empty elements get their value from their attributes. An empty element will typically have at least one attribute. In HTML, the IMG tag is a good example of an empty element. It gets its value from the src attribute.

Number of Occurences
You can specify the number of times a child element is used within its parent.

Once and only once. The element listed by itself indicates that it can be used once and only once:

DTD definition

Used in document

<!ELEMENT EVENTLIST (EVENT)>

<EVENTLIST>
<EVENT>Balsa Wood Flyer Days</EVENT>
</EVENTLIST>

At least once, or many times. The element followed by a plus sign indicates that this element can be used many times with the parent:

DTD definition

Used in document

<!ELEMENT EVENTLIST (EVENT+)>

<EVENTLIST>
<EVENT>Balsa Wood Flyer Days</EVENT>
<EVENT>Sundays in the Park</EVENT>
<EVENT>Teach Your Child to Fly</EVENT>
</EVENTLIST>

Once or not at all. The element followed by a question mark indicates that this element can be used either one time or not at all:

DTD definition

Used in document

<!ELEMENT EVENT (LOCATION, SPONSOR?)>>

<EVENT>
<LOCATION>West Bay Ballpark</LOCATION>
</EVENT>

or

<EVENT>
<LOCATION>West Bay Ballpark</LOCATION>
<SPONSOR>Flying Toys</SPONSOR>
</EVENT>

Once, not at all, or a many times as you want. The element followed by an asterisk indicates that this element can be used as many time as needed.

DTD definition

Used in document

<!ELEMENT EVENT (LOCATION*, EVENT-NAME)>

<EVENT>
<LOCATION>West Bay Ballpark</LOCATION>
<LOCATION>North Side Park</LOCATION>
<EVENT-NAME>Sundays in the Park</EVENT-NAME>
</EVENT>

or

<EVENT>
<EVENT-NAME>Sundays in the Park</EVENT-NAME>
</EVENT>

Order
You can specify the order in which child elements appear.

Specific order. Child elements can be defined to be used in a specific order. The comma (,) separates elements that are listed in a specific order. For example, you could set a rule that creates an EVENTLIST. In the list, you must always use the EVENT element, followed by the SPONSOR element.

DTD definition

Used in document

<EVENTLIST (EVENT, SPONSOR)>

<EVENTLIST>
<EVENT>Balsa Wood Plane Days</EVENT>
<SPONSOR>Flying Toys</SPONSOR>
</EVENTLIST>

Either Or. You can define child elements so that one or another can be used. The bar (|) separates either or choices.

DTD definition

Used in document

<EVENT (EVENT-NAME | SPONSOR)>

<EVENT>
<EVENT-NAME>Balsa Wood Plane Days</EVENT-NAME>
</EVENT>

or

<EVENT>
<SPONSOR>Flying Toys</SPONSOR>
</EVENT>

Groups
Groups can be used to create complex rules, that combine elements and different usage option.

For example, when groups are combined with a "use many times" symbol, you can create a rule that allows multiple uses of elements -- either in in any order or as repeated sets. For example, here the element EVENTLIST can contain multiple sets of EVENT and SPONSOR groups:

DTD definition

Used in document

<EVENTLIST (EVENT, SPONSOR)*>

<EVENTLIST>
<EVENT>Balsa Wood Plane Days</EVENT-NAME>
<SPONSOR>Flying Toys</SPONSOR>
<EVENT>Sundays in the Park</EVENT-NAME>
<SPONSOR>Deer Island Recreation Department</SPONSOR>
</EVENTLIST

Here, the EVENTLIST can contain either the EVENT element or the SPONSOR element, but this either or group can be used many times.

DTD definition

Used in document

<EVENTLIST (EVENT | SPONSOR)*>

<EVENTLIST>
<EVENT>Balsa Wood Plane Days</EVENT-NAME>
<SPONSOR>Flying Toys</SPONSOR>
<SPONSOR>Deer Island Recreation Department</SPONSOR>
</EVENTLIST

Hints for Element Names

  • Select names that are both easy to remember and easy to type.
  • Give your tags should have some inherent meaning. For example, if you want to identify "last name" as an element, consider naming the element something like "last-name" or "surname."
  • Use names that are consistent with current processes. If people call "social security number" SSN, create an element called SSN. Don't create an unfamiliar "socsecnum" element.
  • Be consistent in your use of names. It is easier to apply one set of general rules to 20 different tags than it is to remember eight discrete tags that follow no particular pattern.




Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date