Presentation is loading. Please wait.

Presentation is loading. Please wait.

Document Type Definition (DTD)

Similar presentations


Presentation on theme: "Document Type Definition (DTD)"— Presentation transcript:

1 Document Type Definition (DTD)
IST 421 Spring 2004 Lecture 6

2 DTD Add rules to an XML document that enforce structure
Document Type Definition(DTD) XML Schema

3 DTD Validates a document against its model, i.e. declares what is legal Define the elements your document can contain Define the order in which elements appear Require that certain elements appear Define the allowed number of occurrences of a given element Define the type of data an element can contain Define child elements for a given element Define the attributes for each of your elements Assign constraints to the attribute values

4 DTD XML documents do not have to include a DTD
DTD becomes a way to validate a document and guarantees consistency DTD is important when sharing an XML document with other programs A document is valid if it conforms to a DTD An XML document may not be valid and yet be well-formed XML code

5 Invoice without Rules <?xml version=“1.0”?> <invoice>
<merchant name>Giant Foods</merchant name> <flower_name>Tulips</flower_name> <ingredient_list> <ingredient amount=“1cup”>Milk</ingredient> <ingredient amount=“3 tsp”>Butter</ingredient> <ingredient amount=“2 tsp”>Salt</ingredient> <ingredient amount=“3 cloves”>Garlic</ingredient> </ingredient_list> </invoice>

6 Invoice with Rules <?xml version=“1.0”?> <invoice>
<merchant_name>Giant Foods</merchant_name> <merchant_address> <street>123 Any Street</street> <city>Harrisburg</city> <state>PA</state> </merchant_address> <sales_date> </sales_date> <items_purchased> <item price=“2.35” quantity=“1”>Milk</item> <item price=“0.99” quantity=“1”>Eggs 12 count</item> <item price=“2.65” quantity=“1”>Tropicana Orange Juice</item> </items_purchased> </invoice>

7 DTD There are 2 kinds of DTD declarations:
Internal DTD – provided as part of the document External DTD – an external file The syntax and rules for defining the 2 are the same

8 Element Declarations If you use a DTD with your XML document, DTD must declare all elements used Syntax for declaring an element: <!ELEMENT element_name (content model)>

9 Element Declarations <?xml version=“1.0”? standalone=“yes”?>
<!DOCTYPE merchant_name [ <!ELEMENT merchant_name (#PCDATA)> ]> <merchant_name>Giant Foods</merchant_name>

10 Content Models for Elements
Text <!ELEMENT item (#PCDATA)> Supports text or character data Elements <!ELEMENT item (element_name)> Supports content that is another element Mixed content <!ELEMENT item (#PCDATA, element_name)> Supports both text and other elements #PCDATA must be first in declaration

11 Content Models for Elements
Empty <!ELEMENT item (EMPTY)> Supports an element that has no content Any <!ELEMENT item (ANY)> May contain text or elements PCDATA stands for parsed character data Data type is not yet known

12 <?xml version=“1.0”?>
<invoice> <merchant_name>Giant Foods</merchant_name> <merchant_address> <street>123 Any Street</street> <city>Harrisburg</city> <state>PA</state> </merchant_address> <sales_date> </sales_date> <items_purchased> <item price=“2.35” quantity=“1”>Milk</item> <item price=“0.99” quantity=“1”>Eggs 12 count</item> <item price=“2.65” quantity=“1”>Tropicana Orange Juice</item> </items_purchased> </invoice>

13 Internal DTD <?xml version=“1.0”?> <!DOCTYPE invoice[
<!ELEMENT invoice (merchant_name, merchant_address, street, city, state, sales_date, Items_purchased)> <ELEMENT merchant_name (#PCDATA)> <ELEMENT merchant_address (#PCDATA)> <ELEMENT street (#PCDATA)> <ELEMENT city (#PCDATA)> <ELEMENT state (#PCDATA)> <ELEMENT sales_date (#PCDATA)> <ELEMENT items_purchased (item)> <ELEMENT item (#PCDATA)> <!ATTLIST item price NMTOKEN #REQUIRED quantity NMTOKEN #REQUIRED> ]>

14 DTD Guidelines Special symbols to indicate how many times an element may appear. An * indicates a unit may appear as many times as needed or not at all An + indicates a unit must appear at least once and as many times as needed An ? Indicates the unit can appear only once An , indicates the elements must appear in the order specified

15 Attributes Syntax for a DTD attribute declaration
<!ATTLIST element_name attribute_name data_type default_value attribute_name data_type default_value > Can be located anywhere in the DTD but it is good practice to keep it close to the corresponding element

16 Attribute Data Types CDATA ID Stands for character/string data
Contains any combination of characters except “<“ or “&” Is simple and easy to use ID Defined to have a value that is unique, like a key Must start with a letter

17 Attribute Data Types IDREF NMTOKEN or NMTOKENS
Define an attribute that refers to one of the ID attributes NMTOKEN or NMTOKENS Data typing NMTOKEN type attributes may not contain any white space NMTOKENS type attributes may contain white space May consist of letters, numbers, hyphens, periods, underscores, and colons.

18 Attribute Default Values
#REQUIRED: attribute must contain some value #IMPLIED: attribute has no default value and may be omitted #FIXED fixedvalue: attribute must always be set to the value, fixedvalue Default: merely type a default value instead of the above

19 DTD Comments Comments may be written in the DTD in the same fashion as for an XML document <!-- This is a comment for a DTD -->

20 External DTD’s Make sure the standalone value in the XML declaration is set to “no” DTD declaration must tell the parser where to find the DTD file <!DOCTYPE invoice SYSTEM “invoice.dtd”> <!DOCTYPE invoice SYSTEM “

21 XML Schemas May 2001, W3C released XML Schema Language recommendation
Covers structure Covers data types Alternative to DTD’s More powerful method to describe and set constraints on XML components

22 XML Presentation

23 Separating Content from Style
Want to display the XML information Web browser Print Display of a handheld computer or Web-enabled phone Portable Document Format (PDF) Using XML to describe data makes it easy to create multiple presentation layouts for the same data

24 Cascading Style Sheets (CSS)
CSS style sheets contain rules and declarations that tell how to display certain elements Style information is in an external file with a .css extension Style Sheet Declaration <?xml-stylesheet type=“text/css” href=“invoices.css”?>

25 Cascading Style Sheets (CSS)
CSS have 2 parts: Element selector separated by comas Set of properties declarations separated by a semicolon address, city { font-size: 12pt; font-family: arial }

26 Cascading Style Sheets (CSS)
Comment style is different than DTD’s /* This is a comment */

27 CSS Properties Major property categories Font properties
Text properties Color properties Border properties Display properties

28 Assignment Create a DTD for your XML document from last class – the camping gear list and run it against a validating parser.


Download ppt "Document Type Definition (DTD)"

Similar presentations


Ads by Google