dtds and schemas © 24/01/2012university of greenwich1 xml 2 dtds, schemas and namespaces kevin...

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

Post on 18-Dec-2015

225 views

Category:

Documents


1 download

TRANSCRIPT

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

© 24/01/2012 University of Greenwich 1

DTDs and Schemas

XML 2

DTDs, Schemas and Namespaces

Kevin McManusRecycled from Gill Windall’s notes

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

© 24/01/2012 University of Greenwich 2

DTDs and Schemas

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

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

© 24/01/2012 University of Greenwich 3

DTDs and Schemas

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

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

© 24/01/2012 University of Greenwich 4

DTDs and Schemas

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 Agent Mortgage Broker

agreed format

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

© 24/01/2012 University of Greenwich 5

DTDs and Schemas

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

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

© 24/01/2012 University of Greenwich 6

DTDs and Schemas

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 editor Validating parservalid

document

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

© 24/01/2012 University of Greenwich 7

DTDs and Schemas

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:

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

DTDs and Schemas<?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

Note how the firstname and surname elements appear in both author and recommended_by elements

None of the tags in this example contain attributes

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

© 24/01/2012 University of Greenwich 9

DTDs and Schemas

goodbooks1.dtd

<?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)>

contains 10 element definitions

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

© 24/01/2012 University of Greenwich 10

DTDs and Schemas

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

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

© 24/01/2012 University of Greenwich 11

DTDs and Schemas

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

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

© 24/01/2012 University of Greenwich 12

DTDs and Schemas

DTD syntax

parsed character data - a string of text#PCDATA

parentheses ( ) are used to group elements so thismeans 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

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

© 24/01/2012 University of Greenwich 13

DTDs and Schemas

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.

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

© 24/01/2012 University of Greenwich 14

DTDs and Schemas

Quick Quiz

<!ELEMENT transactions (tran*)><!ELEMENT tran (account, (debit|credit)?)><!ELEMENT account (#PCDATA)><!ELEMENT debit (#PCDATA)><!ELEMENT credit (#PCDATA)>

<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>

Here's a DTD

Why is the following not a valid document according to the DTD?

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

© 24/01/2012 University of Greenwich 15

DTDs and Schemas

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"

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

DTDs and Schemas<?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

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

© 24/01/2012 University of Greenwich 17

DTDs and Schemas

goodbooks2.dtd

<?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)>

year_published is now optional

course can occur more than once

new rule defining a rating attribute for the book element

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

© 24/01/2012 University of Greenwich 18

DTDs and Schemas

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>

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

© 24/01/2012 University of Greenwich 19

DTDs and Schemas

Not so Quick Quiz

• How do you decide if information should be in an element or an attribute?

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

© 24/01/2012 University of Greenwich 20

DTDs and Schemas

Linking the DTD to the XML document

name of the root element

URL of document containing the DTD

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE recommended_books SYSTEM "goodBooks2.dtd"><recommended_books><book rating="excellent"> <author> <firstname>Stephen</firstname> ......

The XML document can refer to an external DTD using <!DOCTYPE >

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

© 24/01/2012 University of Greenwich 21

DTDs and Schemas

Linking the DTD to 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>

Alternatively the DTD can be included inline within the XML document

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

© 24/01/2012 University of Greenwich 22

DTDs and Schemas

Quick Quiz

<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>

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 <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.

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

© 24/01/2012 University of Greenwich 23

DTDs and Schemas

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">

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

© 24/01/2012 University of Greenwich 24

DTDs and Schemas

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">

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

© 24/01/2012 University of Greenwich 25

DTDs and Schemas

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.

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

© 24/01/2012 University of Greenwich 26

DTDs and Schemas

goodbooks3.xsd

<?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*)>

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

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

© 24/01/2012 University of Greenwich 27

DTDs and Schemas

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> ......

<!ELEMENT book (author, title, year_published?, publisher, course+, recommended_by)>

unless stated the value of minOccurs and maxOccurs is 1

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

© 24/01/2012 University of Greenwich 28

DTDs and Schemas

goodbooks3.xsd

...... <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">

Note how the attribute definition is nested within the definition of the book element

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

© 24/01/2012 University of Greenwich 29

DTDs and Schemas

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"/>

<!ELEMENT title (#PCDATA)><!ELEMENT year_published (#PCDATA)><!ELEMENT publisher (#PCDATA)><!ELEMENT course (#PCDATA)>

note data types

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

© 24/01/2012 University of Greenwich 30

DTDs and Schemas

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)>

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

© 24/01/2012 University of Greenwich 31

DTDs and Schemas

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">

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

© 24/01/2012 University of Greenwich 32

DTDs and Schemas

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

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

© 24/01/2012 University of Greenwich 33

DTDs and Schemas

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.

ProductML CustomerML

InvoiceMLwhen defining a new XML language to describe invoice documents we may want to draw on existing languages for describing products and customers

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

© 24/01/2012 University of Greenwich 34

DTDs and Schemas

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 ...

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

© 24/01/2012 University of Greenwich 35

DTDs and Schemas

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">

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

© 24/01/2012 University of Greenwich 36

DTDs and Schemas

NamespacesThe 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"> <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 prod prefix

declaring a namespace associated with the cust prefix

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

© 24/01/2012 University of Greenwich 37

DTDs and Schemas

NamespacesIn 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.

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

© 24/01/2012 University of Greenwich 38

DTDs and Schemas

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

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

© 24/01/2012 University of Greenwich 39

DTDs and Schemas

Questions