What is valid XML document? Design DTD for address book XML document

Mumbai University > Information Technology > Sem 4 > Web Programming

Marks: 10M

Year: Dec 2015

1 Answer
  • An XML document with correct syntax is called "Well Formed".
  • An XML document validated against a DTD is both "Well Formed" and "Valid".
  • A "Valid" XML document is a "Well Formed" XML document, which also conforms to the rules of a DTD:

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE note SYSTEM "Note.dtd">
    <body>Don't forget!</body>

The DOCTYPE declaration, in the example above, is a reference to an external DTD file.

The purpose of a DTD is to define the structure of an XML document. It defines the structure with a list of legal elements:

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

The DTD above is interpreted like this:

  • !DOCTYPE note defines that the root element of the document is note
  • !ELEMENT note defines that the note element must contain the elements: "to, from, heading, body"
  • !ELEMENT to defines the to element to be of type "#PCDATA"
  • !ELEMENT from defines the from element to be of type "#PCDATA"
  • !ELEMENT heading defines the heading element to be of type "#PCDATA"
  • !ELEMENT body defines the body element to be of type "#PCDATA"

DTD for Address Book XML Document :

< ?xml version=’1.0′ encoding=’utf-8’? > 
< !– DTD for a AddressBook.xml — > 
< !DOCTYPE AddressBook [ 
< !ELEMENT AddressBook (Address+) > 
< !ELEMENT Address (Name, Street, City) > 
< !ELEMENT Name (#PCDATA) > 
< !ELEMENT Street (#PCDATA) > 
< !ELEMENT City (#PCDATA) > 
] > 
< AddressBook > 
< Address > 
< Name >Jeniffer< /Name > 
< Street >Wall Street < /Street > 
< City >New York< /City > 
< /Address > 
< /AddressBook >

The DTD for AddressBook.xml is contained in a file AddressBook.dtd AddressBook.xml contains only XML Data with a reference to the DTD file

< ?xml version="1.0" encoding="UTF-8"? > 
< !DOCTYPE AddressBook SYSTEM "file:/// c:/XML/AddressBook.dtd " > " > 
< AddressBook > 
< Address > 
< Name >Jeniffer< /Name > 
< Street >Wall Street< /Street > 
< City >New York< /City > 
< /Address > 
< /AddressBook >
Please log in to add an answer.