Wattle Software - producers of XMLwriter XML editor
 Home | Search | Site Map 
XMLwriter
 Screenshots
 Features
 About Latest Version
 Awards & Reviews
 User Comments
 Customers
Download
 Download XMLwriter
 Download Plug-ins
 Download Help Manual
 MSXML Updates
 Downloading FAQ
Buy
 Buy XMLwriter
 Pricing
 Upgrading
 Sales Support
 Sales FAQ
Support
 Sales Support
 Technical Support
 Submit a Bug Report
 Feedback & Requests
 Technical FAQ
Resources
 XML Links
 XML Training
 XMLwriter User Tools
 The XML Guide
 XML Book Samples
Wattle Software
 About Us
 Contact Details
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.

<!ELEMENT name allowable_contents>

Example:
<!ELEMENT foo (#PCDATA)>
<!ELEMENT img EMPTY>

Rules:

  • All element typesglossary used in an XML document must be declared in the Document Type Definition (DTD) using an Element Type Declarationvalidity constraint.
  • An element type cannot be declared more than oncevalidity constraint.
  • The name in the element type's end tagglossary must match the name in the element type's start tagglossarywell-formedness constraint. Element names are case sensitive.
  • The keyword ELEMENT must be in upper casewell-formedness constraint.

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>well-formedness constraint.
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.

<!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:

  • The child element must be declared in a separate element type declarationvalidity constraint.



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 [
  <!--'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:

  • All of the children elements must be declared in a separate element type declarationvalidity constraint.



Declaring Optional Children:

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:

  • If the child element is used in the XML document it must be declared in a separate element type declarationvalidity constraint.



Declaring Zero or More Children:

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:

  • If the child element is used in the XML document it must be declared in a separate element type declarationvalidity constraint.



Declaring One or More Children:

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:

  • The child element must be declared in a separate element type declarationvalidity constraint.



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 [
  <!--'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:

  • The child element used in the XML document must be declared in a separate element type declarationvalidity constraint.



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 (PCDATAglossary).

<!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 typeglossary may only appear once in a mixed content declarationvalidity constraint.
  • If a child element is used in the XML document, it must be declared in a separate element type declarationvalidity constraint.
  • The (#PCDATA) and children element declarations must be separated by the (|) operatorwell-formedness constraint.
  • (#PCDATA) must come first in the mixed content declarationwell-formedness constraint.
  • The operator (*) must follow the mixed content declaration if children elements are includedwell-formedness constraint.

Note:

  • Non-markup text (PCDATA) can be placed anywhere (before, between or after children elements).
  • The children elements can be placed in any order.


XML Guide TOC

XML Declaration
DOCTYPE Declaration & DTDs
ELEMENT Type Declaration
ATTLIST Declaration
ENTITY Declaration
NOTATION Declaration
CDATA Section
Processing Instruction
Conditional Section
Comments

Glossary

© Wattle Software 1998-2017. All rights reserved.