Presentation is loading. Please wait.

Presentation is loading. Please wait.

Defining XML The Document Type Definition. Document Type Definition text syntax for defining –elements of XML –attributes (and possibly default values)

Similar presentations

Presentation on theme: "Defining XML The Document Type Definition. Document Type Definition text syntax for defining –elements of XML –attributes (and possibly default values)"— Presentation transcript:

1 Defining XML The Document Type Definition

2 Document Type Definition text syntax for defining –elements of XML –attributes (and possibly default values) –structure implies that an external definition exists and may be required to properly understand the content

3 Why do we need DTDs? Define classes of xml documents –For particular applications –Agreement on data and structure Validate xml data –DTD is used to check structure Document an xml class –DTD provides complete information about an xml class

4 linking an XML file to a DTD a document type declaration is added to the xml XML file DTD DOCTYPE link myDTD.dtdmessage.xml

5 What Is a DTD? Defines a type of xml document –What elements are allowed? –What attributes do they have? –How can they be structured? DTD is in text format Usually external to the xml data –Linked by a document type declaration May be included in the xml data file

6 Element type declarations the element definition element name of the element being defined content that the element can have #PCDATA = parsed character data

7 One line of text, stored in messageML.dtd Welcome to XML! Example of a message document conforming to this DTD Example

8 Internal DTD Example <!DOCTYPE message [ ]> Welcome to XML!

9 Element declarations define the content of elements Content can be text or other elements Content defines structure –How are the elements nested? –How many elements can be included? –What order do elements come in? Defining structure

10 a classroom contains exactly one teacher followed by exactly one student a dessert contains either one iceCream or one pastry, but not both an album contains one or more tracks

11 occurrence indicators Plus sign (+) Asterisk (*) Question mark (?) Element will appear 0 to 1 times Element will appear 0 to many times Element will appear 1 to many times

12 A Simple Document Type Definition

13 DTD Example 1 <!ELEMENT class (number, (instructor ¦ assistant+), (credit ¦ nocredit) )> a class must contain a number followed by either an instructor or one or more assistants followed by either a credit or a nocredit CM4003 Stewart Massie 15

14 DTD Example 2 <!ELEMENT donutBox (jam?, lemon*, ((cream | sugar)+ | iced)) a donutBox contains 0 or 1 jam followed by 0 to many lemon followed by either one to many cream or sugar or one iced raspberry sour half-sour chocolate</iced pink

15 DTD Example 3 <!ELEMENT farm (farmer+, (dog* | cat?), pig*, (goat | cow)?, (chicken+ | duck*) )> Farmer Maggot Tiddles Donald

16 DTD Example 4 mixed content (narrative XML) A element may contain any combination of, or elements interspersed with parsed character data. Todays date is and Stewart Massie, a lecturer is delivering a scintillating XML lecture.

17 Defining attributes attributes assigned to elements using the instruction ATTLIST defines –Which element the attribute belongs to –The name of the attribute –The values the attribute can take –Possible default values –Whether the attribute MUST be present or not

18 Attribute values In HTML all attributes are text DTDs support 10 attribute types Most common are: –CDATA (literal text) –ID (unique identifier) –NMTOKEN (no whitespace) –Enumeration (of all possible values)

19 Conditions on attributes #REQUIRED –the attribute must be given a value in the XML #IMPLIED –the attribute may be omitted from the XML #FIXED –the value of the attribute is fixed and defined in the DTD literal –a default value is supplied literally in the DTD

20 Example attribute declarations <pig weight = 1000kg id_code = pig017> Porky

21 entities used to represent text that would cause parsing problems < represents < & represents & > represents > " represents &apos; represents

22 defining entities now &super; is replaced in the XML (or in attribute values) by supercallifragilisticexpialidocious

23 CDATA or PCDATA? PCDATA –Parsed Character DATA –will be parsed for entities CDATA –Character DATA –Will NOT be parsed –CDATA sections are sometimes included in xml to include literal sections of code

24 Writing a CDATA section <!CDATA[ Hi! Im a CDATA section! I can include anything that would normally upset the parser: <> hahahahahahaha!!! The only thing I have to avoid is a double square closing bracket, which means the CDATA has ended. ]]>

25 Validation of xml Validation means checking that an xml document conforms to its DTD Adds security to automatic processing Allows free machine-machine exchange of xml Applied before manipulating xml –See XSLT, SAX, DOM later

26 Well-formed vs valid Well-formed xml –The data obeys the xml syntax rules Valid xml –The data is well-formed xml –The data has a DTD –The data conforms to the DTD xml data may be well-formed but invalid

27 xml parser types validating parser –checks XML is well-formed conforms to XML specification –checks XML is valid (has and matches a DTD) non-validating parser –only checks XML is well-formed –may pass invalid XML

Download ppt "Defining XML The Document Type Definition. Document Type Definition text syntax for defining –elements of XML –attributes (and possibly default values)"

Similar presentations

Ads by Google