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.
<!ELEMENT name allowable_contents> |
Example: |
---|
<!ELEMENT foo (#PCDATA)> |
<!ELEMENT img EMPTY> |
Rules:
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. |
Children element types are declared using parentheses in the parent element type's declaration.
<!ELEMENT parent_name (child_name)> <!ELEMENT child_name allowable_contents> |
Example: |
---|
<?xml version="1.0"?> <!DOCTYPE student [ <!--'student' must have one child element type 'id'--> <!ELEMENT student (id)> <!--'id' may only contain text that is not markup in its content--> <!ELEMENT id (#PCDATA)> ]> <student> <id>9216735</id> </student> |
Rules:
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 [ <!--'student' must contain three child elements in the order listed--> <!ELEMENT student (id,surname,firstname)> <!--the elements listed below may only contain text that is not markup--> <!ELEMENT id (#PCDATA)> <!ELEMENT firstname (#PCDATA)> <!ELEMENT surname (#PCDATA)> ]> <student> <id>9216735</id> <surname>Smith</surname> <firstname>Jo</firstname> </student> |
Rules:
Optional children are declared using the (?) operator. Optional means zero or one times.
<!ELEMENT parent_name (child_name?)> <!ELEMENT child's_name allowable_contents> |
Example: |
---|
<?xml version="1.0"?> <!DOCTYPE student [ <!--'student' can have zero or one child element of type 'dob'--> <!ELEMENT student (dob?)> <!--'dob' may only contain text that is not markup in its content--> <!ELEMENT dob (#PCDATA)> ]> <student> <dob>19.06.74</dob> </student> |
Rules:
Zero or more children are declared using the (*) operator.
<!ELEMENT parent_name (child_name*)> <!ELEMENT child_name allowable_contents> |
Example: |
---|
<?xml version="1.0"?> <!DOCTYPE student [ <!--'student' can have zero or more child elements of type 'subject'--> <!ELEMENT student (subject*)> <!--'subject' may only contain text that is not markup in its content--> <!ELEMENT subject (#PCDATA)> ]> <student> <subject>Mathematics</subject> <subject>Physics</subject> <subject>Chemistry</subject> </student> |
Rules:
One or more children are declared using the (+) operator.
<!ELEMENT parent_name (child_name+)> <!ELEMENT child_name allowable_contents> |
Example: |
---|
<?xml version="1.0"?> <!DOCTYPE student [ <!--'student' must have at least one child element of type 'subject'--> <!ELEMENT student (subject+)> <!--'subject' may only contain text that is not markup in its content--> <!ELEMENT subject (#PCDATA)> ]> <student> <subject>Mathematics</subject> </student> |
Rules:
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 [ <!--'student' must have 'id' or 'surname' as its child element--> <!ELEMENT student (id|surname)> <!--the elements listed below may only contain text that is not markup--> <!ELEMENT id (#PCDATA)> ]> <student> <id>9216735</id> </student> |
Rules:
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 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:
Note: