0% found this document useful (0 votes)
9 views4 pages

XML 5

The document discusses XML related problems and their solutions. It provides examples of creating XML documents with internal and external DTDs to describe tables of employee data and TV schedules. It also provides an example of a DTD for a document containing names and addresses.

Uploaded by

177 Vivek Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views4 pages

XML 5

The document discusses XML related problems and their solutions. It provides examples of creating XML documents with internal and external DTDs to describe tables of employee data and TV schedules. It also provides an example of a DTD for a document containing names and addresses.

Uploaded by

177 Vivek Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

XML Related Problems

1] Prepare an XML document for the following table:


NAME EMP_ID SALARY
Mr. ABC E_001 Rs. 15,000
Mrs. XYZ E_002 Rs. 17,000

2] Create an XML document that describes the following:


Employee_I Name Department Date of Joining Current Salary
D
E001 Mr. X A 02/07/1978 Rs. 30,000.00
E003 Mrs.Y B 11/11/1998 Rs. 20,000.00
E006 Mr. Z C 25/03/2003 Rs. 15,000.00

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:-

[1] Internal DTD Declaration:


If the DTD is declared inside the XML file, it should be wrapped in a DOCTYPE definition with
the following syntax:
<!DOCTYPE root-element [element-declarations]>
Example XML document with an internal DTD:
<?xml version="1.0"?>
<!DOCTYPE note [
<!ELEMENT note (to,from,heading,body)>
<!ELEMENT to (#PCDATA)>
<!ELEMENT from (#PCDATA)>
<!ELEMENT heading (#PCDATA)>
<!ELEMENT body (#PCDATA)>
]>
<note>
<to>Ram</to>
<from>Shyam</from>
<heading>Reminder</heading>
<body>On Sunday We will watch 3 Idiots</body>
</note>
[2] External DTD Declaration:
If the DTD is declared in an external file, it should be wrapped in a DOCTYPE definition with
the following syntax:
<!DOCTYPE root-element SYSTEM "filename">

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>

And this is the file "Note.dtd" which contains the DTD:


<!ELEMENT note (to,from,heading,body)>
<!ELEMENT to (#PCDATA)>
<!ELEMENT from (#PCDATA)>
<!ELEMENT heading (#PCDATA)>
<!ELEMENT body (#PCDATA)>

1) XML document describing the given information:


<?xml version=”1.0” encoding=UTF-8”?>
<employees>
<employee>
<name>Mr. ABC</name>
<emp_id>E_001</emp_id>
<salary>15,000</salary>
</employee>
<employee>
<name>Mrs. XYZ</name>
<emp_id>E_002</emp_id>
<salary>17,000</salary>
</employee>
<employees>

2) XML document describing the given information:


<?xml version=”1.0”?>
<Employees>
<Employee>
<Employee-ID>E001</Employee-ID>
<Name>Mr. X</Name>
<Department>A</Department>
<Date-of-Joining>
<dd>02</dd>
<mm>02</mm>
<yyyy>1978</yyyy>
</Date-of-Joining>
<Salary>30000</Salary>
</Employee>
<Employee>
<Employee-ID>E003</Employee-ID>
<Name>Mrs. Y</Name>
<Department>B</Department>
<Date-of-Joining>
<dd>11</dd>
<mm>11</mm>
<yyyy>1998</yyyy>
</Date-of-Joining>
<Salary>20000</Salary>
</Employee>
<Employee>
<Employee-ID>E006</Employee-ID>
<Name>Mr. Z</Name>
<Department>C</Department>
<Date-of-Joining>
<dd>25</dd>
<mm>03</mm>
<yyyy>2003</yyyy>
</Date-of-Joining>
<Salary>15000</Salary>
<Employee>
</Employees>

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)>

<!ATTLIST TVSCHEDULE NAME CDATA #REQUIRED>


<!ATTLIST CHANNEL CHAN CDATA #REQUIRED>
<!ATTLIST PROGRAMSLOT VTR CDATA #IMPLIED>
<!ATTLIST TITLE RATING CDATA #IMPLIED>
<!ATTLIST TITLE LANGUAGE CDATA #IMPLIED>

[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

You might also like