‹‹ Previous
Next ››
DTD
- DTD is called Document Type Definition .
- DTD defines the structure of the XML file.
- DTD is used to define user tags that is going to be used in the XML file.
- DTD is also used to define attributes for user defined tags.
- Basically, DTD is to validate the XML file.
- Validation of XML file meaning is that to check that XML file is written according to DTD.
- User defined tag is also called element of the XML file.
- Synatx for defining DTD is
- <!DOCTYPE . . . >
- DTD starts with left angle bracket (<) and closes with right angle bracket (>).
- After left angle bracket, an exclamation mark (!) is used.
- After exclamation mark (!), the word DOCTYPE is written.
Defining the root element (or tag) in DTD
- Root tag of XML file is defined in DTD by writting root tag name after DOCTYPE.
- For example, to write a root tag facultydetail in DTD. Following is the DTD:
- <!DOCTYPE FacultyDetail >
- Further, to define other elements within the root element. Use following DTD:
- A Faculty element is mentioned in the root element FacultyDetail .
- Now suppose, you want three information (FacultyName, Subject and City) related to faculty in the Faculty element.
- Following is DTD code:
- <!DOCTYPE FacultyDetail [
- <!ELEMENT Faculty (FacultyName, Subject, City)>
- ]>
- Now, furhter each three elements is defined with its type of content.
- See the following DTD code:
- <!DOCTYPE FacultyDetail [
- <!ELEMENT Faculty (FacultyName, Subject, City)>
- <!ELEMENT FacultyName (#PCDATA)>
- <!ELEMENT Subject (#PCDATA)>
- <!ELEMENT City (#PCDATA)>
- ]>
- Here, #PCDATA is used to define type of the content of the element.
- #PCDATA defines that content of element is Parsed Character Data .
- Remember, DTD is closed using symbol ]> .
- Closing bracket ] is used to indicate end of the root element FacultyDetail .
- Right angle bracket > after closing bracket ] is used to indicate end of DOCTYPE .
Explanation of DTD example
- In the above DTD example, one root element FacultyDetail is defined.
- Within root element FacultyDetail , one element Faculty is defined.
- Within Faculty element, three elements FacultyName, Subject and City are defined.
- Then each three elements is futher defined with its type of content which is Parsed Character Data .
- Parsed Character Data is defined using the keyword #PCDATA .
Full XML program with corresponding DTD
Example: Write XML file with corresponding DTD for faculty information.
- The above XML program has an internal DTD that defines one root element and other elements of the XML file.
External DTD
- In case of external DTD, DTD is written in a separate file with file extension .dtd.
- Then .dtd file is included in the XML file.
See the following example.
Suppose name of separate DTD file is
fac.dtd as given below:
<!ELEMENT Faculty (FacultyName, Subject, City)>
<!ELEMENT FacultyName (#PCDATA)>
<!ELEMENT Subject (#PCDATA)>
<!ELEMENT City (#PCDATA)>
Now this file is included in the following XML file:
‹‹ Previous
Next ››