Declaring Elements

printer-friendly version of this section  Print  e-mail this section  E-Mail  add a public, group or private note  Add Note  add a bookmark about this section  Add Bookmark    

Java APIs for XML Kick Start
By Aoyon Chowdhury, Parag Choudhary

Table of Contents
Appendix B.  XML: A Quick Tour


As mentioned earlier, you can define the elements and the order in which they should occur. You can also determine how many times an element can occur. The element declaration for a DTD takes the following form:

<!ELEMENT name content_model> 

Here the name argument is the name of the element and the content model can be one of the following:

  • EMPTY

  • ANY

  • An element content model

  • Character data

If the content is EMPTY, the element cannot have any data or subelements. This is often used to refer to external files such as graphics, and so on.

If the content is ANY, the element can contain any element that is declared in the DTD, or any type of text data or CDATA section. In our case, the forCDATA element contains the CDATA section. Therefore, the corresponding element declaration is as follows:

<!ELEMENT forCDATA ANY> 

Element Content Model

An element content model is a description of how the subelements might appear under the current element. The model describes the order of the elements and how many times they can or will occur.

The general form for an element that has a content model is as follows:

<!ELEMENT name_of_element (element1, element2.....)> 

Additionally, you can also specify element occurrence indicators. An element occurrence indicator specifies how many times an element or a group of elements can appear under an element. There are three element occurrence indicators: +, ?, and *.

The ? occurrence indicator specifies that an element or group of elements can occur once at most under an element. For example, consider the following element declaration:

<!ELEMENT sample (sample1?,sample2)> 

This element declaration implies that only the following two pieces of XML will be valid:

<sample>     <sample1>some data</sample1>     <sample2>another data</sample2> </sample> 

and

<sample>     <sample2>another data</sample2> </sample> 

The + occurrence indicator specifies that an element or group of elements must occur one or more times.

The * occurrence indicator specifies that an element or group of elements must occur zero or any number of times in an XML document.

Let's see how these rules are applied for the DTD for the CarParts.xml file. The carparts element is the root element of the XML document and all other elements must come under it. So, the corresponding element declaration is as follows:

<!ELEMENT carparts (hubcaps,supplier,engines,carbodies,wheels,carstereos, forCDATA)> 

Next, the engines, carbodies, wheels, and carstereos elements can contain subelements, and each of these subelements must occur one or more times. Therefore, the element declarations are as follows:

<!ELEMENT engines (engine+)> <!ELEMENT carbodies (carbody+)> <!ELEMENT wheels (wheel+)> <!ELEMENT carstereos (carstereo+)> 

Character Data

The last type of content that an element can have is text. To specify that an element can have only text, you use the #PCDATA keyword. The construct is as follows:

<!ELEMENT name (#PCDATA)> 

Coming back to our DTD, we know that the hubcaps element can have only text as its content. So, the element declaration for hubcaps is as follows:

<!ELEMENT hubcaps (#PCDATA)> 

We will conclude our discussion on DTD with a brief overview of how to declare the attributes for the elements.


printer-friendly version of this section  Print  e-mail this section  E-Mail  add a public, group or private note  Add Note  add a bookmark about this section  Add Bookmark    
Top

[0672324342/app02lev1sec14]

 
 


JavaT APIs for XML Kick Start
JAX: Java APIs for XML Kick Start
ISBN: 0672324342
EAN: 2147483647
Year: 2002
Pages: 133

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