Advertise Here
Need an introduction to XML Document Type Definitions? Learn about the building blocks of DTD, how to define DTD elements in XML documents, how to define the legal attributes of XML elements, how to validate DTE, and how to test for DTD errors. - ?
Extensible Markup Language

Search XML Files

XML Files RSS Feed XML Files Updates

 


DTD - Elements

by Jan Egil Refsnes

back next

Declaring an Element

In the DTD, XML elements are declared with an element declaration. An element declaration has the following syntax:

<!ELEMENT element-name (element-content)>


Empty elements

Empty elements are declared with the keyword EMPTY inside the parentheses:

<!ELEMENT element-name (EMPTY)>

example:
<!ELEMENT img (EMPTY)>


Elements with data

Elements with data are declared with the data type inside parentheses:

<!ELEMENT element-name (#CDATA)>
or
<!ELEMENT element-name (#PCDATA)>
or
<!ELEMENT element-name (ANY)>
example:
<!ELEMENT note (#PCDATA)>

#CDATA means the element contains character data that is not supposed to be parsed by a parser.
#PCDATA means that the element contains data that IS going to be parsed by a parser.
The keyword ANY declares an element with any content.

If a #PCDATA section contains elements, these elements must also be declared.


Elements with children (sequences)

Elements with one or more children are defined with the name of the children elements inside the parentheses:

<!ELEMENT element-name (child-element-name)>
or
<!ELEMENT element-name (child-element-name,child-element-name,.....)>
example:
<!ELEMENT note (to,from,heading,body)>

When children are declared in a sequence separated by commas, the children must appear in the same sequence in the document. In a full declaration, the children must also be declared, and the children can also have children. The full declaration of the note document will be:
<!ELEMENT note (to,from,heading,body)>
<!ELEMENT to      (#CDATA)>
<!ELEMENT from    (#CDATA)>
<!ELEMENT heading (#CDATA)>
<!ELEMENT body    (#CDATA)>


Wrapping

If the DTD is to be included in your XML source file, it should be wrapped in a DOCTYPE definition with the following syntax:

<!DOCTYPE root-element [element-declarations]>
example: 
<?xml version="1.0"?>
<!DOCTYPE note [
  <!ELEMENT note (to,from,heading,body)>
  <!ELEMENT to      (#CDATA)>
  <!ELEMENT from    (#CDATA)>
  <!ELEMENT heading (#CDATA)>
  <!ELEMENT body    (#CDATA)>
]>
<note>
  <to>Tove</to>
  <from>Jani</from>
  <heading>Reminder</heading>
  <body>Don't forget me this weekend</body>
</note>


Declaring only one occurrence of the same element 

<!ELEMENT element-name (child-name)>
example
<!ELEMENT note (message)>

The example declaration above declares that the child element message can only occur one time inside the note element.


Declaring minimum one occurrence of the same element

<!ELEMENT element-name (child-name+)>
example
<!ELEMENT note (message+)>

The + sign in the example above declares that the child element message must occur one or more times inside the note element.


Declaring zero or more occurrences of the same element 

<!ELEMENT element-name (child-name*)>
example
<!ELEMENT note (message*)>

The * sign in the example above declares that the child element message can occur zero or more times inside the note element.


Declaring zero or one occurrences of the same element 

<!ELEMENT element-name (child-name?)>
example
<!ELEMENT note (message?)>

The ? sign in the example above declares that the child element message can occur zero or one times inside the note element.


Declaring mixed content

example
<!ELEMENT note (to+,from,header,message*,#PCDATA)>

The example above declares that the element note must contain at least one to child element, exactly one from child element, exactly one header, zero or more message, and some other parsed character data as well. Puh!


back next
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 DevX XML Content 
- W3C Has a New XML Standard: XProc
- The Key XForms Enhancements in Version 1.1
- Performing Hierarchical Restructuring Using ANSI SQL
- Taking XML Validation to the Next Level: Explore CAM's Expressive Power
- Taking XML Validation to the Next Level: Introducing CAM

Jump to : Top Of Page or HOME