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 Global Local
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 FacetsConstraint 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

XML's validation - XML Schema

  • 1.
    Validation – XMLSchemas Nguyễn Đăng Khoa
  • 2.
    Content • Document TypeDefinitions (DTDs) • XML Schemas
  • 3.
    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
  • 4.
    XSD’s goals • CheckXML document is valid or not
  • 5.
    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
  • 6.
    XSD 1.0 -Specifications • Part 1: Structures (http://www.w3.org/TR/xmlschema-1/) • Part 2: Datatypes (http://www.w3.org/TR/xmlschema-2/)
  • 7.
  • 8.
    XSD – Namespacein XSD & XML
  • 9.
  • 10.
    XSD – Defaultnamespace
  • 11.
    XSD – XMLdoesn’t use namespace URI to xsd
  • 12.
    XSD – XMLuses namespace
  • 13.
    XSD – XMLuses namespace URI to xsdnamespace blank
  • 14.
    XSD – XMLuses namespace
  • 15.
    XSD – XMLuses namespace
  • 16.
    XSD – Elementand Attribute qualification
  • 17.
    XSD – qualifiedvs. unqualified qualified unqualified
  • 18.
    XSD – Globalvs. Local Global Local
  • 19.
    XSD – Globalvs. 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.
  • 20.
    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
  • 21.
    XSD – elementFormDefault– Example Valid
  • 22.
    XSD – elementFormDefault– Example Invalid Element 'first': This element is not expected. Expected is ({http://www.example.com/name}first)
  • 23.
    XSD – elementFormDefault– Example Valid
  • 24.
    XSD – elementFormDefault– Example Invalid Element '{http://www.example.com/name}first': This element is not expected. Expected is (first)
  • 25.
    XSD – attributeFormDefault– Example • Is same elementFormDefault • unqualified is default value
  • 26.
  • 27.
    XSD – attributeFormDefault– Example Invalid
  • 28.
    XSD – attributeFormDefault– Example Invalid
  • 29.
    XSD – ContentModels • Is allowable content (type) of elements and attributes • There are 2 content models – <complexType> – <simpleType>
  • 30.
    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
  • 31.
  • 32.
  • 33.
    XSD – Ruleof <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
  • 34.
  • 35.
  • 36.
  • 37.
    XSD - <element>– Local Type include the type declaration as a child of the element declaration
  • 38.
    XSD - <element>– Global Type
  • 39.
    XSD - <element>– Cardinality
  • 40.
    XSD - <element>– Cardinality Default: 1
  • 41.
    XSD - <element>– Default value
  • 42.
    XSD - <element>– Fixed value
  • 43.
    XSD – MixedContent • Enable you to include both text and element content within a single content model
  • 44.
    XSD – SimpleContent • Enable you to include only text within a single content model
  • 45.
  • 46.
    XSD – ElementsWildcards • Suppose you want to specify that your element can contain any of the elements declared in your namespace, or any elements from another namespace
  • 47.
    XSD – ElementsWildcards List of namespace URIs
  • 48.
    XSD – ElementsWildcards indicate that any element from the schema’s target namespace can be used
  • 49.
    XSD – ElementsWildcards indicate that elements not in any namespace can be used
  • 50.
    XSD – ElementsWildcards indicate that all elements from any namespace or no namespace are allowed
  • 51.
    XSD – ElementsWildcards indicate that elements from namespaces other than the schema’s target namespace can be used
  • 52.
    XSD – ElementsWildcards (Default) Elements represented by this <any> element must be declared. Furthermore, the element must be valid according to its declaration
  • 53.
    XSD – ElementsWildcards Elements represented by this <any> element need not be declared in the schema and need not be valid even if they are declared
  • 54.
    XSD – ElementsWildcards Elements represented by this <any> element must be validated if they are declared, but must not be validated if they are declared
  • 55.
    XSD – ElementsWildcards – Rules • The <any> declaration can appear only within a content model. • You are not allowed to create global <any> declarations
  • 56.
    XSD – <group> •Enable you to define reusable groups of elements
  • 57.
    XSD – Exercise- No namespace
  • 58.
    XSD – Exercise– Use namespace
  • 59.
  • 60.
    XSD - <attributeGroup> •Enable you to define reusable groups of attributes
  • 61.
    XSD – Built-inData Types
  • 62.
    XSD – Built-inData Types
  • 63.
    XSD – Built-inData Types
  • 64.
    XSD – Built-inData Types
  • 65.
    XSD – Built-inData Types
  • 66.
    XSD – Built-inData 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
  • 67.
  • 68.
    XSD – User-DefinedData 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
  • 69.
    XSD – User-DefinedData Types prevent a simple type from being subtyped
  • 70.
    XSD – RestrictionType • is a subset of its base type Facet
  • 71.
    XSD – RestrictionType – Constraining FacetsConstraint 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
  • 72.
    XSD – ListType • create a list of items
  • 73.
    XSD – UnionType • allow potential values for elements and attributes to have any of several types
  • 74.

Editor's Notes

  • #2 XML trong OOP
  • #4 They describe the structure and content of XML documents in more detail than DTDsXML Schemas are a mature technology used in a variety of XML applications.Apart from their use in validation, aspects of XML Schemas are used in various other XMLtechnologies such as XQuery and SOAP
  • #8 XSD is xml
  • #9 XSD is xml
  • #12 XSD is xml
  • #13 XSD is xml
  • #14 XSD is xml
  • #15 XSD is xml
  • #16 XSD is xml
  • #23 ERROR: Element &apos;first&apos;: This element is not expected. Expected is ( {http://www.example.com/name}first ).
  • #25 ERROR: Element &apos;{http://www.example.com/name}first&apos;: This element is not expected. Expected is ( first ).
  • #30 Attribute just use &lt;simpleType&gt;
  • #39 Even though the type is global, it is still part of the target namespace. Therefore, when referring to the type, you must include the target namespace prefix (if any). This example used the prefix target to refer to the target namespace
  • #40 maxOccurs must be greater than or equal to the value for minOccursyou may want to reuse entire element declarations instead of just the type
  • #41 maxOccurs must be greater than or equal to the value for minOccursyou may want to reuse entire element declarations instead of just the type
  • #42 maxOccurs must be greater than or equal to the value for minOccurs
  • #43 maxOccurs must be greater than or equal to the value for minOccurs
  • #46 To declare an empty content model in a &lt;complexType&gt; definition, you simply create the&lt;complexType&gt; definition without any &lt;element&gt; or content model declarations.
  • #52 ##any Enables elements from all namespaces to be included as part of the wildcard.##other Enables elements from namespaces other than the targetNamespace to be included as part of the wildcard.##targetNamespace Enables elements from only the targetNamespace to be included as part of the wildcard.##local Enables any well-formed elements that are not qualified by a namespace to be included as part of the wildcard.Whitespace-separated Enables elements from any listed namespaces to be included as part of the list of allowable wildcards. Possible list values also include ##targetNamespace namespace URIs and ##local
  • #69 Because you must derive every &lt;simpleType&gt; definition from another data type, A simpleType declaration contained within a complexType or attribute element defines that simple type within the scope of the complexType or attribute that contains it. If a simpleType declaration has the schema element as its parent, it has global scope within that schema.
  • #70 The final attribute can be used to prevent a simple type from being subtyped. If finalcontains the value list, the type cannot be extended by listing. If final contains thevalue restriction, the type cannot be extended by restriction. If final contains thevalue union, the type cannot become a member of a union. These three values can becombined in a whitespace-separated list. For instance, final=&quot;list union&quot; preventsderivation by list and union but not by restriction. If final has the value #all, the typecannot be used as a base type in any way.
  • #71 EnumerationsNumeric (http://msdn.microsoft.com/en-us/library/ms256050(v=vs.110).aspx)LengthForcing pattern
  • #73  a simple type (listOfDates) that allows a list of dates (each list item date must be separated by white space) as its contents.http://msdn.microsoft.com/en-us/library/ms256050(v=vs.110).aspx
  • #74 The value of the memberTypes attribute should be a whitespaceseparatedlist of references to global &lt;simpleType&gt; definitions or built-in XML Schema datatypes
  • #75 http://msdn.microsoft.com/en-us/library/ms256050(v=vs.110).aspx