Title: XML Schema (W3C)
1XML Schema (W3C)
- Thanks to Jussi Pohjolainen
- TAMK University of Applied Sciences
2XML NAMESPACES
3XML Namespaces
- The idea behing XML namespaces is to avoid
element name conflicts. - Example of name conflict (w3schools.com)
lttablegt lttrgt lttdgtAppleslt/tdgt
lttdgtBananaslt/tdgt lt/trgt lt/tablegt
lttablegt ltnamegtAfrican Coffee Tablelt/namegt
ltwidthgt80lt/widthgt ltlengthgt120lt/lengthgt lt/tablegt
Same tag-name, different content and meaning!
4Solving Name Conflict
- lthtablegt
- lthtrgt
- lthtdgtAppleslt/htdgt
- lthtdgtBananaslt/htdgt
- lt/htrgt
- lt/htablegt
- ltftablegt
- ltfnamegtAfrican Coffee Tablelt/fnamegt
- ltfwidthgt80lt/fwidthgt
- ltflengthgt120lt/flengthgt
- lt/ftablegt
Prefix h has xhtml-related elements and prefix f
has furniture-related elements
5xmlns - attributes
- When using prefixes in XML, a so-called namespace
for the prefix must be defined. - The namespace is defined by the xmlns attribute
in the start tag of an element.
6xmlns - attribute
- ltrootgt
- lthtable xmlnsh"http//www.w3.org/TR/html4/"gt
- lthtrgt
- lthtdgtAppleslt/htdgt
- lthtdgtBananaslt/htdgt
- lt/htrgt
- lt/htablegt
- ltftable xmlnsf"http//www.w3schools.com/furni
ture"gt - ltfnamegtAfrican Coffee Tablelt/fnamegt
- ltfwidthgt80lt/fwidthgt
- ltflengthgt120lt/flengthgt
- lt/ftablegt
- lt/rootgt
7xmlns - attribute
- ltroot
- xmlnsh"http//www.w3.org/TR/html4/"
- xmlnsf"http//www.w3schools.com/furniture"gt
- lthtablegt
- lthtrgt
- lthtdgtAppleslt/htdgt
- lthtdgtBananaslt/htdgt
- lt/htrgt
- lt/htablegt
- ltftablegt
- ltfnamegtAfrican Coffee Tablelt/fnamegt
- ltfwidthgt80lt/fwidthgt
- ltflengthgt120lt/flengthgt
- lt/ftablegt
- lt/rootgt
8Namespace name
- The name of namespace should be unique lthtable
xmlnsh"http//www.w3.org/TR/html4/"gt - It is just a string, but it should be declared as
URI. - Using URI reduces the possibility of different
namespaces using duplicate identifiers.
9Example An XHTML MathML SVG Profile
- An XHTMLMathMLSVG profile is a profile that
combines XHTML 1.1, MathML 2.0 and SVG 1.1
together. - This profile enables mixing XHTML, MathML and SVG
in the same document using XML namespaces
mechanism.
10- lt?xml version"1.0"?gt
- lt!DOCTYPE html PUBLIC
- "-//W3C//DTD XHTML 1.1 plus MathML 2.0 plus
SVG 1.1//EN" - "http//www.w3.org/2002/04/xhtml-math-svg/xhtm
l-math-svg-flat.dtd"gt - lthtml xmlns "http//www.w3.org/1999/xhtml
" - xmlnssvg "http//www.w3.org/2000/svg"gt
- ltheadgt
- lttitlegtExample of XHTML, SVG and
MathMLlt/titlegt - lt/headgt
- ltbodygt
- lth2gtMathMLlt/h2gt
- ltpgt
- ltmath xmlns"http//www.w3.org/1998/Math/Mat
hML"gt - ltmfracgt
- ltmigtalt/migt
- ltmigtblt/migt
- lt/mfracgt
11W3C SCHEMA
12XML Schema (W3C)
- Language for defining set of rules for XML
documents. - W3C Recommendation (2001)
- More specific than DTD
- Datatypes!
- Is XML-language and it uses xml namespaces
13Schema vs. DTD (W3Schools.com)
- 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
14DTD Linking
Defines the structure, tag names and order for
all xhtml - documents
W3C has created XML-language "XHTML" by defining
it's rules in DTD.
15DTD Linking
Defines the structure, tag names and order for
all "book"- documents
TAMK has created XML-language "Book" by defining
it's rules in DTD.
16Schema Linking
Defines the structure, tag names and order for
all "book"- documents
TAMK has created XML-language "Book" by defining
it's rules in a Schema.
17Linking?
- The basic idea with linking to Schema
- lt?xml version"1.0"?gt
- ltroot schemaLocation"note.xsd"gt
- ltfoogt...lt/foogt
- lt/rootgt
- The problem with this is that now it is set that
attribute "schemaLocation" is part of your
XML-language
18Linking and Namespace Usage
- Linking with namespace
- lt?xml version"1.0"?gt
- ltroot
- xmlnsxsi"http//www.w3.org/2001/XMLSchema-instan
ce" - xsischemaLocation"note.xsd"gt
- ltfoogt...lt/foogt
- lt/rootgt
- Now the "schemaLocation" attribute is in it's
own namespaces (xsi) and does not belong to the
"main" language.
19Simple Schema
- lt?xml version"1.0"?gt
- ltxsdschema xmlnsxsd"http//www.w3.org/2001/XMLS
chema"gt - ltxsdelement name"complete_name"
type"complete_name_type"/gt -
- ltxsdcomplexType name"type"complete_name_type
"gt - ltxsdsequencegt
- ltxsdelement name"nome" type"xsdstring"/gt
- ltxsdelement name"cognome" type"xsdstring"/gt
- lt/xsdsequencegt
- lt/xsdcomplexTypegt
- lt/xsdschemagt
20Let's remove namespaces...
- lt?xml version"1.0"?gt
- ltschemagt
- ltelement name"complete_name" type"complete_name
_type"/gt -
- ltcomplexType name"complete_name_type"gt
- ltsequencegt
- ltelement name"nome" type"string"/gt
- ltelement name"cognome" type"string"/gt
- lt/sequencegt
- lt/complexTypegt
- lt/schemagt
It doesn't look so confusing after all?
21The Basics Element
- You define the name for the elements by using
element-element. ? - ltelement name"foo" type"bar" /gt
- Type?
- 44 Built-in schema datatypes
- string, double, time, date, etc.
- See all the datatypes
22Usage of Datatypes
- ltxsdelement name"firstname"
- type"xsdstring" /gt
- ltxsdelement name"ableToSwim"
- type"xsdboolean" /gt
- ltxsdelement name"date"
- type"xsddate" /gt
23minOccurs and maxOccurs
- The amount of elements
- In DTD , ?,
- In Schema minOccurs, maxOccurs
- Exampleltxsdelement name"date" type"xsddate"
minOccurs"1" maxOccurs"2" /gt - Default and special values
- default minOccurs 1
- default maxOccurs same as minOccurs
- maxOccurs"unbounded" unlimited
24Defining new Datatypes
- If the the built-in datatypes are not enough, you
can build your own datatypes. - This does not necessarily work
- ltxsdelement name"grade" type"xsdinteger" /gt
- There are two ways of specifying your own
datatype - Named Data Type
- Anonymous Data Type
251) Named Data Type
- lt?xml version"1.0"?gt
- ltxsdschema xmlnsxsd"http//www.w3.org/2001/XMLS
chema"gt - ltxsdelement name"grade" type"grade_type"
/gt -
- ltxsdsimpleType name"grade_type"gt
- ltxsdrestriction base"xsdpositiveInteger
"gt - ltxsdminInclusive value"4"/gt
- ltxsdmaxInclusive value"10"/gt
- lt/xsdrestrictiongt
- lt/xsdsimpleTypegt
-
- lt/xsdschemagt
262) Anonymous Data Type
- lt?xml version"1.0"?gt
- ltxsdschema xmlnsxsd"http//www.w3.org/2001/XMLS
chema"gt - ltxsdelement name"grade"gt
- ltxsdsimpleTypegt
- ltxsdrestriction base"xsdpositiveInt
eger"gt - ltxsdminInclusive value"4"/gt
- ltxsdmaxInclusive value"10"/gt
- lt/xsdrestrictiongt
- lt/xsdsimpleTypegt
- lt/xsdelementgt
-
- lt/xsdschemagt
27Benefits of Named Data Type
- If you want re-use your datatype
- lt?xml version"1.0"?gt
- ltxsdschema xmlnsxsd"http//www.w3.org/2001/XMLS
chema"gt - ltxsdelement name"grade" type"grade_type" /gt
- ltxsdelement name"teachers_IQ"
type"grade_type" /gt - ltxsdsimpleType name"grade_type"gt
- ltxsdrestriction base"xsdpositiveInteger
"gt - ltxsdminInclusive value"4"/gt
- ltxsdmaxInclusive value"10"/gt
- lt/xsdrestrictiongt
- lt/xsdsimpleTypegt
-
- lt/xsdschemagt
28SimpleType enumeration
- Alternative content
- ltxsdsimpleType name"car"gt
- ltxsdrestriction base"xsdstring"gt
- ltxsdenumeration value"Audi"/gt
- ltxsdenumeration value"Golf"/gt
- ltxsdenumeration value"BMW"/gt
- lt/xsdrestrictiongt
- lt/xsdsimpleTypegt
29SimpleType pattern
- Using REGEX
- ltxsdsimpleType name"lowercase_char"gt
- ltxsdrestriction base"xsdstring"gt
- ltxsdpattern value"a-z"/gt
- lt/xsdrestrictiongt
- lt/xsdsimpleTypegt
30REGEX Examples
- ltxspattern value"A-ZA-ZA-Z"/gt
- ltxspattern value"a-zA-Za-zA-Za-zA-Z"/gt
- ltxspattern value"xyz"/gt
- ltxspattern value"0-90-90-90-90-9"/gt
- ltxspattern value"(a-z)"/gt
- ltxspattern value"malefemale"/gt
- ltxspattern value"a-zA-Z0-98"/gt
31Structure of the XML-file
- It's possible to define the structure of the
XML-file using complexType - If element A has child-elements, then element A's
type is complexType
32SimpleType vs. ComplexType
- SimpleType
- ltgradegt7lt/gradegt
- Since grade does not hold other child elements,
grade's type is simpleType - ComplexType
- ltstudentsgtltstudentgtJacklt/studentgtlt/studentsgt
- Since student does hold child element(s),
student's type is complexType
33Example XML - File
- lt?xml version"1.0"?gt
- ltstudentsgt
- ltfirstnamegtPekkalt/firstnamegt
- ltlastnamegtVirtanenlt/lastnamegt
- lt/studentsgt
34Example XSD fileNamed ComplexType
- lt?xml version"1.0"?gt
- ltxsdschema xmlnsxsd"http//www.w3.org/2001/XMLS
chema"gt - ltxsdelement name"students"
type"students_type"gt - ltxsdcomplexType name"students_type"gt
- ltxsdsequencegt
- ltxsdelement name"firstname"
type"xsdstring"/gt - ltxsdelement name"lastname"
type"xsdstring"/gt - lt/xsdsequencegt
- lt/xsdcomplexTypegt
-
- lt/xsdschemagt
Use now complexType (vs. simpleType)
35Example XSD fileAnonymous ComplexType
- lt?xml version"1.0"?gt
- ltxsdschema xmlnsxsd"http//www.w3.org/2001/XMLS
chema"gt - ltxsdelement name"students"gt
- ltxsdcomplexTypegt
- ltxsdsequencegt
- ltxsdelement name"firstname"
type"xsdstring"/gt - ltxsdelement name"lastname"
type"xsdstring"/gt - lt/xsdsequencegt
- lt/xsdcomplexTypegt
- lt/xsdelementgt
-
- lt/xsdschemagt
36Example ComplexType
- ltxsdelement name"employee" type"personinfo"/gt
- ltxsdelement name"student" type"personinfo"/gt
- ltxsdelement name"member" type"personinfo"/gt
- ltxsdcomplexType name"personinfo"gt
- ltxsdsequencegt
- ltxsdelement name"firstname"
type"xsdstring"/gt - ltxsdelement name"lastname"
type"xsdstring"/gt - lt/xsdsequencegt
- lt/xsdcomplexTypegt
37Deep Structure in XML - File
- lt?xml version"1.0"?gt
- ltstudentsgt
- ltstudentgt
- ltnamegt
- ltfirstnamegtPekkalt/firstnamegt
- lt/namegt
- lt/studentgt
- lt/studentsgt
38Using Anonymous Data Type The Horror!
- lt?xml version"1.0"?gt
- ltxsdschema xmlnsxsd"http//www.w3.org/2001/XMLS
chema"gt - ltxsdelement name"students"gt
- ltxsdcomplexTypegt
- ltxsdsequencegt
- ltxsdelement name"student"gt
- ltxsdcomplexTypegt
- ltxsdsequencegt
- ltxsdelement
name"name"gt -
ltxsdcomplexTypegt -
ltxsdsequencegt -
ltxsdelement name"firstname" type"xsdstring"/gt -
lt/xsdsequencegt -
lt/xsdcomplexTypegt - lt/xsdelementgt
- lt/xsdsequencegt
- lt/xsdcomplexTypegt
- lt/xsdelementgt
39"There is an error in my schema, could you find
it for me?"
- lt?xml version"1.0"?gt
- ltxsdschema xmlnsxsd"http//www.w3.org/2001/XMLS
chema"gt - ltxsdelement name"students"gt
- ltxsdcomplexTypegt
- ltxsdsequencegt
- ltxsdelement name"student"gt
- ltxsdcomplexTypegt
- ltxsdsequencegt
- ltxsdelement name"name"gt
- ltxsdcomplexTypegt
- ltxsdsequencegt
- ltxsdelement name"firstname" type"xsdstring"/gt
- lt/xsdsequencegt
- lt/xsdcomplexTypegt
- lt/xsdelementgt
- lt/xsdsequencegt
- lt/xsdcomplexTypegt
- lt/xsdelementgt
40Use Named Datatypes! It's easier to find errors..
- lt?xml version"1.0"?gt
- ltxsdschema xmlnsxsd"http//www.w3.org/2001/XMLS
chema"gt - ltxsdelement name"students" type"students_type
" /gt - ltxsdcomplexType name"students_type"gt
- ltxsdsequencegt
- ltxsdelement name"student"
name"student_type" /gt - lt/xsdsequencegt
- lt/xsdcomplexTypegt
- ltxsdcomplexType name"student_type"gt
- ltxsdsequencegt
- ltxsdelement name"name" name"name_type"
/gt - lt/xsdsequencegt
- lt/xsdcomplexTypegt
- ltxsdcomplexType name"name_type"gt
- ltxsdsequencegt
41Order of the elements
- Sequence Elements appear in same order than in
Schema - All Elements can appear in any order
- Choice One element can appear from the
choice-list - ltxsdelement name"person"gt
- ltxsdcomplexTypegt
- ltxsdchoicegt
- ltxsdelement name"employee"
type"employee"/gt - ltxsdelement name"member" type"member"/gt
- lt/xsdchoicegt
- lt/xsdcomplexTypegt
- lt/xsdelementgt
42Attribute
- XML
- ltstudent id"A1"gt...lt/studentgt
- Schema
- ltxsdelement name"student" type"student_type"
/gt - ltxsdcomplexType name"student_type"gt
- ltxsdsequencegt
- ...
- lt/xsdsequencegt
- ltxsdattribute name"id" type"xsdID"/gt
- lt/xsdcomplexTypegt
43Empty Element with Attribute
- XML
- ltstudent id"A1" /gt
- Schema
- ltxsdelement name"student" type"student_type"
/gt - ltxsdcomplexType name"student_type"gt
- ltxsdattribute name"id" type"xsdID"/gt
- lt/xsdcomplexTypegt
44PHP5 and Schema
- With PHP5 you do not have to link xml to schema
files. - The linking is done in PHP5 code, not in XML.
- Example of schema-validation
- doc new domDocument
- if ( doc-gtload("books.xml") and
- doc-gtschemaValidate("books.xsd') )
-
- Â Â Â print "Is WellFormed and Schema-valid!"
-