XML 5
XML 5
3] Write an XML document with an external DTD having TV schedule as the root
element and its channel as child element. Banner and day (one or more) are child
elements of channel. Day has date and program slot as its child elements. A program
slot contains time, title and description (optional). Each element has at least one
attribute attached with it.
4] Write the DTD and an XML document for the following situation:
Data consists of a set of names and addresses. Each name has a surname optional
middle name and a first name. Address contains either a house number and street
name or the name of the village. An address must also contain Post office name and
Pin code. Address may contain a number of telephone numbers.
SOLUTION
Basically a DTD defines the XML document structure with a list of legal elements and attributes.
A DTD can be declared inline inside an XML document, or as an external reference:-
This is the same XML document as above, but with an external DTD:
<? xml version="1.0"?>
<!DOCTYPE note SYSTEM "Note.dtd">
<note>
<to>Ram</to>
<from>Shyam</from>
<heading>Reminder</heading>
<body>On Sunday We will watch 3 Idiots!</body>
</note>
3) Writing the DTD within an XML document for the given situation:
<!ELEMENT TVSCHEDULE (CHANNEL+)>
<!ELEMENT CHANNEL (BANNER. DAY+)>
<!ELEMENT BANNER (#PCDATA)>
<!ELEMENT DAY ((DATE, HOLIDAY) | (DATE, PROGRAMSLOT+))+>
<!ELEMENT HOLIDAY (#PCDATA)>
<!ELEMENT DATE (#PCDATA)>
<!ELEMENT PROGRAMSLOT (TIME, TITLE, DESCRIPTION?)>
<!ELEMENT TIME (#PCDATA)>
<!ELEMENT TITLE (#PCDATA)>
<!ELEMENT DESCRIPTION (#PCDATA)>
[N.B. There will not be any statement like <!DOCTYPE …….> in an External DTD]
4) Writing the DTD within an XML document for the given situation:
<?xml version="1.0"?>
<!DOCTYPE document [
<!ELEMENT document (data+)>
<!ELEMENT data (name, address)>
<!ELEMENT name (surname, middle-name?, first-name)>
<!ELEMENT address (post-off, pin, ((house-no, street)|village), tel+)>
<!ELEMENT surname (#PCDATA)>
<!ELEMENT middle-name (#PCDATA)>
<!ELEMENT first-name (#PCDATA)>
<!ELEMENT post-off (#PCDATA)>
<!ELEMENT pin (#PCDATA)>
<!ELEMENT house-no (#PCDATA)>
<!ELEMENT street (#PCDATA)>
<!ELEMENT village (#PCDATA)>
<!ELEMENT tel (#PCDATA)>
]>
<document>
<data>
<name>
<surname>Das</surname>
<middle-name>Kumar</middle-name>
<first-name>Ram</first-name>
</name>
<address>
<post-off>Dum Dum</post-off>
<pin>700030</pin>
<house-no>453</house-no>
<street>Purba Snithi</street>
<tel>25501923</tel>
</address>
</data>
<data>
<name>
<surname>Sarkar</surname>
<first-name>Shyam</first-name>
</name>
<address>
<post-off>Chinsurah</post-off>
<pin>721121</pin>
<village>Adisaptagram</village>
<tel>26501923</tel>
<tel>26601923</tel>
</address>
</data>
</document>
END