XML's validation - XML Schema

74
Validation – XML Schemas Nguyễn Đăng Khoa

description

Content: - Structures - Datatypes References: - Beginning XML, 5th Edition, Joe Fawcett, Liam R. E. Quin, Danny Ayers - XML in a nutshell,3rd Edition, Elliotte Rusty Harold & W. Scott Means - http://www.w3schools.com/

Transcript of XML's validation - XML Schema

Page 1: XML's validation - XML Schema

Validation – XML Schemas

Nguyễn Đăng Khoa

Page 2: XML's validation - XML Schema

Content

• Document Type Definitions (DTDs)• XML Schemas

Page 3: XML's validation - XML Schema

XML Schemas (XSD)

• Define XML vocabularies and grammars• Describe the structure and content of XML

documents in more detail than DTDs• XML Schema 1.0 specifications were first

published by the W3C in 2001, with a second edition following in 2004

• XML Schema 1.1 became a W3C Recommendation in April 2012

Page 4: XML's validation - XML Schema

XSD’s goals

• Check XML document is valid or not

Page 5: XML's validation - XML Schema

XSD – Benefits

• Are created using basic XML• Fully support the Namespace Recommendation• Validate text element content based on built-in

and user-defined data types• Create complex and reusable content models• Enable the modeling of programming concepts

such as object inheritance and type substitution

Page 6: XML's validation - XML Schema

XSD 1.0 - Specifications

• Part 1: Structures (http://www.w3.org/TR/xmlschema-1/)

• Part 2: Datatypes (http://www.w3.org/TR/xmlschema-2/)

Page 7: XML's validation - XML Schema

XSD - Example

Page 8: XML's validation - XML Schema

XSD – Namespace in XSD & XML

Page 9: XML's validation - XML Schema

XSD – Explicitly namespace

Page 10: XML's validation - XML Schema

XSD – Default namespace

Page 11: XML's validation - XML Schema

XSD – XML doesn’t use namespace

URI to xsd

Page 12: XML's validation - XML Schema

XSD – XML uses namespace

Page 13: XML's validation - XML Schema

XSD – XML uses namespace

URI to xsdnamespace blank

Page 14: XML's validation - XML Schema

XSD – XML uses namespace

Page 15: XML's validation - XML Schema

XSD – XML uses namespace

Page 16: XML's validation - XML Schema

XSD – Element and Attribute qualification

Page 17: XML's validation - XML Schema

XSD – qualified vs. unqualified

qualified

unqualified

Page 18: XML's validation - XML Schema

XSD – Global vs. Local

GlobalLocal

Page 19: XML's validation - XML Schema

XSD – Global vs. Local

• Global declarations are declarations that appear as direct children of the <schema> element. Global element declarations can be reused throughout the XML Schema.

• Local declarations do not have the <schema> element as their direct parent and can be used only in their specific context.

Page 20: XML's validation - XML Schema

XSD – elementFormDefault

• qualified: nested elements must belong to the target namespace of the schema

• unqualified is default value, a mix of qualified and unqualified elements

Page 21: XML's validation - XML Schema

XSD – elementFormDefault – Example

Valid

Page 22: XML's validation - XML Schema

XSD – elementFormDefault – Example

Invalid

Element 'first': This element is not expected. Expected is ({http://www.example.com/name}first)

Page 23: XML's validation - XML Schema

XSD – elementFormDefault – Example

Valid

Page 24: XML's validation - XML Schema

XSD – elementFormDefault – Example

Invalid

Element '{http://www.example.com/name}first': This element is not expected. Expected is (first)

Page 25: XML's validation - XML Schema

XSD – attributeFormDefault – Example

• Is same elementFormDefault• unqualified is default value

Page 26: XML's validation - XML Schema

XSD – attributeFormDefault – Example

Valid

Page 27: XML's validation - XML Schema

XSD – attributeFormDefault – Example

Invalid

Page 28: XML's validation - XML Schema

XSD – attributeFormDefault – Example

Invalid

Page 29: XML's validation - XML Schema

XSD – Content Models

• Is allowable content (type) of elements and attributes

• There are 2 content models– <complexType>– <simpleType>

Page 30: XML's validation - XML Schema

XSD - <complexType>

• Defines types that contain attributes or elements

• There are 3 ways of interpreting a list of elements– <sequence>: Elements must appear in the given

order– <choice>: Only one of the elements in the list may

appear– <all>: Elements can appear in any order, with each

child element occurring zero or one time

Page 31: XML's validation - XML Schema

XSD – <complexType> – Example

Page 32: XML's validation - XML Schema

XSD – <complexType> – Example

Page 33: XML's validation - XML Schema

XSD – Rule of <all>

• The <all> declaration must be the only content model declaration that appears as a child of a <complexType> definition

• The <all> declaration can contain only <element> declarations as its children – No <sequence>, <choice>, <group> in it

• The <all> declaration’s children may appear once each in the instance document

Page 34: XML's validation - XML Schema

XSD - <element>

Page 35: XML's validation - XML Schema

XSD - <element> – Name

Page 36: XML's validation - XML Schema

XSD - <element> – Type

Page 37: XML's validation - XML Schema

XSD - <element> – Local Type

include the type declaration as a child of the element declaration

Page 38: XML's validation - XML Schema

XSD - <element> – Global Type

Page 39: XML's validation - XML Schema

XSD - <element> – Cardinality

Page 40: XML's validation - XML Schema

XSD - <element> – Cardinality

Default: 1

Page 41: XML's validation - XML Schema

XSD - <element> – Default value

Page 42: XML's validation - XML Schema

XSD - <element> – Fixed value

Page 43: XML's validation - XML Schema

XSD – Mixed Content

• Enable you to include both text and element content within a single content model

Page 44: XML's validation - XML Schema

XSD – Simple Content

• Enable you to include only text within a single content model

Page 45: XML's validation - XML Schema

XSD – Empty Content

Page 46: XML's validation - XML Schema

XSD – Elements Wildcards

• Suppose you want to specify that your element can contain any of the elements declared in your namespace, or any elements from another namespace

Page 47: XML's validation - XML Schema

XSD – Elements Wildcards

List of namespace URIs

Page 48: XML's validation - XML Schema

XSD – Elements Wildcards

indicate that any element from the schema’s target namespace can be used

Page 49: XML's validation - XML Schema

XSD – Elements Wildcards

indicate that elements not in any namespace can be used

Page 50: XML's validation - XML Schema

XSD – Elements Wildcards

indicate that all elements from any namespace or no namespace are allowed

Page 51: XML's validation - XML Schema

XSD – Elements Wildcards

indicate that elements from namespaces other than the schema’s target namespace can be used

Page 52: XML's validation - XML Schema

XSD – Elements Wildcards

(Default) Elements represented by this <any> element must be declared. Furthermore, the element must be valid according to its declaration

Page 53: XML's validation - XML Schema

XSD – Elements Wildcards

Elements represented by this <any> element need not be declared in the schema and need not be valid even if they are declared

Page 54: XML's validation - XML Schema

XSD – Elements Wildcards

Elements represented by this <any> element must be validated if they are declared, but must not be validated if they are declared

Page 55: XML's validation - XML Schema

XSD – Elements Wildcards – Rules

• The <any> declaration can appear only within a content model. • You are not allowed to create global <any> declarations

Page 56: XML's validation - XML Schema

XSD – <group>

• Enable you to define reusable groups of elements

Page 57: XML's validation - XML Schema

XSD – Exercise - No namespace

Page 58: XML's validation - XML Schema

XSD – Exercise – Use namespace

Page 59: XML's validation - XML Schema

XSD - <attribute>

Page 60: XML's validation - XML Schema

XSD - <attributeGroup>

• Enable you to define reusable groups of attributes

Page 61: XML's validation - XML Schema

XSD – Built-in Data Types

Page 62: XML's validation - XML Schema

XSD – Built-in Data Types

Page 63: XML's validation - XML Schema

XSD – Built-in Data Types

Page 64: XML's validation - XML Schema

XSD – Built-in Data Types

Page 65: XML's validation - XML Schema

XSD – Built-in Data Types

Page 66: XML's validation - XML Schema

XSD – Built-in Data Types

• In addition to the types listed, the XML Schema Recommendation also allows the types defined within the XML Recommendation– ID– IDREF– IDREFS– ENTITY– ENTITIES– NOTATION– NMTOKEN– NMTOKENS

Page 67: XML's validation - XML Schema

XSD – Exercise

Page 68: XML's validation - XML Schema

XSD – User-Defined Data Types

• When you declare a <simpleType>, you must always base your declaration on an existing data type– Built-in– Custom

• <simpleType> definitions are often called derived types. There are three primary derived types:– Restriction types– List types– Union types

Page 69: XML's validation - XML Schema

XSD – User-Defined Data Types

prevent a simple type from being subtyped

Page 70: XML's validation - XML Schema

XSD – Restriction Type

• is a subset of its base type

Facet

Page 71: XML's validation - XML Schema

XSD – Restriction Type – Constraining Facets

Constraint Description

enumeration Defines a list of acceptable values

fractionDigits Specifies the maximum number of decimal places allowed. Must be equal to or greater than zero

length Specifies the exact number of characters or list items allowed. Must be equal to or greater than zero

maxExclusive Specifies the upper bounds for numeric values (the value must be less than this value)

maxInclusive Specifies the upper bounds for numeric values (the value must be less than or equal to this value)

maxLength Specifies the maximum number of characters or list items allowed. Must be equal to or greater than zero

minExclusive Specifies the lower bounds for numeric values (the value must be greater than this value)

minInclusive Specifies the lower bounds for numeric values (the value must be greater than or equal to this value)

minLength Specifies the minimum number of characters or list items allowed. Must be equal to or greater than zero

pattern Defines the exact sequence of characters that are acceptable

totalDigits Specifies the exact number of digits allowed. Must be greater than zero

whiteSpace Specifies how white space (line feeds, tabs, spaces, and carriage returns) is handled

Ref: http://www.w3schools.com/schema/schema_facets.asp

Page 72: XML's validation - XML Schema

XSD – List Type

• create a list of items

Page 73: XML's validation - XML Schema

XSD – Union Type

• allow potential values for elements and attributes to have any of several types

Page 74: XML's validation - XML Schema

XSD – Union Type