Presentation is loading. Please wait.

Presentation is loading. Please wait.

DTDs and Schemas © 24/01/2012University of Greenwich1 XML 2 DTDs, Schemas and Namespaces Kevin McManus Recycled from Gill Windall’s notes.

Similar presentations


Presentation on theme: "DTDs and Schemas © 24/01/2012University of Greenwich1 XML 2 DTDs, Schemas and Namespaces Kevin McManus Recycled from Gill Windall’s notes."— Presentation transcript:

1 DTDs and Schemas © 24/01/2012University of Greenwich1 XML 2 DTDs, Schemas and Namespaces Kevin McManus Recycled from Gill Windall’s notes

2 DTDs and Schemas © 24/01/2012University of Greenwich2 DTDs and Schemas Document Type Definitions (DTD) and XML Schemas (XSD) are alternative ways of defining an XML language They contain rules that specify things such as the tags in the vocabulary which tags are allowed to be nested in other tags which tags and attributes are mandatory / optional which values are allowed for attributes So a DTD or a Schema defines a language that can then used to create valid documents

3 DTDs and Schemas © 24/01/2012University of Greenwich3 DTDs and Schemas For an XML document to be valid it must conform to the rules specified in its DTD or Schema XML documents which use the language defined in the DTD or Schema DTD or Schema defines an XML language

4 DTDs and Schemas © 24/01/2012University of Greenwich4 Why do we need valid documents? Without a DTD or a Schema the application codes have to validate all the data before processing checking that required elements are present checking that attribute values are correct. If a change in the format is agreed between the two companies then the application code at both ends needs changing. Estate AgentMortgage Broker agreed format

5 DTDs and Schemas © 24/01/2012University of Greenwich5 Why do we need valid documents? With an agreed DTD or Schema standard code can be used at each end to generate and check the data off-the-shelf software validating parsers Changes only need to be made in one place the DTD or Schema. A DTD or Schema is a way of representing an agreement in a machine readable form that can be processed by standard software

6 DTDs and Schemas © 24/01/2012University of Greenwich6 Why do we need valid documents? Because DTDs and Schemas are machine readable they can be used by standard software in a variety of ways Estate Agent application DTD / Schema Mortgage Broker application XML editorValidating parser valid document

7 DTDs and Schemas © 24/01/2012University of Greenwich7 Defining DTDs root element is recommended_books the root element contains zero or more book elements each book element contains the following elements: author, title, year_published, publisher, course and recommended_by the author and recommended_by elements both consists of firstname and surname elements As an example we shall develop a DTD for an XML document type intended to list books recommended by lecturers for various courses. The first version of such documents will have the following structure:

8 DTDs and Schemas Stephen Spainhour Webmaster in a Nutshell 1999 O'Reilly WAT Gill Windall Benoît Marchal Applied XML Solutions 2000 Sams WAT Kevin McManus goodbooks1.xml Note how the firstname and surname elements appear in both author and recommended_by elements None of the tags in this example contain attributes

9 DTDs and Schemas © 24/01/2012University of Greenwich9 goodbooks1.dtd <!ELEMENT book (author, title, year_published, publisher, course, recommended_by)> contains 10 element definitions

10 DTDs and Schemas © 24/01/2012University of Greenwich10 goodbooks1.dtd >(#PCDATA)surnameELEMENT<! >(#PCDATA)firstnameELEMENT<! >(firstname, surname)recommended_byELEMENT<! >(#PCDATA)courseELEMENT<! >(#PCDATA)publisherELEMENT<! >(#PCDATA)year_publishedELEMENT<! >(#PCDATA)titleELEMENT<! >(firstname, surname)authorELEMENT<! >(author, title, year_published, publisher, course, recommended_by) bookELEMENT<! >(book*)recommended_booksELEMENT<! element contentselement / tag nametype

11 DTDs and Schemas © 24/01/2012University of Greenwich11 goodbooks1.dtd The DTD can be read as meaning: recommended_books contains zero of more book elements each book element contains in order the elements: author title year_published publisher course recommended_by the author and recommended_by elements both consists of firstname and surname elements the title, year_published, publisher, course, firstname and surname elements consist of text the actual data

12 DTDs and Schemas © 24/01/2012University of Greenwich12 DTD syntax parsed character data - a string of text#PCDATA parentheses ( ) are used to group elements so this means zero or more occurrences of eleA followed by eleB (eleA,eleB)* eleA is followed by eleBeleA, eleB eleA or eleB occurs but not botheleA | eleB eleA occurs zero or more timeseleA* eleA occurs one of more timeseleA+ eleA is optionaleleA? Meaning of contentsExpression

13 DTDs and Schemas © 24/01/2012University of Greenwich13 Four Element Forms Empty Elements have no element content but can still contain information in their attributes. Element-Only Elements contain only child elements. Their content model is a list of child elements arranged using the expressions listed in the previous table. Text-Only Elements contain only character data (text). Their content model is simply (#PCDATA). Mixed Elements contain both child elements and character data. Their content model must contain a choice list beginning with #PCDATA. The rest of the choice list contains the child elements and it must end in an asterisk indicating that the entire choice group is optional. Although this constrains the type of child element it does not constrain the order or quantity.

14 DTDs and Schemas © 24/01/2012University of Greenwich14 Quick Quiz 7652 9856 23.56 0085 45.50 1134 100 23.56 Here's a DTD Why is the following not a valid document according to the DTD?

15 DTDs and Schemas © 24/01/2012University of Greenwich15 goodbooks2.xml Extending the recommended books example to include attributes The definition of the document type is changed to: make the year_published element optional allow more than one course to be referenced include a rating attribute of the book element which can take the values "ok" or "good" or "excellent" and has a default value of "ok"

16 DTDs and Schemas Stephen Spainhour Webmaster in a Nutshell 1999 O'Reilly WAT Internet Publishing Gill Windall Benoît Marchal Applied XML Solutions Sams WAT Kevin McManus attribute repeated course element attribute omitted year_published goodbooks2.xml

17 DTDs and Schemas © 24/01/2012University of Greenwich17 goodbooks2.dtd <!ELEMENT book (author, title, year_published?, publisher, course+, recommended_by)> year_published is now optional course can occur more than once new rule defining a rating attribute for the book element

18 DTDs and Schemas © 24/01/2012University of Greenwich18 Attribute Rules "ok" is the default value from the rating enumerated series. Other attribute definitions are possible: #REQUIRED – the attribute is required #IMPLIED – the attribute is optional #FIXED value – the attribute has a fixed value (constant) As well as enumerated attribute types there are: CDATA – unparsed character data NOTATION – notation declared elsewhere in the DTD ENTITY – external entity ID – unique identifier IDREF – reference to an ID elsewhere in the DTD NMTOKEN – name containing only token characters, i.e. no whitespace Attributes can be defined anywhere in the DTD but usualy placed immediately after the corresponding element Multiple attributes for an element are declared in a singe attribute list. <!ATTLIST book rating (ok | good | excellent) "ok" reviewer CDATA #REQUIRED >

19 DTDs and Schemas © 24/01/2012University of Greenwich19 Not so Quick Quiz How do you decide if information should be in an element or an attribute?

20 DTDs and Schemas © 24/01/2012University of Greenwich20 Linking the DTD to the XML document name of the root element URL of document containing the DTD Stephen...... The XML document can refer to an external DTD using

21 DTDs and Schemas © 24/01/2012University of Greenwich21 Linking the DTD to the XML document <!DOCTYPE recommended_books [ <!ELEMENT book (author, title, year_published?, publisher, course+, recommended_by)> ]> Stephen Alternatively the DTD can be included inline within the XML document

22 DTDs and Schemas © 24/01/2012University of Greenwich22 Quick Quiz This program was brought to you by Webbed Wonders. We can be contacted at Lettuce Towers Braythorpe Street Wessex WA1 7QT Thanks for your interest. Suppose we want to define an element that can contain a mixture of other elements (i.e. tags) and plain text Which of the following do you think is the correct way of specifying in a DTD the element as used above? 1. 2. 3. 4. 5. It's not possible because the document isn't well-formed.

23 DTDs and Schemas © 24/01/2012University of Greenwich23 What else can you do with DTDs? Specify that an attribute value is unique within a document (a bit like a primary key in a data base table) e.g. Specify that the value of one attribute refers to an attribute type ID using an attribute type IDREF (like a foreign key) e.g...............

24 DTDs and Schemas © 24/01/2012University of Greenwich24 What else can you do with DTDs? Define your own entities, often commonly used strings e.g......... &Disclaimer; Define ways of handling non-XML data e.g.........

25 DTDs and Schemas © 24/01/2012University of Greenwich25 What can you not do with DTDs? Specify the data type (e.g. integer) of an element or attribute. The only data type recognised is string. Specify a set of values that an element's content may take (you can do this for attributes but not elements). Write them using XML tools! The and constructs are SGML comment declarations. Easily mix vocabularies (i.e. XML vocabularies) from different DTDs. Accurately define the structure of a mixed element cf. the preceding quick quiz. Because of these and other restrictions there have been a number of initiatives to develop alternatives to DTDs. The one that has the backing of the W3C is the XML Schemas specification.

26 DTDs and Schemas © 24/01/2012University of Greenwich26 goodbooks3.xsd <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified"> Re-writing goodbooks2.dtd as an XML schema results in a significantly longer file. This is listed over the next 4 slides with the corresponding DTD for comparison

27 DTDs and Schemas © 24/01/2012University of Greenwich27 goodbooks3.xsd...... <!ELEMENT book (author, title, year_published?, publisher, course+, recommended_by)> unless stated the value of minOccurs and maxOccurs is 1

28 DTDs and Schemas © 24/01/2012University of Greenwich28 goodbooks3.xsd...... Note how the attribute definition is nested within the definition of the book element

29 DTDs and Schemas © 24/01/2012University of Greenwich29 goodbooks3.xsd note data types

30 DTDs and Schemas © 24/01/2012University of Greenwich30 goodbooks3.xsd

31 DTDs and Schemas © 24/01/2012University of Greenwich31 Things to notice about goodbooks3.xsd XML schemas are much more verbose than DTDs The XML schemas language itself conforms to XML syntax rules and so can be manipulated using standard XML tools (e.g. XML Spy) More specific restrictions can be made on the occurrence of elements than with DTDs e.g. both the above mean the same but in schemas minOccurs and maxOccurs can be used to restrict the number of allowed occurrences In DTDs the only data type for elements is #PCDATA whereas schemas contain much more support for data types e.g. A full range of data types are supported (e.g. boolean, float, datetime) plus you can define your own. XML Schemas make use of namespaces

32 DTDs and Schemas © 24/01/2012University of Greenwich32 Linking a Schema to an XML document Not totally standard and somewhat tied to W3C but the method below works with at least some tools that support Schemas <recommended_books xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="goodbooks3.xsd"> Stephen Spainhour...... this line associates the schema stored in goodbooks2.xsd in the same directory with the XML document

33 DTDs and Schemas © 24/01/2012University of Greenwich33 Namespaces Namespaces are a way of avoiding name conflicts, i.e. where different XML vocabularies use the same names to mean different things. In designing an XML based language we may want to include elements from several other XML languages e.g. ProductMLCustomerML InvoiceML when defining a new XML language to describe invoice documents we may want to draw on existing languages for describing products and customers

34 DTDs and Schemas © 24/01/2012University of Greenwich34 Namespaces What to do about name clashes, e.g. it is likely that ProductML and CustomerML both contain elements Giant Widget George Barford We don't want applications that process InvoiceML to confuse the elements. Dear Mr Giant Widget, Your George Barford has been despatched today...

35 DTDs and Schemas © 24/01/2012University of Greenwich35 Namespaces Namespaces give a mechanism for "qualifying" element names with a prefix so that they are all unique, e.g. Giant Widget George Barford Wherever you see element names including a prefix followed by a ":" you can be sure that namespaces are being used e.g.

36 DTDs and Schemas © 24/01/2012University of Greenwich36 Namespaces The prefix needs to be defined in the XML document that is using it by including the xmlns attribute. For example to define the prod: and cust: prefixes in an invoice document declaring a default namespace that uses no prefix <invoices xmlns:prod="http://mycompany.com/products" xmlns:cust="http://mycompany.com/customers" xmlns="http://mycompany.com/invoices"> 2314.... Giant Widget George Barford.... declaring a namespace associated with the prod prefix declaring a namespace associated with the cust prefix

37 DTDs and Schemas © 24/01/2012University of Greenwich37 Namespaces In the previous example it is tempting to guess that this line… <invoices xmlns:prod="http://mycompany.com/products" xmlns:cust="http://mycompany.com/customers" xmlns="http://mycompany.com/invoices"> associates the prod: prefix with an XML Schema located at http://mycompany.com/products and cust: with one at http://mycompany.com/customers But these URLs need not be actual locations at all - they are simply unique names used to identify namespaces. URIs (URLs & URNs) are convenient ways of specifying unique values. There is a way of tying prefixes to actual XML Schemas (but not DTDs) so that documents can be validated against multiple Schemas. The syntax is both messy and unclear and beyond what we are going to look at here.

38 DTDs and Schemas © 24/01/2012University of Greenwich38 Summary DTDs and schemas are two methods of specifying an XML language DTDs are widely supported have limited features XSDs are an XML language provide tighter specification than DTDs support namespaces Namespaces support multi-lingual XML applications validation of multiple namespaces remains unclear

39 DTDs and Schemas © 24/01/2012University of Greenwich39 Questions


Download ppt "DTDs and Schemas © 24/01/2012University of Greenwich1 XML 2 DTDs, Schemas and Namespaces Kevin McManus Recycled from Gill Windall’s notes."

Similar presentations


Ads by Google