XML : eXtensible Markup Language :: Document Type Definition [ DTD]
What is DTD? |
- Document Type Definition (DTD) helps to defines the building blocks of an XML document.
- It is used to define the document structure with a list of legal elements and attributes.
- A DTD can be embedded in same XML document, or it can be mentioned as an external file and should have extension ( .dtd ).
Syntax :
<!DOCTYPE root-element [element-declarations]>
Example
Why We should Use DTD:
- Used to verify own data
- It Verifies that the data received is valid
- Defines the legal building blocks of an XML document
- Defines the document structure with a list of legal elements.
Type of DTD. |
DTD can be use by two different ways
- Internal DTD
- External DTD
Internal DTD Declaration
If the DTD is declared inside the XML file,
Syntax
<!DOCTYPE dtd-name
[
<!ELEMENT element-name (element-content type)gt;
<!ATTLIST element-name attribute-name attribute-type default-value>
]>
Example : Internal DTD
External DTD Declaration
If the DTD is declared in an external file.
Syntax:
<!DOCTYPE root-element SYSTEM "filename">
Example : External DTD
Defining Elements |
In XML, an element is a logical component of the document.
An attribute, represents the characteristics of an element.
Example
HTML elements are “body” and “title”.
Empty Element
<!ELEMENT element-name (EMPTY)>
Element with Data
<!ELEMENT element-name (#CDATA)>
<!ELEMENT element-name (#PCDATA)>
Element with Child element
<!ELEMENT element-name (child-element-name)>
Element with Child element minimum occurrence of same element
<!ELEMENT element-name (child-name+)>
Element with Child element minimum 0 or one occurrence of same element
<!ELEMENT element-name (child-name?)>
Element with Child element minimum 0 or more occurrence of same element
<!ELEMENT element-name (child-name*)>
Defining Attributes |
An attribute, represents the characteristics of an element.
Example
HTML Element img having src as attribute . eg. <img src=”world.jpg” />
Syntax
<!ATTLIST element-name attribute-name CDATA "default-value">
Enumerated Attribute Value
<!ATTLIST element-name attribute-name (eval|eval|..) default-value>
Fixed Attribute Value
<!ATTLIST element-name attribute-name attribute-type #FIXED "value">
Required Attribute Value
<!ATTLIST element-name attribute_name attribute-type #REQUIRED >
Implied Attribute Value
<!ATTLIST element-name attribute-name attribute-type #IMPLIED >
Example
<!ELEMENT rectangle EMPTY>
<!ATTLIST rectangle width CDATA "0">
Defining Entities |
Entity Declaration in DTD
Example
HTML entity like
The contents of the internal entities occur within the XML document.
Syntax
<!ENTITY entity-name "entity-value">
- External entities are those whose contents are outside the XML document.
- SYSTEM keyword is used to specify any entity external to the document
Syntax
<!ENTITY entity-name SYSTEM "URI/URL">
Recent Comments