Download presentation
Presentation is loading. Please wait.
Published byBeverley Cobb Modified over 8 years ago
1
XML DTDs and Schemas How we define our markup language
2
November 2012Web Programming2 Remember XML? ● XHTML is a DTD of XML – A Document Type Declaration (or Definition) of XML ● This means that XHTML is XML – Similarly, HTML is SGML, which is related ● What is valid XHTML is defined using the DTD ● But the DTD is in effect a language of its own...
3
November 2012Web Programming3 The DTD defines... ● What elements appear in a document ● What attributes elements can/must have ● What elements can/must appear inside others ● The sequence of elements
4
November 2012Web Programming4 The DTD is declared ● At the top of any XHTML file, e.g. (etc)
5
November 2012Web Programming5 Example XML DTD
6
November 2012Web Programming6 The problem with DTDs ● Not easily manipulated or transformed ● Do not specify or control the type of data 1.417 Lecture ● Perfectly valid! And there's nothing that can be done in the DTD to prevent it ● There is a solution
7
November 2012Web Programming7 XML Schemas ● We took a little look in CS15020 ● http://www.w3.org/XML/Schema http://www.w3.org/XML/Schema ● There's a great tutorial at – http://www.w3schools.com/schema/ http://www.w3schools.com/schema/ ● The main advantage of the schema is that you can define data types for elements and attributes ● And a schema is itself XML – No other language to worry about
8
November 2012Web Programming8 Will schemas replace the DTD? ● W3Schools think so... – XML Schemas are extensible to future additions – XML Schemas are richer and more powerful than DTDs – XML Schemas are written in XML – XML Schemas support data types – XML Schemas support namespaces
9
November 2012Web Programming9 The Namespace ● If you want to use more than one schema in any one document, you could have a conflict – e.g. in one schema is defined as a paragraph; in another it's defined as a person ● Answer: declare the namespace you're using –
10
November 2012Web Programming10 The date problem ● Imagine 04-12-2012 ● To us, that's 4 th December – But in some countries it's 12 th April ● XML schemas has a data type date that forces the format YYYY-MM-DD – e.g. 2012-12-04 (the ISO standard)the ISO standard ● By declaring what data should look like, we help to ensure everyone understands
11
November 2012Web Programming11 Example XML Schema <xs:element name="problem" type="xs:string" minOccurs="1" maxOccurs="unbounded"/> Other possibilities are: xs:all (any order, 1 occ.) xs:choice (1 only) Other possibilities include: xs:integer xs:decimal xs:date
12
November 2012Web Programming12 Using the schema ● If it were a DTD, we might have: <!DOCTYPE list SYSTEM "http://somewhere.com/list.dtd"> ● The equivalent for a schema would be: <list xmlns="http://somewhere.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema- instance" xsi:schemaLocation="http://somewhere.com list.xsd">
13
November 2012Web Programming13 Breaking it down <list xmlns="http://somewhere.com" The string here technically didn't have to be a URI (it's actually referred to as an IRI – an Internationalized Resource Identifier) It merely serves as a string to identify the namespace we're working with; all the elements used in this XML document are declared in the "http://somewhere.com" namespace
14
November 2012Web Programming14 Breaking it down (2) xmlns:xsi="http://www.w3.org/2001/XMLSchema- instance" Where the schema that defines the XML for defining a schema is (!) - we need this so we can go on to define the location of the schema that we're using: xsi:schemaLocation="http://somewhere.com list.xsd" So three arguments. 1.the name we gave it (a string to identify it); 2.where schemas are defined; 3.where to find the schema we're using.
15
November 2012Web Programming15 Are schemas complicated? ● Defining a document's structure with a schema would appear more complicated than writing a DTD ● But it offers a lot more ● And it is XML (and we know that...?) – So should be tidy and logical ● But are they necessary? – Well, what of the future?
16
November 2012Web Programming16 The future? ● Will Schemas replace DTDs? – A lot of people think so ● The advantages mean they probably will ● If you need to learn schema thoroughly, visit http://www.w3schools.com/schema http://www.w3schools.com/schema
17
November 2012Web Programming17 What of HTML5 ● Ian Hickson has this to say: Ian Hickson – There's no official formal schema for HTML5 because we have found that providing an official formal schema leads to people using that schema even if it is buggy, never considering writing their own. By not having an official formal schema, we have found that implementors are more likely to write their own code, competing on quality. – ref. ref.
18
November 2012Web Programming18 So it has a DTD still? ● Actually, no. – See ref on previous slide ● It seems that HTML5 is defined quite separately, and browsers are expected to understand it ● The DOCTYPE and associated headers for HTML5 are unlike those of its XHTML and HTML predecessors.
19
November 2012Web Programming19 So why use a schema? ● To formally describe an XML document – To your specification – Possibly for display on the web – Possibly in conjunction with other technologies we are about to look at...
20
November 2012Web Programming20 Why might that be useful on the web? ● Consider a games shop catalogue: The Sims a strategic life- simulation computer game Electronic Arts £19.99
21
November 2012Web Programming21 Need to display it on the web ● It's much more logical to store it using a different schema to XHTML ● But we therefore need to transform it somehow to XHTML ● And this can be done, easily, using what we will learn in the next couple of lectures.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.