XML's validation - XML Schema
-
Upload
videdegroup -
Category
Education
-
view
476 -
download
1
description
Transcript of XML's validation - XML Schema
Validation – XML Schemas
Nguyễn Đăng Khoa
Content
• Document Type Definitions (DTDs)• XML Schemas
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
XSD’s goals
• Check XML document is valid or not
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
XSD 1.0 - Specifications
• Part 1: Structures (http://www.w3.org/TR/xmlschema-1/)
• Part 2: Datatypes (http://www.w3.org/TR/xmlschema-2/)
XSD - Example
XSD – Namespace in XSD & XML
XSD – Explicitly namespace
XSD – Default namespace
XSD – XML doesn’t use namespace
URI to xsd
XSD – XML uses namespace
XSD – XML uses namespace
URI to xsdnamespace blank
XSD – XML uses namespace
XSD – XML uses namespace
XSD – Element and Attribute qualification
XSD – qualified vs. unqualified
qualified
unqualified
XSD – Global vs. Local
GlobalLocal
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.
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
XSD – elementFormDefault – Example
Valid
XSD – elementFormDefault – Example
Invalid
Element 'first': This element is not expected. Expected is ({http://www.example.com/name}first)
XSD – elementFormDefault – Example
Valid
XSD – elementFormDefault – Example
Invalid
Element '{http://www.example.com/name}first': This element is not expected. Expected is (first)
XSD – attributeFormDefault – Example
• Is same elementFormDefault• unqualified is default value
XSD – attributeFormDefault – Example
Valid
XSD – attributeFormDefault – Example
Invalid
XSD – attributeFormDefault – Example
Invalid
XSD – Content Models
• Is allowable content (type) of elements and attributes
• There are 2 content models– <complexType>– <simpleType>
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
XSD – <complexType> – Example
XSD – <complexType> – Example
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
XSD - <element>
XSD - <element> – Name
XSD - <element> – Type
XSD - <element> – Local Type
include the type declaration as a child of the element declaration
XSD - <element> – Global Type
XSD - <element> – Cardinality
XSD - <element> – Cardinality
Default: 1
XSD - <element> – Default value
XSD - <element> – Fixed value
XSD – Mixed Content
• Enable you to include both text and element content within a single content model
XSD – Simple Content
• Enable you to include only text within a single content model
XSD – Empty Content
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
XSD – Elements Wildcards
List of namespace URIs
XSD – Elements Wildcards
indicate that any element from the schema’s target namespace can be used
XSD – Elements Wildcards
indicate that elements not in any namespace can be used
XSD – Elements Wildcards
indicate that all elements from any namespace or no namespace are allowed
XSD – Elements Wildcards
indicate that elements from namespaces other than the schema’s target namespace can be used
XSD – Elements Wildcards
(Default) Elements represented by this <any> element must be declared. Furthermore, the element must be valid according to its declaration
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
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
XSD – Elements Wildcards – Rules
• The <any> declaration can appear only within a content model. • You are not allowed to create global <any> declarations
XSD – <group>
• Enable you to define reusable groups of elements
XSD – Exercise - No namespace
XSD – Exercise – Use namespace
XSD - <attribute>
XSD - <attributeGroup>
• Enable you to define reusable groups of attributes
XSD – Built-in Data Types
XSD – Built-in Data Types
XSD – Built-in Data Types
XSD – Built-in Data Types
XSD – Built-in Data Types
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
XSD – Exercise
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
XSD – User-Defined Data Types
prevent a simple type from being subtyped
XSD – Restriction Type
• is a subset of its base type
Facet
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
XSD – List Type
• create a list of items
XSD – Union Type
• allow potential values for elements and attributes to have any of several types
XSD – Union Type