Element-Content Model:: Example

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 7

Element-content model:

1) Empty Elements: keyword “EMPTY”:


<!ELEMENT element-name (EMPTY)> example: <!ELEMENT img (EMPTY)>
2) Text-only: Elements with text are declared:
<!ELEMENT element-name (#PCDATA)> example: < ! ELEMENT name ( # PCDATA) >
3) Any: keyword “ANY” declares an element with any content:<!ELEMENT element-name
(ANY)>
4) Complex: a regular expression over other elements.
Example: < ! ELEMENT company ( (person | product)*) >
5) Mixed content: Example: <!ELEMENT note (to+,from,header,message*,#PCDATA)>
This example 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.
Note: The elements in the regular expression must be defined in the DTD
DTD - Attributes
In DTD, XML element attributes are declared with an ATTLIST declaration. Attribute
declaration has the following syntax:
Attributes: Attributes provide extra information about elements. Attributes are placed inside the
starting tag of an element.
Example: <img src="computer.gif" />

<!ATTLIST element-name
attribute-name
Example:
<! ELEMENT person (ssn, name, attribute-type
office, phone?) > default-
value>
<! ATTLIS person age CDATA #REQUIRED) >
The attribute-type can have the following values:

CDADA = string
ID = key
IDREF = foreign key
IDREFS = foreign keys
separated by space
(val | val | …) = enumeration
NMTOKEN = must be a valid XML
name
NMTOKENS = multiple valid XML
The default-value can have the following values:
names
ENTITY = entity
ENTITIES = a list of entities
NOTATION = a name of a notation
xml: = the value is
predefined

#DEFAULT value The attribute has


a default
value
#REQUIRED The attribute
value must be
Attribute declaration examples

included in the
element
Example 1:
DTD example:

IMPLIED The attribute is


<!ELEMENT square EMPTY>
<!ATTLIST square width CDATA "0">
optional
XML example:
<square width="100"></square>

FIXED value
or The only value
<square width="100”/>
allowed
Example 2:
DTD example:
< !DOCTYPE family [
< !ELEMENT family (person)* >
< !ELEMENT person ( name ) >
< !ELEMENT name (#PCDATA) >
< !ATTLIST person id ID #REQUIRED
mother IDREF #IMPLIED
father IDREF #IMPLIED
children IDREFS #IMPLIED
]>
XML example:
<family>
<person id = “jane” mother = “mary” father = “john”>
<name> Jane Doe </name>
</person>
<person id = “john” children = “jane jack”>
<name> John Doe </name>
</person>
<person id = “mary” children = “jane jack”>
<name> Mary Smith </name>
</person>
<person id = “jack” mother = “smith” father = “john”>
<name> Jack Smith </name>
</person>
</family>
DTD – Entities
Entities
• Entities as variables used to define shortcuts to common text.
• Entity references are references to entities.
• Entities can be declared internal.
• Entities can be declared external.
Internal Entity Declaration
Syntax:
<!ENTITY entity-name "entity-
value">

DTD Example:
<!ENTITY writer "Jan Egil Refsnes.">
<!ENTITY copyright "Copyright
XML101.">
External Entity Declaration

Syntax:
<!ENTITY
XML entity-name SYSTEM
example:
"URL">
<author>&writer;&copyright;</aut
DTD Example:
hor> writer SYSTEM
<!ENTITY
"http://www.xml101.com/entities
/entities.xml">
<!ENTITY copyright SYSTEM
"http://www.xml101.com/entities
Entities: Entities as variables used to define common text. Entity references are references to

/entities.dtd">
entities.

XML example:
<author>&writer;&copyright;</aut
hor>
The following entities are predefined in XML:
Entity References | Character
------------------------------------
&lt; <
&gt; >
&amp; &
&quot; "
&apos; '
Entities are expanded when a XML document is parsed by an XML parser.

DTD Validation
How to test for DTD errors while loading XML document?
Since DTD is the grammar for XML, XML is a parse tree of its DTD. Then we can use a XML
parser to check if the XML is valid.

You might also like