
DTD: Document Type Definition. It is a document or file that describes the structure of a Web page written in XML. The description of the structure and the rules a SGML or XML document must satisfy. The DTD comprises the formal declaration of the elements that make up a document, their mutual coherence and meaning. DTD is Document Type Definition. You can store a DTD at the beginning of a document or externally in a separate file. XML file + DTD file->Parser-> Yes or No DTD states what tags and attributes are used to describe content in an SGML, XML or HTML document, where each tag is allowed, and which tags can appear within other tags. For example, in a Document Type Definition one could say that LIST tags can contain ITEM tags, but ITEM tags cannot contain LIST tags. If an XML document has an associated document type definition and if the document complies with the constraints expressed in it, it is valid XML document. The document type definition must appear before the first element in the document. The name following the word DOCTYPE in the document type definition must match the name of root element. A Document Type Definition provides applications with advance notice of what names and structures can be used in a particular document type. Why we need a DTD XML is a language specification. Based on this specification, individuals and organizations develop their own markup languages, which they then use to communicate information with. When this information is transferred from source to destination, the destination: * Needs to know how the document is structured and * Needs to check if the content is indeed compliant with the structure The Document Type Definition also known as DTD holds information about the structure of an XML document. In this chapter we will understand the important aspects of DTDs. Why Use a DTD? •A single DTD ensures a common format for each XML document that references it. •An application can use a standard DTD to verify that data that it receives from the outside world is valid •A description of legal, valid data further contributes to the interoperability and efficiency of using XML DTDs let you say: •What element types can occur and where •What attributes each element type can have •What notations are in use •What external entities can be referenced DTD can be declared inline in the XML document, or as an external reference. Two types of document type declaration • Internal DOCTYPE declaration • External DOCTYPE declaration Internal DOCTYPE declaration If the DTD is included in an XML source file, it should be wrapped in a DOCTYPE definition. After the line <?xml version="1.0"?> you must type Syntax: <!DOCTYPE root-element [element-declarations]> Example: <?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>Sam</to> <from>Ros</from> <heading>Reminder</heading> <body>Don't forget me this weekend</body> </note> External DOCTYPE declaration If the DTD is external to the XML source file, it should be wrapped in a DOCTYPE definition. Syntax: <!DOCTYPE root-element SYSTEM "filename"> Example:This is the same XML document as above, but with an external DTD. <?xml version="1.0"?> <!DOCTYPE note SYSTEM "note.dtd"> <note> <to>Sam</to> <from>Ros</from> <heading>Reminder</heading> <body>Don't forget me this weekend!</body> </note> This is a copy of the file "note.dtd" containing the DTD <!ELEMENT note (to,from,heading,body)> <!ELEMENT to (#PCDATA)> <!ELEMENT from (#PCDATA)> <!ELEMENT heading (#PCDATA)> <!ELEMENT body (#PCDATA)> Note: SYSTEM in the DTD declaration can be replaced by PUBLIC if the DTD is available via the Internet. You would then need to have a public name for the DTD in the file. For example, the W3Group uses DTDs for the various markup languages they recommend. 1) What do you mean by DTD? 2) Receiver can verify the content and structure of received XML file with the help of DTD file. Comment on this statement 3) Explain 2 types of DTDs. External doctype and internal doctype DTD Elements Declaring an Element With an element declaration XML elements are declared in the DTD. An element declaration has the following syntax: <!ELEMENT element-name category> or <!ELEMENT element-name (element-content)> <!ELEMENT brand (#PCDATA)> Example of a DTD (author2.dtd) <!ELEMENT note (body)> <!ELEMENT body (#PCDATA)> Example 1 of XML using this DTD Example of XML not obeying this DTD <?xml version="1.0" standalone="no"?> <?xml version="1.0" standalone="no"?> <!DOCTYPE note SYSTEM “author2.dtd"> <!DOCTYPE note SYSTEM “author2.dtd"> <note> <note></note> <body>example</body> </note> Empty Elements Empty elements are declared with the category keyword EMPTY: <!ELEMENT element-name EMPTY> example: <!ELEMENT br EMPTY> XML example: <br /> Example of a DTD (author3.dtd) <!ELEMENT note (body)> <!ELEMENT body EMPTY> Example 1 of XML using this DTD Example of XML not obeying this DTD <?xml version="1.0" standalone="no"?> <?xml version="1.0" standalone="no"?> <!DOCTYPE note SYSTEM “author3.dtd"> <!DOCTYPE note SYSTEM “author3.dtd"> <note> <note> <body/> <body>example</body> </note> </note> Elements with only character data Elements with only character data are declared with #PCDATA inside parentheses: <!ELEMENT element-name (#PCDATA)> example: <!ELEMENT from (#PCDATA)> The description (#PCDATA) stands for parsed character data. It's the tag that is shown and also will be parsed (interpreted) by the program that reads the XML document. You can also define (#CDATA), this stands for character data. CDATA will not be parsed. Elements with any contents Elements declared with the category keyword ANY, can contain any combination of parsable data: <!ELEMENT element-name ANY> example: <!ELEMENT note ANY> “|” means either-or ”,” means succession. EMPTY (without parenthesis) means no contained data. Example of a DTD (author1.dtd) <!ELEMENT author (givenname,surname)> <!ELEMENT givenname (#PCDATA)> <!ELEMENT surname (#PCDATA)> Example 1 of XML using this DTD Example of XML not obeying this DTD <?xml version="1.0" standalone="no"?> <?xml version="1.0" standalone="no"?> <!DOCTYPE author SYSTEM “author1.dtd"> <!DOCTYPE author SYSTEM “author1.dtd"> <author> <author> <givenname>Margaret</givenname> <givenname>Margaret</givenname> <surname>York</surname> </author> </author> Example of a DTD (author.dtd) <!ELEMENT author (givenname|surname)> <!ELEMENT givenname (#PCDATA)> <!ELEMENT surname (#PCDATA)> Example of XML obeying this DTD Example of XML not obeying this DTD <?xml version="1.0" standalone="no"?> <?xml version="1.0" standalone="no"?> <!DOCTYPE author SYSTEM “author.dtd"> <!DOCTYPE author SYSTEM “author.dtd"> <author> <author> <givenname>Margaret</givenname> <givenname>Margaret</givenname> </author> <surname>York</surname> </author> Number of sub elements If you use <!ELEMENT car (brand, type) >, the sub elements brand and type can occur once inside the element car. To change the number of possible occurrences the following indications can be used: If an element name in DTD is followed by • the star [*] - This element can occur zero, once or several times. • the plus [+] - This element can occur once or several times. • the question mark [?] - This element can occur zero or one times. The indications are used behind the sub element name. For instance: <!ELEMENT animal (color+) > (a*) means that a is repeated 0, 1 or more times. Declaring minimum one occurrence of the same element <!ELEMENT element-name (child-name+)> example <!ELEMENT note (message+)> The + sign in the example above declares that the child element message must occur one or more times inside the note element. Declaring zero or more occurrences of the same element <!ELEMENT element-name (child-name*)> example <!ELEMENT note (message*)> The * sign in the example above declares that the child element message can occur zero or more times inside the note element. Declaring zero or one occurrences of the same element <!ELEMENT element-name (child-name?)> example <!ELEMENT note (message?)> The ? sign in the example above declares that the child element message can occur zero or one times inside the note element. Example of a DTD (author4.dtd) <!ELEMENT family (father,mother,child*)> <!ELEMENT father (#PCDATA)> <!ELEMENT mother (#PCDATA)> <!ELEMENT child (#PCDATA)> Example 1 of XML using this DTD Example 1 of XML not satisfies this DTD <?xml version="1.0"?> <?xml version="1.0"?> <!DOCTYPE family SYSTEM "author4.dtd"> <!DOCTYPE family SYSTEM "author4.dtd"> <family> <family> <father>John</father> <mother>Margaret</mother> <mother>Margaret</mother> <child>Eve</child> <child>Eve</child> <child>Peter</child> <child>Peter</child> </family> </family> (a+) means that a is repeated 1 or more times. Example of a DTD (author5.dtd) <!ELEMENT child-family (father,mother,child+)> <!ELEMENT father (#PCDATA)> <!ELEMENT mother (#PCDATA)> <!ELEMENT child (#PCDATA)> Example 1 of XML using this DTD Example 1 of XML using this DTD <?xml version="1.0" ?> <?xml version="1.0" ?> <!DOCTYPE child-family SYSTEM "author5.dtd"> <!DOCTYPE child-family SYSTEM "author5.dtd"> <child-family> <child-family> <father>John</father> <father>John</father> <mother>Margaret</mother> <mother>Margaret</mother> <child>Eve</child> </child-family> <child>Peter</child> </child-family> (a?) means that the element a is repeated 0 or 1 times. Example of a DTD (d6.dtd) <!ELEMENT basic-family (father?,mother?,child*)> <!ELEMENT father (#PCDATA)> <!ELEMENT mother (#PCDATA)>
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages31 Page
-
File Size-