Recent Posts

Saturday, 31 December 2016

DTD Entities Tutorial

DTD - Entities
1. Entities as variables used to define shortcuts to common text.
2. Entity references are references to entities.
3. Entities can be declared internal.
4. Entities can be declared external
<!ENTITY entity-name "entity-value">
DTD Example
<!ENTITY writer "Jan Egil Refsnes. ">
<!ENTITY copyright "Copyright XML1O1">
XML Example
External Entity Declaration
<!ENTITY entity-name SYSTEM "URI/URL">
DTD Example
<!ENTITY writer SYSTEM "">
<!ENTITY copyright SYSTEM "">
XML example
How to associate a DTD with xml?
1. We need to specify DOCTYPE declaration after XML declaration, in order to associate a DTD with the XML document.
2. A DTD can be declared
   * Inside an XML document
   * External reference.

Internal DTD Declaration
     If the DTD is declared inside the XML file, it must be wrapped inside the <!DOCTYPE> definition.
<!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 heading (#PCDATA)>
<body>Don't forget me this weekend</body>
The DTD above is interpreted like this
!DOCTYPE note defines that the root element of this document is note
!ELEMENT note defines that the note element must contain four 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"

External DTD Declaration
     If the DTD is declared in an external file, the <!DOCTYPE> definition with following syntax
<!DOCTYPE root-element SYSTEM "filename and location">
     This is the same XML document as above, but with an external DTD.
<?xml version="1.0"?>
<!DOCTYPE note SYSTEM "note.dtd">
   <body>Don't forget me this weekend!</body>
And here is the file "note.dtd", which contains the DTD:
<!ELEMENT note (to,from,heading,body)>
<!ELEMENT heading (#PCDATA)>
Develop a DTD file for the following XML file?
   <body>hi, how are you</body>
<! ELEMENT message (to, from, body)>
<! ELEMENT from (#PCDATA)>
<! ELEMENT body (#PCDATA)>
     An XML document is divided into two parts.
1. Prolog
2. Body
* Root element is nothing but the body of the XML document.
* Prolog is that part of the XML document which comes before the root element.
* Generally prolog contains XML declaration and DOCTYPE declaration.

What do you know about SYSTEM keyword in DOCTYPE declaration?
1. SYSTEM keyword indicates that the specified DTD is a private one. Almost always we use this (SYSTEM) keyword only in DOCTYPE declaration.
2. Another keyword i.e. an alternative for SYSTEM is PUBLIC.

Limitations of DTD
1. Weaker data types
2. Constraints can't be effectively specified.
3. Doesn't follow XML syntax, and hence not productive.

No comments:

Post a Comment