XML's validation - XML Schema
-
Upload
videdegroup -
Category
Education
-
view
462 -
download
1
description
Transcript of XML's validation - XML Schema
![Page 1: XML's validation - XML Schema](https://reader030.fdocuments.in/reader030/viewer/2022020717/546b389caf795902048b5eb3/html5/thumbnails/1.jpg)
Validation – XML Schemas
Nguyễn Đăng Khoa
![Page 2: XML's validation - XML Schema](https://reader030.fdocuments.in/reader030/viewer/2022020717/546b389caf795902048b5eb3/html5/thumbnails/2.jpg)
Content
• Document Type Definitions (DTDs)• XML Schemas
![Page 3: XML's validation - XML Schema](https://reader030.fdocuments.in/reader030/viewer/2022020717/546b389caf795902048b5eb3/html5/thumbnails/3.jpg)
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](https://reader030.fdocuments.in/reader030/viewer/2022020717/546b389caf795902048b5eb3/html5/thumbnails/4.jpg)
XSD’s goals
• Check XML document is valid or not
![Page 5: XML's validation - XML Schema](https://reader030.fdocuments.in/reader030/viewer/2022020717/546b389caf795902048b5eb3/html5/thumbnails/5.jpg)
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](https://reader030.fdocuments.in/reader030/viewer/2022020717/546b389caf795902048b5eb3/html5/thumbnails/6.jpg)
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](https://reader030.fdocuments.in/reader030/viewer/2022020717/546b389caf795902048b5eb3/html5/thumbnails/7.jpg)
XSD - Example
![Page 8: XML's validation - XML Schema](https://reader030.fdocuments.in/reader030/viewer/2022020717/546b389caf795902048b5eb3/html5/thumbnails/8.jpg)
XSD – Namespace in XSD & XML
![Page 9: XML's validation - XML Schema](https://reader030.fdocuments.in/reader030/viewer/2022020717/546b389caf795902048b5eb3/html5/thumbnails/9.jpg)
XSD – Explicitly namespace
![Page 10: XML's validation - XML Schema](https://reader030.fdocuments.in/reader030/viewer/2022020717/546b389caf795902048b5eb3/html5/thumbnails/10.jpg)
XSD – Default namespace
![Page 11: XML's validation - XML Schema](https://reader030.fdocuments.in/reader030/viewer/2022020717/546b389caf795902048b5eb3/html5/thumbnails/11.jpg)
XSD – XML doesn’t use namespace
URI to xsd
![Page 12: XML's validation - XML Schema](https://reader030.fdocuments.in/reader030/viewer/2022020717/546b389caf795902048b5eb3/html5/thumbnails/12.jpg)
XSD – XML uses namespace
![Page 13: XML's validation - XML Schema](https://reader030.fdocuments.in/reader030/viewer/2022020717/546b389caf795902048b5eb3/html5/thumbnails/13.jpg)
XSD – XML uses namespace
URI to xsdnamespace blank
![Page 14: XML's validation - XML Schema](https://reader030.fdocuments.in/reader030/viewer/2022020717/546b389caf795902048b5eb3/html5/thumbnails/14.jpg)
XSD – XML uses namespace
![Page 15: XML's validation - XML Schema](https://reader030.fdocuments.in/reader030/viewer/2022020717/546b389caf795902048b5eb3/html5/thumbnails/15.jpg)
XSD – XML uses namespace
![Page 16: XML's validation - XML Schema](https://reader030.fdocuments.in/reader030/viewer/2022020717/546b389caf795902048b5eb3/html5/thumbnails/16.jpg)
XSD – Element and Attribute qualification
![Page 17: XML's validation - XML Schema](https://reader030.fdocuments.in/reader030/viewer/2022020717/546b389caf795902048b5eb3/html5/thumbnails/17.jpg)
XSD – qualified vs. unqualified
qualified
unqualified
![Page 18: XML's validation - XML Schema](https://reader030.fdocuments.in/reader030/viewer/2022020717/546b389caf795902048b5eb3/html5/thumbnails/18.jpg)
XSD – Global vs. Local
GlobalLocal
![Page 19: XML's validation - XML Schema](https://reader030.fdocuments.in/reader030/viewer/2022020717/546b389caf795902048b5eb3/html5/thumbnails/19.jpg)
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](https://reader030.fdocuments.in/reader030/viewer/2022020717/546b389caf795902048b5eb3/html5/thumbnails/20.jpg)
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](https://reader030.fdocuments.in/reader030/viewer/2022020717/546b389caf795902048b5eb3/html5/thumbnails/21.jpg)
XSD – elementFormDefault – Example
Valid
![Page 22: XML's validation - XML Schema](https://reader030.fdocuments.in/reader030/viewer/2022020717/546b389caf795902048b5eb3/html5/thumbnails/22.jpg)
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](https://reader030.fdocuments.in/reader030/viewer/2022020717/546b389caf795902048b5eb3/html5/thumbnails/23.jpg)
XSD – elementFormDefault – Example
Valid
![Page 24: XML's validation - XML Schema](https://reader030.fdocuments.in/reader030/viewer/2022020717/546b389caf795902048b5eb3/html5/thumbnails/24.jpg)
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](https://reader030.fdocuments.in/reader030/viewer/2022020717/546b389caf795902048b5eb3/html5/thumbnails/25.jpg)
XSD – attributeFormDefault – Example
• Is same elementFormDefault• unqualified is default value
![Page 26: XML's validation - XML Schema](https://reader030.fdocuments.in/reader030/viewer/2022020717/546b389caf795902048b5eb3/html5/thumbnails/26.jpg)
XSD – attributeFormDefault – Example
Valid
![Page 27: XML's validation - XML Schema](https://reader030.fdocuments.in/reader030/viewer/2022020717/546b389caf795902048b5eb3/html5/thumbnails/27.jpg)
XSD – attributeFormDefault – Example
Invalid
![Page 28: XML's validation - XML Schema](https://reader030.fdocuments.in/reader030/viewer/2022020717/546b389caf795902048b5eb3/html5/thumbnails/28.jpg)
XSD – attributeFormDefault – Example
Invalid
![Page 29: XML's validation - XML Schema](https://reader030.fdocuments.in/reader030/viewer/2022020717/546b389caf795902048b5eb3/html5/thumbnails/29.jpg)
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](https://reader030.fdocuments.in/reader030/viewer/2022020717/546b389caf795902048b5eb3/html5/thumbnails/30.jpg)
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](https://reader030.fdocuments.in/reader030/viewer/2022020717/546b389caf795902048b5eb3/html5/thumbnails/31.jpg)
XSD – <complexType> – Example
![Page 32: XML's validation - XML Schema](https://reader030.fdocuments.in/reader030/viewer/2022020717/546b389caf795902048b5eb3/html5/thumbnails/32.jpg)
XSD – <complexType> – Example
![Page 33: XML's validation - XML Schema](https://reader030.fdocuments.in/reader030/viewer/2022020717/546b389caf795902048b5eb3/html5/thumbnails/33.jpg)
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](https://reader030.fdocuments.in/reader030/viewer/2022020717/546b389caf795902048b5eb3/html5/thumbnails/34.jpg)
XSD - <element>
![Page 35: XML's validation - XML Schema](https://reader030.fdocuments.in/reader030/viewer/2022020717/546b389caf795902048b5eb3/html5/thumbnails/35.jpg)
XSD - <element> – Name
![Page 36: XML's validation - XML Schema](https://reader030.fdocuments.in/reader030/viewer/2022020717/546b389caf795902048b5eb3/html5/thumbnails/36.jpg)
XSD - <element> – Type
![Page 37: XML's validation - XML Schema](https://reader030.fdocuments.in/reader030/viewer/2022020717/546b389caf795902048b5eb3/html5/thumbnails/37.jpg)
XSD - <element> – Local Type
include the type declaration as a child of the element declaration
![Page 38: XML's validation - XML Schema](https://reader030.fdocuments.in/reader030/viewer/2022020717/546b389caf795902048b5eb3/html5/thumbnails/38.jpg)
XSD - <element> – Global Type
![Page 39: XML's validation - XML Schema](https://reader030.fdocuments.in/reader030/viewer/2022020717/546b389caf795902048b5eb3/html5/thumbnails/39.jpg)
XSD - <element> – Cardinality
![Page 40: XML's validation - XML Schema](https://reader030.fdocuments.in/reader030/viewer/2022020717/546b389caf795902048b5eb3/html5/thumbnails/40.jpg)
XSD - <element> – Cardinality
Default: 1
![Page 41: XML's validation - XML Schema](https://reader030.fdocuments.in/reader030/viewer/2022020717/546b389caf795902048b5eb3/html5/thumbnails/41.jpg)
XSD - <element> – Default value
![Page 42: XML's validation - XML Schema](https://reader030.fdocuments.in/reader030/viewer/2022020717/546b389caf795902048b5eb3/html5/thumbnails/42.jpg)
XSD - <element> – Fixed value
![Page 43: XML's validation - XML Schema](https://reader030.fdocuments.in/reader030/viewer/2022020717/546b389caf795902048b5eb3/html5/thumbnails/43.jpg)
XSD – Mixed Content
• Enable you to include both text and element content within a single content model
![Page 44: XML's validation - XML Schema](https://reader030.fdocuments.in/reader030/viewer/2022020717/546b389caf795902048b5eb3/html5/thumbnails/44.jpg)
XSD – Simple Content
• Enable you to include only text within a single content model
![Page 45: XML's validation - XML Schema](https://reader030.fdocuments.in/reader030/viewer/2022020717/546b389caf795902048b5eb3/html5/thumbnails/45.jpg)
XSD – Empty Content
![Page 46: XML's validation - XML Schema](https://reader030.fdocuments.in/reader030/viewer/2022020717/546b389caf795902048b5eb3/html5/thumbnails/46.jpg)
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](https://reader030.fdocuments.in/reader030/viewer/2022020717/546b389caf795902048b5eb3/html5/thumbnails/47.jpg)
XSD – Elements Wildcards
List of namespace URIs
![Page 48: XML's validation - XML Schema](https://reader030.fdocuments.in/reader030/viewer/2022020717/546b389caf795902048b5eb3/html5/thumbnails/48.jpg)
XSD – Elements Wildcards
indicate that any element from the schema’s target namespace can be used
![Page 49: XML's validation - XML Schema](https://reader030.fdocuments.in/reader030/viewer/2022020717/546b389caf795902048b5eb3/html5/thumbnails/49.jpg)
XSD – Elements Wildcards
indicate that elements not in any namespace can be used
![Page 50: XML's validation - XML Schema](https://reader030.fdocuments.in/reader030/viewer/2022020717/546b389caf795902048b5eb3/html5/thumbnails/50.jpg)
XSD – Elements Wildcards
indicate that all elements from any namespace or no namespace are allowed
![Page 51: XML's validation - XML Schema](https://reader030.fdocuments.in/reader030/viewer/2022020717/546b389caf795902048b5eb3/html5/thumbnails/51.jpg)
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](https://reader030.fdocuments.in/reader030/viewer/2022020717/546b389caf795902048b5eb3/html5/thumbnails/52.jpg)
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](https://reader030.fdocuments.in/reader030/viewer/2022020717/546b389caf795902048b5eb3/html5/thumbnails/53.jpg)
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](https://reader030.fdocuments.in/reader030/viewer/2022020717/546b389caf795902048b5eb3/html5/thumbnails/54.jpg)
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](https://reader030.fdocuments.in/reader030/viewer/2022020717/546b389caf795902048b5eb3/html5/thumbnails/55.jpg)
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](https://reader030.fdocuments.in/reader030/viewer/2022020717/546b389caf795902048b5eb3/html5/thumbnails/56.jpg)
XSD – <group>
• Enable you to define reusable groups of elements
![Page 57: XML's validation - XML Schema](https://reader030.fdocuments.in/reader030/viewer/2022020717/546b389caf795902048b5eb3/html5/thumbnails/57.jpg)
XSD – Exercise - No namespace
![Page 58: XML's validation - XML Schema](https://reader030.fdocuments.in/reader030/viewer/2022020717/546b389caf795902048b5eb3/html5/thumbnails/58.jpg)
XSD – Exercise – Use namespace
![Page 59: XML's validation - XML Schema](https://reader030.fdocuments.in/reader030/viewer/2022020717/546b389caf795902048b5eb3/html5/thumbnails/59.jpg)
XSD - <attribute>
![Page 60: XML's validation - XML Schema](https://reader030.fdocuments.in/reader030/viewer/2022020717/546b389caf795902048b5eb3/html5/thumbnails/60.jpg)
XSD - <attributeGroup>
• Enable you to define reusable groups of attributes
![Page 61: XML's validation - XML Schema](https://reader030.fdocuments.in/reader030/viewer/2022020717/546b389caf795902048b5eb3/html5/thumbnails/61.jpg)
XSD – Built-in Data Types
![Page 62: XML's validation - XML Schema](https://reader030.fdocuments.in/reader030/viewer/2022020717/546b389caf795902048b5eb3/html5/thumbnails/62.jpg)
XSD – Built-in Data Types
![Page 63: XML's validation - XML Schema](https://reader030.fdocuments.in/reader030/viewer/2022020717/546b389caf795902048b5eb3/html5/thumbnails/63.jpg)
XSD – Built-in Data Types
![Page 64: XML's validation - XML Schema](https://reader030.fdocuments.in/reader030/viewer/2022020717/546b389caf795902048b5eb3/html5/thumbnails/64.jpg)
XSD – Built-in Data Types
![Page 65: XML's validation - XML Schema](https://reader030.fdocuments.in/reader030/viewer/2022020717/546b389caf795902048b5eb3/html5/thumbnails/65.jpg)
XSD – Built-in Data Types
![Page 66: XML's validation - XML Schema](https://reader030.fdocuments.in/reader030/viewer/2022020717/546b389caf795902048b5eb3/html5/thumbnails/66.jpg)
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](https://reader030.fdocuments.in/reader030/viewer/2022020717/546b389caf795902048b5eb3/html5/thumbnails/67.jpg)
XSD – Exercise
![Page 68: XML's validation - XML Schema](https://reader030.fdocuments.in/reader030/viewer/2022020717/546b389caf795902048b5eb3/html5/thumbnails/68.jpg)
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](https://reader030.fdocuments.in/reader030/viewer/2022020717/546b389caf795902048b5eb3/html5/thumbnails/69.jpg)
XSD – User-Defined Data Types
prevent a simple type from being subtyped
![Page 70: XML's validation - XML Schema](https://reader030.fdocuments.in/reader030/viewer/2022020717/546b389caf795902048b5eb3/html5/thumbnails/70.jpg)
XSD – Restriction Type
• is a subset of its base type
Facet
![Page 71: XML's validation - XML Schema](https://reader030.fdocuments.in/reader030/viewer/2022020717/546b389caf795902048b5eb3/html5/thumbnails/71.jpg)
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](https://reader030.fdocuments.in/reader030/viewer/2022020717/546b389caf795902048b5eb3/html5/thumbnails/72.jpg)
XSD – List Type
• create a list of items
![Page 73: XML's validation - XML Schema](https://reader030.fdocuments.in/reader030/viewer/2022020717/546b389caf795902048b5eb3/html5/thumbnails/73.jpg)
XSD – Union Type
• allow potential values for elements and attributes to have any of several types
![Page 74: XML's validation - XML Schema](https://reader030.fdocuments.in/reader030/viewer/2022020717/546b389caf795902048b5eb3/html5/thumbnails/74.jpg)
XSD – Union Type