2. XML DTD
• An XML document may have an optional DTD, which defines the
document’s grammar.
Since the DTD defines the XML document’s grammar, we can use an
XML parser to check that if an XML document conforms to the grammar
defined by the DTD.
• The purpose of a DTD is to define the legal building blocks of an XML
document.
• Terminology for XML:
-- well-formed: if tags are correctly closed.
-- valid: if it has a DTD and conforms to it.
Validation is useful in data exchange.
3. • A DTD can be declared inside the XML document, or as an external
reference.
• 1) Internal DTD
This is a example of a simple XML document with an internal DTD:
The DTD a is interpreted like this:
!DOCTYPE tutorials defines that the root
element of this document is tutorials
!ELEMENT tutorials defines that the tutorials
element contains minimum one occurrence of
child element.
!ELEMENT tutorial defines that the tutorials
element must contain two element name,url.
!ELEMENT name defines the to element to be
of type "#PCDATA"
!ELEMENT url defines the from element to
be of type "#PCDATA”
4.
5. EXTERNAL DTD
• If the DTD is declared in an external file, the <!DOCTYPE> definition
must contain a reference to the DTD file:
• The keyword SYSTEM indicates that it's a private DTD
6.
7. DTD - XML Building Blocks
• The main building blocks of both XML and HTML documents are elements.
• Seen from a DTD point of view, all XML documents are made up by the
following building blocks:
• Elements
• Attributes
• Entities
• PCDATA
• CDATA
• Entities
• Some characters have a special meaning in XML, like the less than sign (<) that
defines the start of an XML tag.
• Most of you know the HTML entity: " ". This "no-breaking-space" entity
is used in HTML to insert an extra space in a document. Entities are expanded
when a document is parsed by an XML parser.
• The following entities are predefined in XML:
8. PCDATA and CDATA
• PCDATA
• PCDATA means parsed character data.
• Think of character data as the text found between the start tag and the end
tag of an XML element.
• PCDATA is text that WILL be parsed by a parser. The text will be
examined by the parser for entities and markup.
• However, parsed character data should not contain any &, <, or >
characters; these need to be represented by the & < and >
entities, respectively.
• CDATA means character data.
• CDATA is text that will NOT be parsed by a parser. Tags inside the text
will NOT be treated as markup and entities will not be expanded.
9. DTD ELEMENTS
• In a DTD, elements are declared with an ELEMENT declaration.
• Declaring Elements
• In a DTD, XML elements are declared with the following syntax:
• Empty Elements
• Empty elements are declared with the category keyword EMPTY:
10.
11. DTD - Attributes
• In a DTD, attributes are declared with an ATTLIST declaration.
• Declaring Attributes
• An attribute declaration has the following syntax:
• <!ATTLIST element-name attribute-name attribute-type attribute-value>
DTD example:
<!ATTLIST payment type CDATA "check">
XML example:
<payment type="check" />
12. The attribute-value can be one of the following:
DTD - Entities
1. Entities are used to define shortcuts to special characters.
2. Entities can be declared internal or external.
3. An Internal Entity Declaration
1. <!ENTITY entity-name "entity-value">