XML Schema (W3C) Jussi Pohjolainen TAMK University of Applied Sciences
XML NAMESPACES w3schools.com
XML Namespaces The idea behing XML namespaces is to avoid  element name conflicts . Example of name conflict (w3schools.com) <table> <tr> <td>Apples</td> <td>Bananas</td> </tr> </table> <table> <name>African Coffee Table</name> <width>80</width> <length>120</length> </table> Same tag-name, different content and meaning!
Solving Name Conflict <h:table> <h:tr> <h:td>Apples</h:td> <h:td>Bananas</h:td> </h:tr> </h:table> <f:table> <f:name>African Coffee Table</f:name> <f:width>80</f:width> <f:length>120</f:length> </f:table> Prefix  h  has xhtml-related elements and prefix  f  has furniture-related elements
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.
xmlns - attribute <root> <h:table  xmlns:h=&quot;http://www.w3.org/TR/html4/&quot; > <h:tr> <h:td>Apples</h:td> <h:td>Bananas</h:td> </h:tr> </h:table> <f:table  xmlns:f=&quot;http://www.w3schools.com/furniture&quot; > <f:name>African Coffee Table</f:name> <f:width>80</f:width> <f:length>120</f:length> </f:table> </root>
xmlns - attribute <root xmlns:h=&quot;http://www.w3.org/TR/html4/&quot; xmlns:f=&quot;http://www.w3schools.com/furniture&quot; > <h:table> <h:tr> <h:td>Apples</h:td> <h:td>Bananas</h:td> </h:tr> </h:table> <f:table> <f:name>African Coffee Table</f:name> <f:width>80</f:width> <f:length>120</f:length> </f:table> </root>
Namespace name The name of namespace should be unique:   <h:table xmlns:h= &quot;http://www.w3.org/TR/html4/ &quot;> It is just a string, but it should be declared as URI. Using URI  reduces  the possibility of different namespaces using  duplicate identifiers .
Example:  An XHTML + MathML + SVG Profile An XHTML+MathML+SVG 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.
<?xml version=&quot;1.0&quot;?> <!DOCTYPE html PUBLIC &quot;-//W3C//DTD XHTML 1.1 plus MathML 2.0 plus SVG 1.1//EN&quot; &quot;http://www.w3.org/2002/04/xhtml-math-svg/xhtml-math-svg-flat.dtd&quot;> <html xmlns  = &quot;http://www.w3.org/1999/xhtml&quot; xmlns:svg  = &quot;http://www.w3.org/2000/svg&quot;> <head> <title>Example of XHTML, SVG and MathML</title>  </head> <body> <h2>MathML</h2> <p> <math xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot;> <mfrac> <mi>a</mi> <mi>b</mi> </mfrac> </math> </p> <h2>SVG</h2> <p> <svg:svg width=&quot;50px&quot; height=&quot;50px&quot;> <svg:circle cx=&quot;25px&quot; cy=&quot;25px&quot; r=&quot;20px&quot; fill=&quot;green&quot;/> </svg:svg> </p> </body> </html>
W3C SCHEMA
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
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
DTD Linking Defines the structure, tag names and order for all xhtml - documents W3C has created XML-language &quot;XHTML&quot; by defining it's rules in DTD.
DTD Linking Defines the structure, tag names and order for all &quot;book&quot;- documents TAMK has created XML-language &quot;Book&quot; by defining it's rules in DTD.
Schema Linking Defines the structure, tag names and order for all &quot;book&quot;- documents TAMK has created XML-language &quot;Book&quot; by defining it's rules in DTD.
Linking? The basic idea with linking to Schema: <?xml version=&quot;1.0&quot;?> <root  schemaLocation=&quot;note.xsd&quot; > <foo>...</foo> </root> The problem with this is that now it is set that attribute &quot;schemaLocation&quot; is part of your XML-language
Linking and Namespace Usage Linking with namespace <?xml version=&quot;1.0&quot;?> <root xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot; xsi:schemaLocation=&quot;note.xsd&quot; > <foo>...</foo> </root> Now the &quot;schemaLocation&quot;  –  attribute is in it's own namespaces (xsi) and does not belong to the &quot;main&quot; language.
Simple Schema <?xml version=&quot;1.0&quot;?> <xsd:schema xmlns:xsd=&quot;http://www.w3.org/2001/XMLSchema&quot;> <xsd:element name=&quot;koululainen&quot; type=&quot;koululaiset_tyyppi&quot;/> <xsd:complexType name=&quot;koululaiset_tyyppi&quot;> <xsd:sequence> <xsd:element name=&quot;etunimi&quot; type=&quot;xsd:string&quot;/> <xsd:element name=&quot;sukunimi&quot; type=&quot;xsd:string&quot;/> </xsd:sequence> </xsd:complexType> </xsd:schema>
Let's remove namespaces... <?xml version=&quot;1.0&quot;?> <schema> <element name=&quot;koululainen&quot; type=&quot;koululaiset_tyyppi&quot;/> <complexType name=&quot;koululaiset_tyyppi&quot;> <sequence> <element name=&quot;etunimi&quot;  type=&quot;string&quot;/> <element name=&quot;sukunimi&quot; type=&quot;string&quot;/> </sequence> </complexType> </schema> It doesn't look so confusing after all?
The Basics: Element You define the name for the elements by using  element -element.   <element name=&quot;foo&quot; type=&quot;bar&quot; /> Type? 44 Built-in schema datatypes string, double, time, date, etc. See all the datatypes
Usage of Datatypes <xsd:element name=&quot;firstname&quot;  type=&quot;xsd:string&quot; /> <xsd:element name=&quot;ableToSwim&quot;  type=&quot;xsd:boolean&quot; /> <xsd:element name=&quot;date&quot;  type=&quot;xsd:date&quot; />
minOccurs and maxOccurs The amount of elements In DTD: *, ?, + In Schema: minOccurs, maxOccurs Example <xsd:element name=&quot;date&quot; type=&quot;xsd:date&quot; minOccurs=&quot;1&quot; maxOccurs=&quot;2&quot; /> Default and special values default  minOccurs : 1 default  maxOccurs : same as  minOccurs maxOccurs=&quot;unbounded&quot;  : unlimited
Defining new Datatypes If the the built-in datatypes are not enough, you can build your own datatypes. This does not necessarily work: <xsd:element name=&quot;grade&quot; type=&quot;xsd:integer&quot; /> There are  two ways  of specifying your own datatype Named Data Type Anonymous Data Type
1) Named Data Type <?xml version=&quot;1.0&quot;?> <xsd:schema xmlns:xsd=&quot;http://www.w3.org/2001/XMLSchema&quot;> <xsd:element name=&quot;grade&quot; type=&quot; grade_type &quot; /> <xsd:simpleType name=&quot; grade_type &quot;> <xsd:restriction base=&quot;xsd:positiveInteger&quot;>  <xsd:minInclusive value=&quot;4&quot;/> <xsd:maxInclusive value=&quot;10&quot;/> </xsd:restriction> </xsd:simpleType> </xsd:schema>
2) Anonymous Data Type <?xml version=&quot;1.0&quot;?> <xsd:schema xmlns:xsd=&quot;http://www.w3.org/2001/XMLSchema&quot;> <xsd:element name=&quot;grade&quot;> <xsd:simpleType> <xsd:restriction base=&quot;xsd:positiveInteger&quot;>  <xsd:minInclusive value=&quot;4&quot;/> <xsd:maxInclusive value=&quot;10&quot;/> </xsd:restriction> </xsd:simpleType> </xsd:element> </xsd:schema>
Benefits of Named Data Type If you want re-use your datatype: <?xml version=&quot;1.0&quot;?> <xsd:schema xmlns:xsd=&quot;http://www.w3.org/2001/XMLSchema&quot;> <xsd:element name=&quot;grade&quot; type=&quot; grade_type &quot; /> <xsd:element name=&quot;teachers_IQ&quot; type=&quot; grade_type &quot; /> <xsd:simpleType name=&quot; grade_type &quot;> <xsd:restriction base=&quot;xsd:positiveInteger&quot;>  <xsd:minInclusive value=&quot;4&quot;/> <xsd:maxInclusive value=&quot;10&quot;/> </xsd:restriction> </xsd:simpleType> </xsd:schema>
SimpleType: enumeration Alternative content <xsd:simpleType name=&quot;car&quot;> <xsd:restriction base=&quot;xsd:string&quot;> <xsd:enumeration value=&quot;Audi&quot;/> <xsd:enumeration value=&quot;Golf&quot;/> <xsd:enumeration value=&quot;BMW&quot;/> </xsd:restriction> </xsd:simpleType>
SimpleType: pattern Using REGEX: <xsd:simpleType> <xsd:restriction base=&quot;xsd:string&quot;> <xsd:pattern value=&quot;[a-z]&quot;/> </xsd:restriction> </xsd:simpleType>
REGEX Examples <xs:pattern value=&quot;[A-Z][A-Z][A-Z]&quot;/> <xs:pattern value=&quot;[a-zA-Z][a-zA-Z][a-zA-Z]&quot;/> <xs:pattern value=&quot;[xyz]&quot;/> <xs:pattern value=&quot;[0-9][0-9][0-9][0-9][0-9]&quot;/> <xs:pattern value=&quot;([a-z])*&quot;/> <xs:pattern value=&quot;male|female&quot;/> <xs:pattern value=&quot;[a-zA-Z0-9]{8}&quot;/>
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
SimpleType vs. ComplexType SimpleType <grade> 7 </grade> Since  grade  does not  hold other child – elements, grade's type is  simpleType ComplexType <students> <student>Jack</student> </students> Since  student  does hold  child – element(s), student's type is  complexType
Example: XML - File <?xml version=&quot;1.0&quot;?> <students> <firstname>Pekka</firstname> <lastname>Virtanen</lastname> </students>
Example: XSD – file Named ComplexType <?xml version=&quot;1.0&quot;?> <xsd:schema xmlns:xsd=&quot;http://www.w3.org/2001/XMLSchema&quot;> <xsd:element name=&quot;students&quot; type=&quot; students_type &quot;> <xsd: complexType  name=&quot; students_type &quot;> <xsd:sequence> <xsd:element name=&quot;firstname&quot; type=&quot;xsd:string&quot;/> <xsd:element name=&quot;lastname&quot;  type=&quot;xsd:string&quot;/> </xsd:sequence> </xsd: complexType > </xsd:schema> Use now complexType (vs. simpleType)
Example: XSD – file Anonymous ComplexType <?xml version=&quot;1.0&quot;?> <xsd:schema xmlns:xsd=&quot;http://www.w3.org/2001/XMLSchema&quot;> <xsd:element name=&quot;students&quot;> <xsd:complexType> <xsd:sequence> <xsd:element name=&quot;firstname&quot; type=&quot;xsd:string&quot;/> <xsd:element name=&quot;lastname&quot;  type=&quot;xsd:string&quot;/> </xsd:sequence> </xsd:complexType> </xsd:element> </xsd:schema>
Example: ComplexType <xsd:element name=&quot;employee&quot; type=&quot;personinfo&quot;/> <xsd:element name=&quot;student&quot;  type=&quot;personinfo&quot;/> <xsd:element name=&quot;member&quot;  type=&quot;personinfo&quot;/> <xsd:complexType name=&quot;personinfo&quot;> <xsd:sequence> <xsd:element name=&quot;firstname&quot; type=&quot;xsd:string&quot;/> <xsd:element name=&quot;lastname&quot; type=&quot;xsd:string&quot;/> </xsd:sequence> </xsd:complexType>
Deep Structure in XML - File <?xml version=&quot;1.0&quot;?> <students> <student> <name> <firstname>Pekka</firstname>  </nam> </student> </students>
Using Anonymous Data Type: The Horror! <?xml version=&quot;1.0&quot;?> <xsd:schema xmlns:xsd=&quot;http://www.w3.org/2001/XMLSchema&quot;> <xsd:element name=&quot;students&quot;> <xsd:complexType> <xsd:sequence> <xsd:element name=&quot;student&quot;> <xsd:complexType> <xsd:sequence> <xsd:element name=&quot;name&quot;> <xsd:complexType> <xsd:sequence> <xsd:element name=&quot;firstname&quot; type=&quot;xsd:string&quot;/> </xsd:sequence> </xsd:complexType> </xsd:element> </xsd:sequence> </xsd:complexType> </xsd:element> </xsd:sequence> </xsd:complexType> </xsd:element> </xsd:schema>
&quot;There is an error in my schema, could you find it for me?&quot; <?xml version=&quot;1.0&quot;?> <xsd:schema xmlns:xsd=&quot;http://www.w3.org/2001/XMLSchema&quot;> <xsd:element name=&quot;students&quot;> <xsd:complexType> <xsd:sequence> <xsd:element name=&quot;student&quot;> <xsd:complexType> <xsd:sequence> <xsd:element name=&quot;name&quot;> <xsd:complexType> <xsd:sequence> <xsd:element name=&quot;firstname&quot; type=&quot;xsd:string&quot;/> </xsd:sequence> </xsd:complexType> </xsd:element> </xsd:sequence> </xsd:complexType> </xsd:element> </xsd:complexType> </xsd:element> </xsd:schema>
Use Named Datatypes! It's easier to find errors.. <?xml version=&quot;1.0&quot;?> <xsd:schema xmlns:xsd=&quot;http://www.w3.org/2001/XMLSchema&quot;> <xsd:element name=&quot;students&quot; type=&quot; students_type &quot; /> <xsd:complexType name=&quot; students_type &quot;> <xsd:sequence> <xsd:element name=&quot;student&quot; name=&quot; student_type &quot; /> </xsd:sequence> </xsd:complexType> <xsd:complexType name=&quot; student_type &quot;> <xsd:sequence> <xsd:element name=&quot;name&quot; name=&quot; name_type &quot; /> </xsd:sequence> </xsd:complexType> <xsd:complexType name=&quot; name_type &quot;> <xsd:sequence> <xsd:element name=&quot; firstname &quot; name=&quot;xsd:string&quot; /> </xsd:sequence> </xsd:complexType> </xsd:schema>
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 <xsd:element name=&quot;person&quot;> <xsd:complexType> <xsd: choice > <xsd:element name=&quot;employee&quot; type=&quot;employee&quot;/> <xsd:element name=&quot;member&quot; type=&quot;member&quot;/> </xsd: choice > </xsd:complexType> </xsd:element>
Attribute XML <student  id=&quot;A1&quot; >...</student> Schema <xsd:element name=&quot;student&quot; type=&quot;student_type&quot; /> <xsd:complexType name=&quot;student_type&quot;> <xsd:sequence> ... </xsd:sequence> <xsd:attribute name=&quot;id&quot; type=&quot;xsd:ID&quot;/> </xsd:complexType>
Empty Element with Attribute XML <student  id=&quot;A1&quot; /> Schema <xsd:element name=&quot;student&quot; type=&quot;student_type&quot; /> <xsd:complexType name=&quot;student_type&quot;> <xsd:attribute name=&quot;id&quot; type=&quot;xsd:ID&quot;/> </xsd:complexType>
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->load(&quot;books.xml&quot;) and $doc->schemaValidate(&quot;books.xsd' ) ) {      print &quot;Is WellFormed and Schema-valid!&quot;;  }

XML Schema (W3C)

  • 1.
    XML Schema (W3C)Jussi Pohjolainen TAMK University of Applied Sciences
  • 2.
  • 3.
    XML Namespaces Theidea behing XML namespaces is to avoid element name conflicts . Example of name conflict (w3schools.com) <table> <tr> <td>Apples</td> <td>Bananas</td> </tr> </table> <table> <name>African Coffee Table</name> <width>80</width> <length>120</length> </table> Same tag-name, different content and meaning!
  • 4.
    Solving Name Conflict<h:table> <h:tr> <h:td>Apples</h:td> <h:td>Bananas</h:td> </h:tr> </h:table> <f:table> <f:name>African Coffee Table</f:name> <f:width>80</f:width> <f:length>120</f:length> </f:table> Prefix h has xhtml-related elements and prefix f has furniture-related elements
  • 5.
    xmlns - attributesWhen 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<root> <h:table xmlns:h=&quot;http://www.w3.org/TR/html4/&quot; > <h:tr> <h:td>Apples</h:td> <h:td>Bananas</h:td> </h:tr> </h:table> <f:table xmlns:f=&quot;http://www.w3schools.com/furniture&quot; > <f:name>African Coffee Table</f:name> <f:width>80</f:width> <f:length>120</f:length> </f:table> </root>
  • 7.
    xmlns - attribute<root xmlns:h=&quot;http://www.w3.org/TR/html4/&quot; xmlns:f=&quot;http://www.w3schools.com/furniture&quot; > <h:table> <h:tr> <h:td>Apples</h:td> <h:td>Bananas</h:td> </h:tr> </h:table> <f:table> <f:name>African Coffee Table</f:name> <f:width>80</f:width> <f:length>120</f:length> </f:table> </root>
  • 8.
    Namespace name Thename of namespace should be unique: <h:table xmlns:h= &quot;http://www.w3.org/TR/html4/ &quot;> 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: AnXHTML + MathML + SVG Profile An XHTML+MathML+SVG 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.
    <?xml version=&quot;1.0&quot;?> <!DOCTYPEhtml PUBLIC &quot;-//W3C//DTD XHTML 1.1 plus MathML 2.0 plus SVG 1.1//EN&quot; &quot;http://www.w3.org/2002/04/xhtml-math-svg/xhtml-math-svg-flat.dtd&quot;> <html xmlns = &quot;http://www.w3.org/1999/xhtml&quot; xmlns:svg = &quot;http://www.w3.org/2000/svg&quot;> <head> <title>Example of XHTML, SVG and MathML</title> </head> <body> <h2>MathML</h2> <p> <math xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot;> <mfrac> <mi>a</mi> <mi>b</mi> </mfrac> </math> </p> <h2>SVG</h2> <p> <svg:svg width=&quot;50px&quot; height=&quot;50px&quot;> <svg:circle cx=&quot;25px&quot; cy=&quot;25px&quot; r=&quot;20px&quot; fill=&quot;green&quot;/> </svg:svg> </p> </body> </html>
  • 11.
  • 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 Definesthe structure, tag names and order for all xhtml - documents W3C has created XML-language &quot;XHTML&quot; by defining it's rules in DTD.
  • 15.
    DTD Linking Definesthe structure, tag names and order for all &quot;book&quot;- documents TAMK has created XML-language &quot;Book&quot; by defining it's rules in DTD.
  • 16.
    Schema Linking Definesthe structure, tag names and order for all &quot;book&quot;- documents TAMK has created XML-language &quot;Book&quot; by defining it's rules in DTD.
  • 17.
    Linking? The basicidea with linking to Schema: <?xml version=&quot;1.0&quot;?> <root schemaLocation=&quot;note.xsd&quot; > <foo>...</foo> </root> The problem with this is that now it is set that attribute &quot;schemaLocation&quot; is part of your XML-language
  • 18.
    Linking and NamespaceUsage Linking with namespace <?xml version=&quot;1.0&quot;?> <root xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot; xsi:schemaLocation=&quot;note.xsd&quot; > <foo>...</foo> </root> Now the &quot;schemaLocation&quot; – attribute is in it's own namespaces (xsi) and does not belong to the &quot;main&quot; language.
  • 19.
    Simple Schema <?xmlversion=&quot;1.0&quot;?> <xsd:schema xmlns:xsd=&quot;http://www.w3.org/2001/XMLSchema&quot;> <xsd:element name=&quot;koululainen&quot; type=&quot;koululaiset_tyyppi&quot;/> <xsd:complexType name=&quot;koululaiset_tyyppi&quot;> <xsd:sequence> <xsd:element name=&quot;etunimi&quot; type=&quot;xsd:string&quot;/> <xsd:element name=&quot;sukunimi&quot; type=&quot;xsd:string&quot;/> </xsd:sequence> </xsd:complexType> </xsd:schema>
  • 20.
    Let's remove namespaces...<?xml version=&quot;1.0&quot;?> <schema> <element name=&quot;koululainen&quot; type=&quot;koululaiset_tyyppi&quot;/> <complexType name=&quot;koululaiset_tyyppi&quot;> <sequence> <element name=&quot;etunimi&quot; type=&quot;string&quot;/> <element name=&quot;sukunimi&quot; type=&quot;string&quot;/> </sequence> </complexType> </schema> It doesn't look so confusing after all?
  • 21.
    The Basics: ElementYou define the name for the elements by using element -element.  <element name=&quot;foo&quot; type=&quot;bar&quot; /> Type? 44 Built-in schema datatypes string, double, time, date, etc. See all the datatypes
  • 22.
    Usage of Datatypes<xsd:element name=&quot;firstname&quot; type=&quot;xsd:string&quot; /> <xsd:element name=&quot;ableToSwim&quot; type=&quot;xsd:boolean&quot; /> <xsd:element name=&quot;date&quot; type=&quot;xsd:date&quot; />
  • 23.
    minOccurs and maxOccursThe amount of elements In DTD: *, ?, + In Schema: minOccurs, maxOccurs Example <xsd:element name=&quot;date&quot; type=&quot;xsd:date&quot; minOccurs=&quot;1&quot; maxOccurs=&quot;2&quot; /> Default and special values default minOccurs : 1 default maxOccurs : same as minOccurs maxOccurs=&quot;unbounded&quot; : unlimited
  • 24.
    Defining new DatatypesIf the the built-in datatypes are not enough, you can build your own datatypes. This does not necessarily work: <xsd:element name=&quot;grade&quot; type=&quot;xsd:integer&quot; /> There are two ways of specifying your own datatype Named Data Type Anonymous Data Type
  • 25.
    1) Named DataType <?xml version=&quot;1.0&quot;?> <xsd:schema xmlns:xsd=&quot;http://www.w3.org/2001/XMLSchema&quot;> <xsd:element name=&quot;grade&quot; type=&quot; grade_type &quot; /> <xsd:simpleType name=&quot; grade_type &quot;> <xsd:restriction base=&quot;xsd:positiveInteger&quot;> <xsd:minInclusive value=&quot;4&quot;/> <xsd:maxInclusive value=&quot;10&quot;/> </xsd:restriction> </xsd:simpleType> </xsd:schema>
  • 26.
    2) Anonymous DataType <?xml version=&quot;1.0&quot;?> <xsd:schema xmlns:xsd=&quot;http://www.w3.org/2001/XMLSchema&quot;> <xsd:element name=&quot;grade&quot;> <xsd:simpleType> <xsd:restriction base=&quot;xsd:positiveInteger&quot;> <xsd:minInclusive value=&quot;4&quot;/> <xsd:maxInclusive value=&quot;10&quot;/> </xsd:restriction> </xsd:simpleType> </xsd:element> </xsd:schema>
  • 27.
    Benefits of NamedData Type If you want re-use your datatype: <?xml version=&quot;1.0&quot;?> <xsd:schema xmlns:xsd=&quot;http://www.w3.org/2001/XMLSchema&quot;> <xsd:element name=&quot;grade&quot; type=&quot; grade_type &quot; /> <xsd:element name=&quot;teachers_IQ&quot; type=&quot; grade_type &quot; /> <xsd:simpleType name=&quot; grade_type &quot;> <xsd:restriction base=&quot;xsd:positiveInteger&quot;> <xsd:minInclusive value=&quot;4&quot;/> <xsd:maxInclusive value=&quot;10&quot;/> </xsd:restriction> </xsd:simpleType> </xsd:schema>
  • 28.
    SimpleType: enumeration Alternativecontent <xsd:simpleType name=&quot;car&quot;> <xsd:restriction base=&quot;xsd:string&quot;> <xsd:enumeration value=&quot;Audi&quot;/> <xsd:enumeration value=&quot;Golf&quot;/> <xsd:enumeration value=&quot;BMW&quot;/> </xsd:restriction> </xsd:simpleType>
  • 29.
    SimpleType: pattern UsingREGEX: <xsd:simpleType> <xsd:restriction base=&quot;xsd:string&quot;> <xsd:pattern value=&quot;[a-z]&quot;/> </xsd:restriction> </xsd:simpleType>
  • 30.
    REGEX Examples <xs:patternvalue=&quot;[A-Z][A-Z][A-Z]&quot;/> <xs:pattern value=&quot;[a-zA-Z][a-zA-Z][a-zA-Z]&quot;/> <xs:pattern value=&quot;[xyz]&quot;/> <xs:pattern value=&quot;[0-9][0-9][0-9][0-9][0-9]&quot;/> <xs:pattern value=&quot;([a-z])*&quot;/> <xs:pattern value=&quot;male|female&quot;/> <xs:pattern value=&quot;[a-zA-Z0-9]{8}&quot;/>
  • 31.
    Structure of theXML-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. ComplexTypeSimpleType <grade> 7 </grade> Since grade does not hold other child – elements, grade's type is simpleType ComplexType <students> <student>Jack</student> </students> Since student does hold child – element(s), student's type is complexType
  • 33.
    Example: XML -File <?xml version=&quot;1.0&quot;?> <students> <firstname>Pekka</firstname> <lastname>Virtanen</lastname> </students>
  • 34.
    Example: XSD –file Named ComplexType <?xml version=&quot;1.0&quot;?> <xsd:schema xmlns:xsd=&quot;http://www.w3.org/2001/XMLSchema&quot;> <xsd:element name=&quot;students&quot; type=&quot; students_type &quot;> <xsd: complexType name=&quot; students_type &quot;> <xsd:sequence> <xsd:element name=&quot;firstname&quot; type=&quot;xsd:string&quot;/> <xsd:element name=&quot;lastname&quot; type=&quot;xsd:string&quot;/> </xsd:sequence> </xsd: complexType > </xsd:schema> Use now complexType (vs. simpleType)
  • 35.
    Example: XSD –file Anonymous ComplexType <?xml version=&quot;1.0&quot;?> <xsd:schema xmlns:xsd=&quot;http://www.w3.org/2001/XMLSchema&quot;> <xsd:element name=&quot;students&quot;> <xsd:complexType> <xsd:sequence> <xsd:element name=&quot;firstname&quot; type=&quot;xsd:string&quot;/> <xsd:element name=&quot;lastname&quot; type=&quot;xsd:string&quot;/> </xsd:sequence> </xsd:complexType> </xsd:element> </xsd:schema>
  • 36.
    Example: ComplexType <xsd:elementname=&quot;employee&quot; type=&quot;personinfo&quot;/> <xsd:element name=&quot;student&quot; type=&quot;personinfo&quot;/> <xsd:element name=&quot;member&quot; type=&quot;personinfo&quot;/> <xsd:complexType name=&quot;personinfo&quot;> <xsd:sequence> <xsd:element name=&quot;firstname&quot; type=&quot;xsd:string&quot;/> <xsd:element name=&quot;lastname&quot; type=&quot;xsd:string&quot;/> </xsd:sequence> </xsd:complexType>
  • 37.
    Deep Structure inXML - File <?xml version=&quot;1.0&quot;?> <students> <student> <name> <firstname>Pekka</firstname> </nam> </student> </students>
  • 38.
    Using Anonymous DataType: The Horror! <?xml version=&quot;1.0&quot;?> <xsd:schema xmlns:xsd=&quot;http://www.w3.org/2001/XMLSchema&quot;> <xsd:element name=&quot;students&quot;> <xsd:complexType> <xsd:sequence> <xsd:element name=&quot;student&quot;> <xsd:complexType> <xsd:sequence> <xsd:element name=&quot;name&quot;> <xsd:complexType> <xsd:sequence> <xsd:element name=&quot;firstname&quot; type=&quot;xsd:string&quot;/> </xsd:sequence> </xsd:complexType> </xsd:element> </xsd:sequence> </xsd:complexType> </xsd:element> </xsd:sequence> </xsd:complexType> </xsd:element> </xsd:schema>
  • 39.
    &quot;There is anerror in my schema, could you find it for me?&quot; <?xml version=&quot;1.0&quot;?> <xsd:schema xmlns:xsd=&quot;http://www.w3.org/2001/XMLSchema&quot;> <xsd:element name=&quot;students&quot;> <xsd:complexType> <xsd:sequence> <xsd:element name=&quot;student&quot;> <xsd:complexType> <xsd:sequence> <xsd:element name=&quot;name&quot;> <xsd:complexType> <xsd:sequence> <xsd:element name=&quot;firstname&quot; type=&quot;xsd:string&quot;/> </xsd:sequence> </xsd:complexType> </xsd:element> </xsd:sequence> </xsd:complexType> </xsd:element> </xsd:complexType> </xsd:element> </xsd:schema>
  • 40.
    Use Named Datatypes!It's easier to find errors.. <?xml version=&quot;1.0&quot;?> <xsd:schema xmlns:xsd=&quot;http://www.w3.org/2001/XMLSchema&quot;> <xsd:element name=&quot;students&quot; type=&quot; students_type &quot; /> <xsd:complexType name=&quot; students_type &quot;> <xsd:sequence> <xsd:element name=&quot;student&quot; name=&quot; student_type &quot; /> </xsd:sequence> </xsd:complexType> <xsd:complexType name=&quot; student_type &quot;> <xsd:sequence> <xsd:element name=&quot;name&quot; name=&quot; name_type &quot; /> </xsd:sequence> </xsd:complexType> <xsd:complexType name=&quot; name_type &quot;> <xsd:sequence> <xsd:element name=&quot; firstname &quot; name=&quot;xsd:string&quot; /> </xsd:sequence> </xsd:complexType> </xsd:schema>
  • 41.
    Order of theelements 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 <xsd:element name=&quot;person&quot;> <xsd:complexType> <xsd: choice > <xsd:element name=&quot;employee&quot; type=&quot;employee&quot;/> <xsd:element name=&quot;member&quot; type=&quot;member&quot;/> </xsd: choice > </xsd:complexType> </xsd:element>
  • 42.
    Attribute XML <student id=&quot;A1&quot; >...</student> Schema <xsd:element name=&quot;student&quot; type=&quot;student_type&quot; /> <xsd:complexType name=&quot;student_type&quot;> <xsd:sequence> ... </xsd:sequence> <xsd:attribute name=&quot;id&quot; type=&quot;xsd:ID&quot;/> </xsd:complexType>
  • 43.
    Empty Element withAttribute XML <student id=&quot;A1&quot; /> Schema <xsd:element name=&quot;student&quot; type=&quot;student_type&quot; /> <xsd:complexType name=&quot;student_type&quot;> <xsd:attribute name=&quot;id&quot; type=&quot;xsd:ID&quot;/> </xsd:complexType>
  • 44.
    PHP5 and SchemaWith 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->load(&quot;books.xml&quot;) and $doc->schemaValidate(&quot;books.xsd' ) ) {     print &quot;Is WellFormed and Schema-valid!&quot;; }