XML Schema (W3C) - PowerPoint PPT Presentation

1 / 44
About This Presentation
Title:

XML Schema (W3C)

Description:

XML Schema (W3C) Thanks to Jussi Pohjolainen TAMK University of Applied Sciences Example: XSD file Named ComplexType – PowerPoint PPT presentation

Number of Views:112
Avg rating:3.0/5.0
Slides: 45
Provided by: Marco390
Category:
Tags: w3c | xml | schema

less

Transcript and Presenter's Notes

Title: XML Schema (W3C)


1
XML Schema (W3C)
  • Thanks to Jussi Pohjolainen
  • TAMK University of Applied Sciences

2
XML NAMESPACES
  • w3schools.com

3
XML 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!
4
Solving 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
5
xmlns - 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.

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

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

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

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

11
W3C SCHEMA
12
XML 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

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

14
DTD 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.
15
DTD 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.
16
Schema 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.
17
Linking?
  • 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

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

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

20
Let'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?
21
The 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

22
Usage of Datatypes
  • ltxsdelement name"firstname"
  • type"xsdstring" /gt
  • ltxsdelement name"ableToSwim"
  • type"xsdboolean" /gt
  • ltxsdelement name"date"
  • type"xsddate" /gt

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

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

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

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

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

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

29
SimpleType pattern
  • Using REGEX
  • ltxsdsimpleType name"lowercase_char"gt
  • ltxsdrestriction base"xsdstring"gt
  • ltxsdpattern value"a-z"/gt
  • lt/xsdrestrictiongt
  • lt/xsdsimpleTypegt

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

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

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

33
Example XML - File
  • lt?xml version"1.0"?gt
  • ltstudentsgt
  • ltfirstnamegtPekkalt/firstnamegt
  • ltlastnamegtVirtanenlt/lastnamegt
  • lt/studentsgt

34
Example 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)
35
Example 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

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

37
Deep Structure in XML - File
  • lt?xml version"1.0"?gt
  • ltstudentsgt
  • ltstudentgt
  • ltnamegt
  • ltfirstnamegtPekkalt/firstnamegt
  • lt/namegt
  • lt/studentgt
  • lt/studentsgt

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

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

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

42
Attribute
  • 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

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

44
PHP5 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!"
Write a Comment
User Comments (0)
About PowerShow.com