1 / 39

XML 2 DTDs, Schemas and Namespaces

XML 2 DTDs, Schemas and Namespaces. Kevin McManus Recycled from Gill Windall’s notes. 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

ernst
Download Presentation

XML 2 DTDs, Schemas and Namespaces

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. XML 2 DTDs, Schemas and Namespaces Kevin McManus Recycled from Gill Windall’s notes University of Greenwich

  2. 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 University of Greenwich

  3. 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 University of Greenwich

  4. Why do we need valid documents? agreed format • 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 Agent Mortgage Broker University of Greenwich

  5. 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 bystandard software University of Greenwich

  6. 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 Mortgage Broker application valid document XML editor Validating parser DTD / Schema University of Greenwich

  7. Defining DTDs 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: • 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 University of Greenwich

  8. <?xml version="1.0" encoding="UTF-8"?> <recommended_books> <book> <author> <firstname>Stephen</firstname> <surname>Spainhour</surname> </author> <title>Webmaster in a Nutshell</title> <year_published>1999</year_published> <publisher>O'Reilly</publisher> <course>WAT</course> <recommended_by> <firstname>Gill</firstname> <surname>Windall</surname> </recommended_by> </book> <book> <author> <firstname>Benoît</firstname> <surname>Marchal</surname> </author> <title>Applied XML Solutions</title> <year_published>2000</year_published> <publisher>Sams</publisher> <course>WAT</course> <recommended_by> <firstname>Kevin</firstname> <surname>McManus</surname> </recommended_by> </book> </recommended_books> goodbooks1.xml None of the tags in this example contain attributes Note how the firstname and surname elements appear in both author and recommended_by elements

  9. goodbooks1.dtd contains 10 element definitions <?xml version="1.0" encoding="UTF-8"?> <!ELEMENT recommended_books (book*)> <!ELEMENT book (author, title, year_published, publisher, course, recommended_by)> <!ELEMENT author (firstname, surname)> <!ELEMENT title (#PCDATA)> <!ELEMENT year_published (#PCDATA)> <!ELEMENT publisher (#PCDATA)> <!ELEMENT course (#PCDATA)> <!ELEMENT recommended_by (firstname, surname)> <!ELEMENT firstname (#PCDATA)> <!ELEMENT surname (#PCDATA)> University of Greenwich

  10. type element / tag name element contents <! ELEMENT recommended_books (book*) > <! ELEMENT book (author, title, year_published, publisher, course, recommended_by) > <! ELEMENT author (firstname, surname) > <! ELEMENT title (#PCDATA) > <! ELEMENT year_published (#PCDATA) > <! ELEMENT publisher (#PCDATA) > <! ELEMENT course (#PCDATA) > <! ELEMENT recommended_by (firstname, surname) > <! ELEMENT firstname (#PCDATA) > <! ELEMENT surname (#PCDATA) > goodbooks1.dtd University of Greenwich

  11. 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 University of Greenwich

  12. Expression Meaning of contents eleA? eleA is optional eleA+ eleA occurs one of more times eleA* eleA occurs zero or more times eleA | eleB eleA or eleB occurs but not both eleA, eleB eleA is followed by eleB (eleA,eleB)* parentheses ( ) are used to group elements so this means zero or more occurrences of eleA followed by eleB #PCDATA parsed character data - a string of text DTD syntax University of Greenwich

  13. 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. University of Greenwich

  14. Quick Quiz <!ELEMENT transactions (tran*)> <!ELEMENT tran (account, (debit|credit)?)> <!ELEMENT account (#PCDATA)> <!ELEMENT debit (#PCDATA)> <!ELEMENT credit (#PCDATA)> Here's a DTD Why is the following not a valid document according to the DTD? <transactions> <tran><account>7652</account></tran> <tran><account>9856</account><credit>23.56</credit></tran> <tran><account>0085<debit>45.50</debit></account></tran> <tran> <account>1134</account> <debit>100</debit><credit>23.56</credit> </tran> </transactions> University of Greenwich

  15. 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" University of Greenwich

  16. <?xml version="1.0" encoding="UTF-8"?> <recommended_books> <book rating="excellent"> <author> <firstname>Stephen</firstname> <surname>Spainhour</surname> </author> <title>Webmaster in a Nutshell</title> <year_published>1999</year_published> <publisher>O'Reilly</publisher> <course>WAT</course> <course>Internet Publishing</course> <recommended_by> <firstname>Gill</firstname> <surname>Windall</surname> </recommended_by> </book> <book rating="good"> <author> <firstname>Benoît</firstname> <surname>Marchal</surname> </author> <title>Applied XML Solutions</title> <publisher>Sams</publisher> <course>WAT</course> <recommended_by> <firstname>Kevin</firstname> <surname>McManus</surname> </recommended_by> </book> </recommended_books> attribute repeated course element attribute omitted year_published goodbooks2.xml

  17. goodbooks2.dtd year_published is now optional <?xml version="1.0" ?> <!ELEMENT recommended_books (book*)> <!ELEMENT book (author, title, year_published?, publisher, course+, recommended_by)><!ATTLIST book rating (ok | good | excellent) "ok"> <!ELEMENT author (firstname, surname)> <!ELEMENT title (#PCDATA)> <!ELEMENT year_published (#PCDATA)> <!ELEMENT publisher (#PCDATA)> <!ELEMENT course (#PCDATA)> <!ELEMENT recommended_by (firstname, surname)> <!ELEMENT firstname (#PCDATA)> <!ELEMENT surname (#PCDATA)> course can occur more than once new rule defining a rating attribute for the book element University of Greenwich

  18. 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 > University of Greenwich

  19. Not so Quick Quiz • How do you decide if information should be in an element or an attribute? University of Greenwich

  20. Linking the DTD to the XML document The XML document can refer to an external DTD using <!DOCTYPE > name of the root element <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE recommended_books SYSTEM "goodBooks2.dtd"><recommended_books> <book rating="excellent"> <author> <firstname>Stephen</firstname> ...... URL of document containing the DTD University of Greenwich

  21. Linking the DTD to the XML document Alternatively the DTD can be included inline within the XML document <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE recommended_books [ <!ELEMENT recommended_books (book*)> <!ELEMENT book (author, title, year_published?, publisher, course+, recommended_by)> <!ATTLIST book rating (ok | good | excellent) "ok"> <!ELEMENT author (firstname, surname)> <!ELEMENT title (#PCDATA)> <!ELEMENT year_published (#PCDATA)> <!ELEMENT publisher (#PCDATA)> <!ELEMENT course (#PCDATA)> <!ELEMENT recommended_by (firstname, surname)> <!ELEMENT firstname (#PCDATA)> <!ELEMENT surname (#PCDATA)> ]> <recommended_books> <book rating="excellent"> <author> <firstname>Stephen</firstname> University of Greenwich

  22. Quick Quiz Suppose we want to define an element that can contain a mixture of other elements (i.e. tags) and plain text <about> This program was brought to you by <a href="http://www.webbedwonders.co.uk">Webbed Wonders</a>. We can be contacted at <address> <line>Lettuce Towers</line> <line>Braythorpe Street</line> <line>Wessex</line> <postcode>WA1 7QT</postcode> </address> Thanks for your interest.</about> Which of the following do you think is the correct way of specifying in a DTD the <about> element as used above? 1. <!ELEMENT about (a, address)> 2. <!ELEMENT about (#PCDATA | a | address)*> 3. <!ELEMENT about (#PCDATA, a, address)*> 4. <!ELEMENT about (#PCDATA, a, # PCDATA, address, #PCDATA)> 5. It's not possible because the document isn't well-formed. University of Greenwich

  23. 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. <!ATTLIST BankBranch BranchID ID #REQUIRED> • Specify that the value of one attribute refers to an attribute type ID using an attribute type IDREF (like a foreign key) e.g. <!ATTLIST account branch IDREF #REQUIRED> ....... <BankBranch BranchID="SC30_00_02"> ....... <account branch="SC30_00_02"> University of Greenwich

  24. What else can you do with DTDs? • Define your own entities, often commonly used strings e.g. <!ENTITY Disclaimer "Umpire decision is final!"> ........ <footer>&Disclaimer;</footer> • Define ways of handling non-XML data e.g. <!NOTATION png SYSTEM 'png_view.exe'> ........ <diagram type="png" file="graph.png"> University of Greenwich

  25. 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 <!ELEMENT> and <!ATTLIST> 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. University of Greenwich

  26. goodbooks3.xsd 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 <?xml version="1.0" encoding="UTF-8"?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified"> <xs:element name="recommended_books"> <xs:complexType> <xs:sequence> <xs:element ref="book" minOccurs="0" maxOccurs="unbounded"/> </xs:sequence> </xs:complexType> </xs:element> <!ELEMENT recommended_books (book*)> University of Greenwich

  27. goodbooks3.xsd <xs:element name="book"> <xs:complexType> <xs:sequence> <xs:element ref="author"/> <xs:element ref="title"/> <xs:element ref="year_published" minOccurs="0"/> <xs:element ref="publisher"/> <xs:element ref="course" maxOccurs="unbounded"/> <xs:element ref="recommended_by"/> </xs:sequence> ...... unless stated the value of minOccurs and maxOccurs is 1 <!ELEMENT book (author, title, year_published?, publisher, course+, recommended_by)> University of Greenwich

  28. goodbooks3.xsd Note how the attribute definition is nested within the definition of the book element ...... <xs:attribute name="rating" use="optional" default="ok"> <xs:simpleType> <xs:restriction base="xs:string"> <xs:enumeration value="excellent"/> <xs:enumeration value="good"/> <xs:enumeration value="ok"/> </xs:restriction> </xs:simpleType> </xs:attribute> </xs:complexType> </xs:element> <!ATTLIST book rating (ok | good | excellent) "ok"> University of Greenwich

  29. goodbooks3.xsd <xs:element name="author"> <xs:complexType> <xs:sequence> <xs:element ref="firstname"/> <xs:element ref="surname"/> </xs:sequence> </xs:complexType> </xs:element> <!ELEMENT author (firstname, surname)> <xs:element name="title" type="xs:string"/> <xs:element name="year_published" type="xs:short"/> <xs:element name="publisher" type="xs:string"/> <xs:element name="course" type="xs:string"/> note data types <!ELEMENT title (#PCDATA)> <!ELEMENT year_published (#PCDATA)> <!ELEMENT publisher (#PCDATA)> <!ELEMENT course (#PCDATA)> University of Greenwich

  30. goodbooks3.xsd <xs:element name="recommended_by"> <xs:complexType> <xs:sequence> <xs:element ref="firstname"/> <xs:element ref="surname"/> </xs:sequence> </xs:complexType> </xs:element> <!ELEMENT recommended_by (firstname, surname)> <xs:element name="firstname" type="xs:string"/> <xs:element name="surname" type="xs:string"/> </xs:schema> <!ELEMENT firstname (#PCDATA)> <!ELEMENT surname (#PCDATA)> University of Greenwich

  31. 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. <!ELEMENT recommended_books (book*)> <xs:element ref="book" minOccurs="0" maxOccurs="unbounded "/> • 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. <xs:element name="title" type="xs:string"/> <xs:element name="year_published" type="xs:short"/> • 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 <xs:element name="recommended_books"> University of Greenwich

  32. 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 <?xml version="1.0" encoding="UTF-8"?> <recommended_books xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="goodbooks3.xsd"> <book rating="excellent"> <author> <firstname>Stephen</firstname> <surname>Spainhour</surname> ...... this line associates the schema stored in goodbooks2.xsd in the same directory with the XML document University of Greenwich

  33. ProductML CustomerML InvoiceML 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. when defining a new XML language to describe invoice documents we may want to draw on existing languages for describing products and customers University of Greenwich

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

  35. Namespaces Namespaces give a mechanism for "qualifying" element names with a prefix so that they are all unique, e.g. <prod:name>Giant Widget</prod:name> <cust:name>George Barford</cust:name> Wherever you see element names including a prefix followed by a ":" you can be sure that namespaces are being used e.g. <xs:element name="event"> University of Greenwich

  36. 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 namespace associated with the prod prefix <invoices xmlns:prod="http://mycompany.com/products" xmlns:cust="http://mycompany.com/customers" xmlns="http://mycompany.com/invoices"> <invoice> <invoice_id>2314</invoice_id> .... <prod:name>Giant Widget</prod:name> <cust:name>George Barford</cust:name> .... </invoice> </invoices> declaring a namespace associated with the cust prefix declaring a default namespace that uses no prefix University of Greenwich

  37. 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. University of Greenwich

  38. 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 University of Greenwich

  39. Questions University of Greenwich

More Related