ELEMENT Type Declaration
Element type declarations set the rules for the type and number of elements that may appear in an XML document, what elements may appear inside each other, and what order they must appear in.
| Example: |
<!ELEMENT foo (#PCDATA)>
|
<!ELEMENT img EMPTY>
|
Rules:
- All element types
used in an XML document must be declared in the Document Type Definition (DTD) using an Element Type Declaration .
- An element type cannot be declared more than once
.
- The name in the element type's end tag
must match the name in the element type's start tag . Element names are case sensitive.
- The keyword ELEMENT must be in upper case
.
Note:
The allowable contents of an element type is EMPTY, ANY, Mixed, or children element types.
| Allowable Contents: |
Definition: |
| EMPTY |
Refers to tags that are empty. For example, the empty IMG tag from HTML may be represented in either of the following ways: <IMG SRC="grommit.gif"/>, or <IMG SRC="grommit.gif"></IMG> . |
| ANY |
Refers to anything at all, as long as XML rules are followed. ANY is useful to use when you have yet to decide the allowable contents of the element. |
| children elements |
You can place any number of element types within another element type. These are called children elements, and the elements they are placed in are called parent elements. See declaring children below. |
| Mixed content |
Refers to a combination of (#PCDATA) and children elements. PCDATA stands for parsed character data, that is, text that is not markup. Therefore, an element that has the allowable content (#PCDATA) may not contain any children. See mixed content below. |
Declaring Children:
Children element types are declared using parentheses in the parent element type's declaration.
| Example: |
<?xml version="1.0"?>
<!DOCTYPE student [
<!ELEMENT student (id)>
<!ELEMENT id (#PCDATA)>
]>
<student>
<id>9216735</id>
</student>
|
Rules:
- The child element must be declared in a separate element type declaration
.
Declaring Multiple Children (Sequence):
Multiple children are declared using commas (,). Commas fix the sequence in which the children are allowed to appear in the XML document.
<!ELEMENT parent_name (child1_name,child2_name,child3_name)>
<!ELEMENT child1_name allowable_contents>
<!ELEMENT child2_name allowable_contents>
<!ELEMENT child3_name allowable_contents>
|
| Example: |
<?xml version="1.0"?>
<!DOCTYPE student [
<!ELEMENT student (id,surname,firstname)>
<!ELEMENT id (#PCDATA)>
<!ELEMENT firstname (#PCDATA)>
<!ELEMENT surname (#PCDATA)>
]>
<student>
<id>9216735</id>
<surname>Smith</surname>
<firstname>Jo</firstname>
</student>
|
Rules:
- All of the children elements must be declared in a separate element type declaration
.
Declaring Optional Children:
Optional children are declared using the (?) operator. Optional means zero or one times.
| Example: |
<?xml version="1.0"?>
<!DOCTYPE student [
<!ELEMENT student (dob?)>
<!ELEMENT dob (#PCDATA)>
]>
<student>
<dob>19.06.74</dob>
</student>
|
Rules:
- If the child element is used in the XML document it must be declared in a separate element type declaration
.
Declaring Zero or More Children:
Zero or more children are declared using the (*) operator.
| Example: |
<?xml version="1.0"?>
<!DOCTYPE student [
<!ELEMENT student (subject*)>
<!ELEMENT subject (#PCDATA)>
]>
<student>
<subject>Mathematics</subject>
<subject>Physics</subject>
<subject>Chemistry</subject>
</student>
|
Rules:
- If the child element is used in the XML document it must be declared in a separate element type declaration
.
Declaring One or More Children:
One or more children are declared using the (+) operator.
| Example: |
<?xml version="1.0"?>
<!DOCTYPE student [
<!ELEMENT student (subject+)>
<!ELEMENT subject (#PCDATA)>
]>
<student>
<subject>Mathematics</subject>
</student>
|
Rules:
- The child element must be declared in a separate element type declaration
.
Combinations of Children (Choice):
A choice between children element types is declared using the (|) operator.
<!ELEMENT parent_name (child1_name|child2_name)>
<!ELEMENT child1_name allowable_contents>
<!ELEMENT child2_name allowable_contents>
|
| Example: |
<?xml version="1.0"?>
<!DOCTYPE student [
<!ELEMENT student (id|surname)>
<!ELEMENT id (#PCDATA)>
]>
<student>
<id>9216735</id>
</student>
|
Rules:
- The child element used in the XML document must be declared in a separate element type declaration
.
Nesting ELEMENTs:
All of the following examples contain valid nested element declarations.
| Examples: |
<?xml version="1.0"?>
<!DOCTYPE student [
<!ELEMENT student (surname,firstname*,dob?,(origin|sex)?)>
<!ELEMENT surname (#PCDATA)>
<!ELEMENT firstname (#PCDATA)>
<!ELEMENT sex (#PCDATA)>
]>
<student>
<surname>Smith</surname>
<firstname>Jo</firstname>
<firstname>Sephine</firstname>
<sex>female</sex>
</student>
|
<?xml version="1.0"?>
<!DOCTYPE student [
<!ELEMENT student (surname,firstname)>
<!ELEMENT firstname (fullname,nickname)>
<!ELEMENT surname (#PCDATA)>
<!ELEMENT fullname (#PCDATA)>
<!ELEMENT nickname (#PCDATA)>
]>
<student>
<surname>Smith</surname>
<firstname>
<fullname>Josephine</fullname>
<nickname>Jo</nickname>
</firstname>
</student>
|
<?xml version="1.0"?>
<!DOCTYPE student [
<!ELEMENT student (sex|maritalstatus*)>
]>
<student>
</student>
|
<?xml version="1.0"?>
<!DOCTYPE student [
<!ELEMENT student ((sex,maritalstatus)*)>
]>
<student>
</student>
|
Mixed Content:
Mixed content is used to declare elements that contain a mixture of children elements and text (PCDATA ).
<!ELEMENT parent_name (#PCDATA|child1_name)*>
|
| Examples: |
<?xml version="1.0"?>
<!DOCTYPE student [
<!ELEMENT student (#PCDATA|id)*>
<!ELEMENT id (#PCDATA)>
]>
<student>
Here's a bit of text mixed up with the child element.
<id>9216735</id>
You can put text anywhere, before or after the child element.
You don't even have to include the 'id' element.
</student>
|
<?xml version="1.0"?>
<!DOCTYPE student [
<!ELEMENT student (#PCDATA)>
]>
<student>
</student>
|
<?xml version="1.0"?>
<!DOCTYPE student [
<!ELEMENT student (#PCDATA|id|surname|dob)*>
<!ELEMENT id (#PCDATA)>
<!ELEMENT surname (#PCDATA)>
]>
<student>
You can put text anywhere. You can also put the elements in any
order in the document.
<surname>Smith</surname>
And, you don't have to include all the elements listed in the
element declaration.
<id>9216735</id>
</student>
|
Rules:
- The same child element type
may only appear once in a mixed content declaration .
- If a child element is used in the XML document, it must be declared in a separate element type declaration
.
- The (#PCDATA) and children element declarations must be separated by the (|) operator
.
- (#PCDATA) must come first in the mixed content declaration
.
- The operator (*) must follow the mixed content declaration if children elements are included
.
Note:
- Non-markup text (PCDATA) can be placed anywhere (before, between or after children elements).
- The children elements can be placed in any order.
|