• Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
    Be the first to like this
No Downloads

Views

Total Views
770
On Slideshare
0
From Embeds
0
Number of Embeds
1

Actions

Shares
Downloads
61
Comments
0
Likes
0

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 1. XML Namespaces and XML Schema Atul Kahate [email_address]
  • 2. XML Namespaces
  • 3. Ambiguity in Tags
    • One of the major goals of XML is to add uniformity to tags
    • Potential problem
      • Several people create their own tags
      • When combined, this may lead to ambiguity
    • Example
      • Company A and B both use a query tag
      • When we combine their XML documents, which one should the XML processor process?
  • 4. XML Namespaces – 1
    • Solve the problem of ambiguity
    • Allow the element and attribute names to be distinguished when merging occurs
    • Provide a unique prefix to the beginning of every element and attribute name
    • Usually it is the Web address of the organization
  • 5. XML Namespaces – 2
    • Technically, the prefix can be any URI
      • URI is more generic than Uniform Resource Locator (URL)
      • URI can be any unique name
    • When namespaces are used, the element and attribute names become two-part names
      • XML namespace and the actual element/attribute name
      • e.g. a title element may now become http://test.com:title
  • 6. Namespace Declarations – 1
    • Declare a namespace in a top-level element
      • All the elements and attributes under that element inherit the namespace
    • Example
      • <book xmlns = “ http://www.test.com ”>
      • <isbn>0-596-0058-8</isbn>
      • <title>My first book</title>
      • <author>Mr XYZ</author>
      • </book>
    • All elements under the book element are a part of the namespace
  • 7. Namespace Declarations – 2
    • If a namespace is declared at an element that is not the root element
      • It applies only till the boundaries of that element
    • Allows different namespaces for different elements in an XML document
      • One per element or sub-element, etc
  • 8. Namespace Declarations – 3
    • Example
      • <supercatalog xmlns = “ http://www.main.com ”>
        • <book xmlns = “ http://www.first.com ”>
        • <isbn>0-596-0058-8</isbn>
        • <title>My first book</title>
        • <author>Mr XYZ</author>
        • </book>
        • <book xmlns = “ http://www.second.com ”>
        • <isbn>0-719-8990-2</isbn>
        • <title>My second book</title>
        • <author>Mr PQR</author>
        • </book>
      • </supercatalog>
  • 9. Namespace Shortcuts
    • Makes namespace declarations and usage more readable
    • Example
      • <zbooks:book xmlns:zbooks = “ http://www.test.com ”>
      • <zbooks:isbn>0-596-0058-8</zbooks:isbn>
      • <zbooks:title>My first book</ zbooks: title>
      • <zbooks:author>Mr XYZ</ zbooks: author>
      • </zbooks:book>
    • All elements starting with zbooks: belong to the http://www.test.com namespace
  • 10. Quick Recap on Namespaces - 1
    • Consider an XML file
    • <employees>
    • <employee>
    • <id>9662</id>
    • <name>Ram</name>
    • <hireDate>2001-08-13</hireDate>
    • </employee>
    • <employee>
    • <id>10000</id>
    • <name>Parag</name>
    • <hireDate>2004-01-12</hireDate>
    • </employee>
    • </employees>
  • 11. Quick Recap on Namespaces - 2
    • Now imagine that the payroll department wants to share this XML file and wants to add payroll data
    • Modified XML file is shown below
    • <employees>
    • <employee>
    • <id>9662</id>
    • <name>Ram</name>
    • <hireDate>2001-08-13</hireDate>
    • <salary> 10000 </salary>
    • <taxes> 2000 </taxes>
    • </employee>
    • <employee>
    • <id>10000</id>
    • <name>Parag</name>
    • <hireDate>2004-01-12</hireDate>
    • <salary> 10000 </salary>
    • <taxes> 2000 </taxes>
    • </employee>
    • </employees>
  • 12. Quick Recap on Namespaces - 3
    • How to handle this change?
      • Update the schema owned by the HR department? OR
      • Separate the data items owned by HR and Payroll and make them responsible for their data items?
        • Use namespaces
        • Different “buckets” for data items owned by HR and Payroll
  • 13. Quick Recap on Namespaces - 4
    • <HRData:employees>
    • <HRData:employee>
    • <HRData:id>9662</HRData:id>
    • <HRData:name>Ram</HRData:name>
    • <HRData:hireDate>2001-08-13</HRData:hireDate>
    • <payrollData:salary> 10000 </payrollData:salary>
    • <payrollData:taxes> 2000 </payrollData:taxes>
    • </HRData:employee>
    • <HRData:employee>
    • <HRData:id>10000</HRData:id>
    • <HRData:name>Parag</HRData:name>
    • <HRData:hireDate>2004-01-12</HRData:hireDate>
    • <payrollData:salary> 10000 </payrollData:salary>
    • <payrollData:taxes> 2000 </payrollData:taxes>
    • </HRData:employee>
    • </HRData:employees>
  • 14. Quick Recap on Namespaces - 5
    • Using a short-hand notation
    • <hr:employees xmlns:hr=“HRData” xmlns:py=“payrollData”>
    • <hr:employee>
    • <hr:id>9662</hr:id>
    • <hr:name>Ram</hr:name>
    • <hr:hireDate>2001-08-13</hr:hireDate>
    • <py:salary> 10000 </py:salary>
    • <py:taxes> 2000 </py:taxes>
    • </hr:employee>
    • <hr:employee>
    • <hr:id>10000</hr:id>
    • <hr:name>Parag</hr:name>
    • <hr:hireDate>2004-01-12</hr:hireDate>
    • <py:salary> 10000 </py:salary>
    • <py:taxes> 2000 </py:taxes>
    • </hr:employee>
    • </hr:employees>
    • Syntax: xmlns:prefix=“Actual namespace” (where prefix is the shorthand prefix)
  • 15. Quick Recap on Namespaces - 6
    • Effectively, we will create two schemas, one for HR and one for payroll
    • Suppose now you want to send the payroll data to the income tax department in XML format
    • What if another company also uses namespace shorthands such as hr and py ?
      • Need to make namespaces globally unique
  • 16. Quick Recap on Namespaces - 7
    • <hr:employees xmlns:hr=&quot;http://www.test.com/hr/&quot; xmlns:py=&quot;http://www.test.com/payroll/&quot;>
    • <hr:employee>
    • <hr:id>9662</hr:id>
    • <hr:name>Ram</hr:name>
    • <hr:hireDate>2001-08-13</hr:hireDate>
    • <py:salary> 10000 </py:salary>
    • <py:taxes> 2000 </py:taxes>
    • </hr:employee>
    • <hr:employee>
    • <hr:id>10000</hr:id>
    • <hr:name>Parag</hr:name>
    • <hr:hireDate>2004-01-12</hr:hireDate>
    • <py:salary> 10000 </py:salary>
    • <py:taxes> 2000 </py:taxes>
    • </hr:employee>
    • </hr:employees>
  • 17. Quick Recap on Namespaces - 8
    • If most elements in an XML file belong to one namespace, we can make it the default namespace
      • No need to prefix elements belonging to that namespace
      • Defining default namespace
        • xmlns=“namespace”
  • 18. Quick Recap on Namespaces - 9
    • <hr:employees xmlns:hr=&quot;http://www.test.com/hr/&quot; xmlns:py=&quot;http://www.test.com/payroll/&quot;>
    • <employee>
    • <id>9662</id>
    • <name>Ram</name>
    • <hireDate>2001-08-13</hireDate>
    • <py:salary> 10000 </py:salary>
    • <py:taxes> 2000 </py:taxes>
    • </employee>
    • <employee>
    • <id>10000</id>
    • <name>Parag</name>
    • <hireDate>2004-01-12</hireDate>
    • <py:salary> 10000 </py:salary>
    • <py:taxes> 2000 </py:taxes>
    • </employee>
    • </employees>
  • 19. Well-formed and Valid XML
  • 20. Well-formed and Valid Documents
    • Well-formed documents
      • Adhere to all the basic rules of XML document design
    • Valid documents
      • A well-formed document that also adheres to its DTD rules
    • All well-formed documents need not be valid documents
  • 21. Well-formed Documents – 1
    • Include a required XML declaration
      • Every XML document must begin with an XML declaration such as
      • <?XML VERSION=“1.0”?>
    • Have a single root element
  • 22. Well-formed Documents – 2
    • All elements, attributes, and entities should use the correct syntax
      • All non-empty elements must have start and end tags
      • Empty elements must have a slash at the end before the greater than tag
      • Every attribute value must be inside quotation marks
      • Nesting of elements must happen correctly
  • 23. Valid Documents
    • Exhibits all characteristics of well-formed documents
    • Additionally
      • Adheres to the rules of its DTD
      • Contain only elements defined by its DTD
      • Includes all the mandatory elements and attributes
  • 24. XML Schemas
  • 25. XML Schemas
    • Similar in concept to a DTD
    • Describes the data elements, attributes and their inter-relationships in an XML document
    • Schema syntax is much closer to the XML tag syntax
  • 26. XML Schemas: Case Study
  • 27. Case Study on Schema – Sample XML Document
    • <?xml version=“1.0”>
    • <company ccode=“XYZ Corp Ltd”>
    • <chairman>Rajesh</chairman>
    • <department>
    • <manager>Hari</manager>
    • <assistant>Madhuri</assistant>
    • <role-description>Administration</role-description>
    • </department>
    • <department>
    • <manager>Vivek</manager>
    • <assistant>Bhavna</assistant>
    • <role-description>Training</role-description>
    • </department>
    • </company>
  • 28. Case Study on Schema – Designing Schema
    • We need the following declaration in our schema file, after the <?xml …?> declaration:
      • <xsd:schema xmlns:xsd=“ http://www.w3.org/2000/10/XMLSchema ”>
  • 29. Case Study –Schema
    • Write out the schema in a manner similar to the way the XML document is organized
    • So, start with a <company> root tag
      • This tag has attributes and sub-elements
      • Hence, it should be of type <complexType>
      • The other type ( <simpleType> ) is reserved for data types holding only values, but no attributes or sub-elements
      • To define the sub-elements, use the <sequence> tag
  • 30. Case Study - Schema -> The structure so far
    • <xsd:element name=“company”>
    • <xsd:complexType>
    • <xsd:sequence>
    • Notes:
      • The <sequence> element defines the order of the child elements
      • Other options are <choice> and <all> , discussed later
      • Let us now define the sub-elements of the company root element
  • 31. Case Study - Schema -> Adding Sub-Elements
    • <xsd:element name=“chairman” type=“xsd:string”/>
    • Defines the first sub-element
    • Now, define the department sub-element and its sub-elements
    • <xsd:element name=“department” minOccurs=“0” maxOccurs=“unbounded”>
    • <xsd:sequence>
    • <xsd:complexType>
  • 32. Case Study - Schema -> Adding Sub-Sub-Elements
    • Now, define the sub-elements of the department tag
    • <xsd:complexType>
    • <xsd:sequence>
    • <xsd:element name=“manager” type=“xsd:string”/>
    • <xsd:element name=“assistant” type=“xsd:string” minOccurs=“0” maxOccurs=“unbounded”/>
    • <xsd:element name=“role-description” type=“xsd:string”/>
    • </xsd:sequence>
    • </xsd:complexType>
  • 33. Case Study - Schema -> Closing the Root Tag
    • Why have we not closed the root tag <company> ?
      • Because, we want to define an attribute for it
    • <xsd:attribute name=“ccode” type=“xsd:string”/>
    • </xsd:complexType>
    • </xsd:element>
    • </xsd:schema>
    • Note: Attribute definitions must always follow the element definitions.
  • 34. Case Study - Schema -> The Complete Schema Code
    • <?xml version=&quot;1.0&quot;?>
    • <xsd:schema xmlns=&quot;http://www.w3.org/2000/10/XMLSchema&quot;>
    • <xsd:element name=&quot;company&quot;>
    • <xsd:complexType>
    • <xsd:sequence>
    • <xsd:element name=“chairman” type=“xsd:string”/>
    • <xsd:element name=“department” minOccurs=“0” maxOccurs=“unbounded”>
    • <xsd:complexType>
    • <xsd:sequence>
    • <xsd:element name=“manager” type=“xsd:string”/>
    • <xsd:element name=“assistant” type=“xsd:string” minOccurs=“0” maxOccurs=“unbounded”/>
    • <xsd:element name=“role-description” type=“xsd:string”/>
    • </xsd:sequence>
    • </xsd:complexType>
    • </xsd:element>
    • </xsd:sequence>
    • <xsd:attribute name=“ccode” type=“xsd:string”/>
    • </xsd:complexType>
    • </xsd:element>
    • </xsd:schema>
  • 35. DTD Versus Schema: An Example
  • 36. XML Example – Using DTD
    • <?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?>
    • <!DOCTYPE BOOK SYSTEM &quot;book_first.dtd&quot;>
    • < BOOK >
    • < BOOK_NAME > Computer Networks </ BOOK_NAME >
    • < AUTHOR > Andrew Tanenbaum </ AUTHOR >
    • < PUBLISHER > Pearson Education </ PUBLISHER >
    • < CATEGORY > Internet Technologies </ CATEGORY >
    • </ BOOK >
  • 37. DTD Example
    • <!ELEMENT BOOK (BOOK_NAME, AUTHOR, PUBLISHER, CATEGORY)>
    • <!ELEMENT BOOK_NAME (#PCDATA)>
    • <!ELEMENT AUTHOR (#PCDATA)>
    • <!ELEMENT PUBLISHER (#PCDATA)>
    • <!ELEMENT CATEGORY (#PCDATA)>
  • 38. XML Document – Using Schema
    • <?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?>
    • < BOOK xmlns:xsi =&quot; http://www.w3.org/2001/XMLSchema-instance &quot;
    • xsi:noNamespaceSchemaLocation =&quot; book_first.xsd &quot;>
    • < BOOK_NAME > Computer Networks </ BOOK_NAME >
    • < AUTHOR > Andrew Tanenbaum </ AUTHOR >
    • < PUBLISHER > Pearson Education </ PUBLISHER >
    • < CATEGORY > Internet Technologies </ CATEGORY >
    • </ BOOK >
  • 39. Corresponding Schema
    • Let us write this as a schema now …
    • <?xml version=&quot;1.0&quot;?>
    • < xsd:schema xmlns:xsd =&quot; http://www.w3.org/2001/XMLSchema &quot;>
    • < xsd:element name =&quot; BOOK &quot;>
    • < xsd:complexType >
    • < xsd:sequence >
    • < xsd:element name =&quot; BOOK_NAME &quot; type =&quot; xsd:string &quot;/>
    • < xsd:element name =&quot; AUTHOR &quot; type =&quot; xsd:string &quot;/>
    • < xsd:element name =&quot; PUBLISHER &quot; type =&quot; xsd:string &quot;/>
    • < xsd:element name =&quot; CATEGORY &quot; type =&quot; xsd:string &quot;/>
    • </ xsd:sequence >
    • </ xsd:complexType >
    • </ xsd:element >
    • </ xsd:schema >
  • 40. Exercise – 1
    • We want to keep information about an employee in the form of employee ID, name, department, salary, and date of joining. Create an XML document and write corresponding DTD and schema.
  • 41. Exercise – 2
    • Modify the above example to split the name into first and last name
  • 42. Schema Basics and Syntaxes
  • 43. The <schema> Element
    • Root element of every XML schema
      • <?xml version=&quot;1.0&quot;?>
      • <xs: schema >
      • ...
      • ...
      • </xs: schema >
    • Usually contains attributes, as shown next
  • 44. Attributes in the <schema> Element
    • <?xml version=&quot;1.0&quot;?>
    • <xs:schema xmlns:xs=&quot;http://www.w3.org/2001/XMLSchema&quot;
    • targetNamespace=&quot;http://www.test.com&quot;
    • xmlns=&quot;http://www.xyz.com&quot;
    • elementFormDefault=&quot;qualified&quot;>
    • ...
    • ...
    • </xs:schema>
    • xmlns:xs=&quot;http://www.w3.org/2001/XMLSchema&quot; indicates that the data types and elements used inside this schema come from this namespace and they should be prefixed with xs:
    • targetNamespace=&quot;http://www.test.com&quot; indicates that the user-defined XML elements, attributes etc come from this schema
    • xmlns=&quot;http://www.xyz.com“ is the default namespace
    • elementFormDefault=&quot;qualified&quot; mandates usage of namespaces for all elements in the current XML document
  • 45. Referencing a Schema from an XML File
    • <?xml version=&quot;1.0&quot;?>
    • <note xmlns=&quot;http://www.xyz.com&quot;
    • xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
    • xsi:schemaLocation=&quot;http://www.xyz.com note.xsd&quot;>
    • <to>Ram</to>
    • <from>Krishna</from>
    • <heading>Reminder</heading>
    • <body>Please send me your XML notes!</body>
    • </note>
    Default namespace Namespace and schema file name
  • 46. Defining Simple Elements
    • A simple element is an XML element that can contain only text (strings, dates, Boolean values, etc). It cannot contain any other elements or attributes.
    • Syntax
      • <xs:element name=&quot;xxx&quot; type=&quot;yyy&quot;/>
    • Examples
      • <lastname>Tanenbaum</lastname>
      • <age>60</age>
      • <dateborn>1945-10-08</dateborn>
    • Corresponding schema declarations
      • <xs:element name=&quot;lastname&quot; type=&quot;xs:string&quot;/>
      • <xs:element name=&quot;age&quot; type=&quot;xs:integer&quot;/>
      • <xs:element name=&quot;dateborn&quot; type=&quot;xs:date&quot;/>
  • 47. Schema Data Types and Default or Constant Values
    • Basic data types
      • xs:string
      • xs:decimal
      • xs:integer
      • xs:boolean
      • xs:date
      • xs:time
    • Declaring default values
      • <xs:element name=&quot;color&quot; type=&quot;xs:string&quot; default=&quot;red&quot;/>
    • Declaring constant values
      • <xs:element name=&quot;color&quot; type=&quot;xs:string&quot; fixed=&quot;red&quot;/>
  • 48. Declaring Attributes in Schemas
    • Simple elements cannot have attributes.
    • If an element has attributes, it is considered to be of complex type.
    • But the attribute itself is always declared as a simple type.
    • This means that an element with attributes always has a complex type definition.
    • Syntax
      • <xs:attribute name=&quot;xxx&quot; type=&quot;yyy&quot;/>
    • Example
      • <lastname lang=&quot;EN&quot;>Joshi</lastname>
    • Corresponding schema declaration
      • <xs:attribute name=&quot;lang&quot; type=&quot;xs:string&quot;/>
  • 49. More on Attributes
    • Default values
      • <xs:attribute name=&quot;lang&quot; type=&quot;xs:string&quot; default=&quot;EN&quot;/>
    • Fixed values
      • <xs:attribute name=&quot;lang&quot; type=&quot;xs:string&quot; fixed=&quot;EN&quot;/>
    • Mandatory attributes
      • <xs:attribute name=&quot;lang&quot; type=&quot;xs:string&quot; use=&quot;required&quot;/>
    • Optional attributes
      • <xs:attribute name=&quot;lang&quot; type=&quot;xs:string&quot; use=&quot;optional&quot;/>
  • 50. Exercise
    • Maintain employee information in the form of employee ID, name, department, salary, and date of joining. Keep maximum information in the form of attributes, rather than as elements. Create an XML document and write and schema.
  • 51. Schema Example
  • 52. Employee XML
    • <? xml version =&quot;1.0&quot; encoding =&quot;utf-8&quot; ?>
    • < Employee lastUpdated =&quot;2007-04-21&quot;>
    • < Name > Manish Potda r </ Name >
    • < Contact >
    • < Address >
    • < Street >12 East Street</ Street >
    • < City >Pune</ City >
    • < Pin >411001</ Pin >
    • < State >Maharashtra</ State >
    • </ Address >
    • < HomePhone >02025530834</ HomePhone >
    • < Email >manish@yahoo.com</ Email >
    • </ Contact >
    • </ Employee >
  • 53. Employee Schema
    • <?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?>
    • <!-- W3C Schema generated by XMLSpy v2007 sp2 (http://www.altova.com) -->
    • < xs:schema xmlns:xs =&quot; http://www.w3.org/2001/XMLSchema &quot;>
    • < xs:element name =&quot; Employee &quot;>
    • < xs:complexType >
    • < xs:sequence >
    • < xs:element name =&quot; Name &quot;/>
    • < xs:element name =&quot; Contact &quot;>
    • < xs:complexType >
    • < xs:sequence >
    • < xs:element ref =&quot; Address &quot;/>
    • < xs:element name =&quot; HomePhone &quot;/>
    • < xs:element name =&quot; Email &quot;/>
    • </ xs:sequence >
    • </ xs:complexType >
    • </ xs:element >
    • </ xs:sequence >
    • < xs:attribute name =&quot; lastUpdated &quot;/>
    • </ xs:complexType >
    • </ xs:element >
    • < xs:element name =&quot; Address &quot;>
    • < xs:complexType >
    • < xs:sequence >
    • < xs:element name =&quot; Street &quot;/>
    • < xs:element name =&quot; City &quot;/>
    • < xs:element name =&quot; Pin &quot;/>
    • < xs:element name =&quot; State &quot;/>
    • </ xs:sequence >
    • </ xs:complexType >
    • </ xs:element >
    • </ xs:schema >
  • 54. Schema and Data Types
    • Schemas provide a much wider range of data types than DTDs
      • Boolean, Date-related, String, Numeric
    • Add flexibility to element declarations
      • Examples
        • <simpleType name=“humanAges”>
        • <minInclusive base=“double” value=“0.0” />
        • <maxInclusive base=“double” value=“150.0” />
        • </simpleType>
        • <simpleType name=“comment”>
        • <maxLength base=“string” value=“1024” />
        • </simpleType>
  • 55. Schema and DTD
    • DTD can be converted into schema
    • Equivalences need to be found
    • Example, consider an element test
    Unbounded 1 1 or more occurrences test+ Unbounded 0 0 or more occurrences test* 1 1 Exactly 1 occurrence test 1 0 0 or 1 occurrences test? maxOccurs in schema minOccurs in schema Meaning DTD Syntax
  • 56. Schema Components
  • 57. Schema Components
    • Generic term for the blocks that make up the abstract data model of the schema syntax
    • There are 12 components, split into three groups
      • Primary components – Simple and complex type definitions, Element and attribute declarations
      • Secondary components – Attribute groups, Identity constraints, Model group definitions
      • Helper components – Not named or accessed independently
  • 58. Primary Components
  • 59. Primary Components
    • Element declarations
    • Simple type definitions – Can hold only values, not child elements, or attributes
    • Complex type definitions – Can have sub-elements and attributes
    • Attribute declarations
    • Note: We declare attributes and elements , but define types
  • 60. Declaring Elements
    • Example
    • <xsd:element name = “dateReceived”/>
    • Or, with data type
    • <xsd:element name = “dateReceived” type = “xsd:date”/>
  • 61. Specifying Element Occurrences
    • minOccurs and maxOccurs can be used
    • Examples
    • <xsd:element name = “dateReceived” type = “xsd:date” minOccurs = “0” maxOccurs = “1”/>
    • <xsd:element name = “student” type = “xsd:string” minOccurs = “1” maxOccurs = “unbounded”/>
  • 62. Simple and Complex Types
    • Simple types
      • Allow atoms of data (element or attribute values) that cannot be divided further
      • Hence, only elements that do not have child elements or attributes are of simple type
    • Complex types
      • Can contain other elements and attributes
  • 63. Simple Types
    • Contain information that cannot be split further
    • Example
    • XML
    • <myElement>I am simple</myElement>
    • Schema
    • < xs:element name =&quot; myElement &quot; type =&quot; xs:string &quot;/>
  • 64. Simple Data Types – Classification
    • Two types
      • Built-in types
        • Primitive types – Examples are string, boolean, number, float, double, decimal, dateTime, time, date, etc
        • Derived types – Examples are integer, nonPositiveInteger, negativeInteger, long, int, short, byte, nonNegativeInteger, unsignedInt, positiveInteger, etc
      • User-derived types
  • 65. Named Complex Types
    • Allow elements to contain sub-elements and attributes; thus, allow creation of content models
    • Schema
      • <xsd:complexType name= &quot;Address&quot;>
      • <xsd:sequence>
      • <xsd:element name = &quot;Street1&quot; />
      • <xsd:element name = &quot;Street2&quot; />
      • <xsd:element name = &quot;City&quot; />
      • <xsd:element name = &quot;Pin&quot; />
      • <xsd:element name = “ State &quot; />
      • </xsd:sequence>
      • </xsd:sequence>
    • XML
      • <xsd:element name = “billingAddress” type = “Address” />
  • 66. Anonymous Complex Types
    • <xsd:element name = &quot;Name&quot;>
    • <xsd:complexType>
    • <xsd:sequence>
    • <xsd:element name = &quot;FirstName&quot; />
    • <xsd:element name = &quot;MiddleInitial&quot; minOccurs = &quot;0&quot; maxOccurs = &quot;unbounded&quot; />
    • <xsd:element name = &quot;LastName&quot; />
    • </xsd:sequence>
    • </xsd:complexType>
    • </xsd:name>
  • 67. Combining Named and Unnamed Declarations
    • <?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?>
    • <!-- W3C Schema generated by XMLSpy v2007 sp2 (http://www.altova.com) -->
    • < xs:schema xmlns:xs =&quot; http://www.w3.org/2001/XMLSchema &quot;>
    • < xs:element name =&quot; ContactDetails &quot;>
    • < xs:complexType >
    • < xs:sequence >
    • < xs:element ref =&quot; Contact &quot; minOccurs =&quot; 1 &quot; maxOccurs =&quot; unbounded &quot;/>
    • </ xs:sequence >
    • </ xs:complexType >
    • </ xs:element >
    • < xs:element name =&quot; Contact &quot;>
    • < xs:complexType >
    • < xs:sequence >
    • < xs:element name =&quot; Name &quot;>
    • < xs:complexType >
    • < xs:sequence >
    • < xs:element name =&quot; FirstName &quot;/>
    • < xs:element name =&quot; MiddleInitial &quot; minOccurs =&quot; 0 &quot; maxOccurs =&quot; unbounded &quot;/>
    • < xs:element name =&quot; LastName &quot;/>
    • </ xs:sequence >
    • </ xs:complexType >
    • </ xs:element >
    • < xs:element name =&quot; billingAddress &quot; type =&quot; Address &quot;/>
    • < xs:element name =&quot; mailingAddress &quot; type =&quot; Address &quot;/>
    • </ xs:sequence >
    • </ xs:complexType >
    • </ xs:element >
    • < xs:complexType name =&quot; Address &quot;>
    • < xs:sequence >
    • < xs:element name =&quot; Street1 &quot;/>
    • < xs:element name =&quot; Street1 &quot;/>
    • < xs:element name =&quot; City &quot;/>
    • < xs:element name =&quot; Pin &quot;/>
    • < xs:element name =&quot; State &quot;/>
    • </ xs:sequence >
    • </ xs:complexType >
    • </ xs:schema >
  • 68. Extending Complex Types
    • Suppose we want to add two more properties to the Address complex type in a given situation
    • <?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?>
    • <!-- W3C Schema generated by XMLSpy v2007 sp2 (http://www.altova.com) -->
    • < xs:schema xmlns:xs =&quot; http://www.w3.org/2001/XMLSchema &quot;>
    • < xs:element name =&quot; ContactDetails &quot;>
    • < xs:complexType >
    • < xs:sequence >
    • < xs:element ref =&quot; Contact &quot; minOccurs =&quot; 1 &quot; maxOccurs =&quot; unbounded &quot;/>
    • </ xs:sequence >
    • </ xs:complexType >
    • </ xs:element >
    • < xs:element name =&quot; Contact &quot;>
    • < xs:complexType >
    • < xs:sequence >
    • < xs:element name =&quot; Name &quot;>
    • < xs:complexType >
    • < xs:sequence >
    • < xs:element name =&quot; FirstName &quot;/>
    • < xs:element name =&quot; MiddleInitial &quot; minOccurs =&quot; 0 &quot; maxOccurs =&quot; unbounded &quot;/>
    • < xs:element name =&quot; LastName &quot;/>
    • </ xs:sequence >
    • </ xs:complexType >
    • </ xs:element >
    • < xs:element name =&quot; billingAddress &quot; type =&quot; Address &quot;/>
    • < xs:element name =&quot; mailingAddress &quot; type =&quot; Address &quot;/>
    • < xs:element name =&quot; CreditAddressReference &quot;>
    • < xs:complexType >
    • < xs:complexContent >
    • < xs:extension base =&quot; Address &quot;>
    • < xs:attribute name =&quot; residentFrom &quot; type =&quot; xs:date &quot;/>
    • < xs:attribute name =&quot; residentTo &quot; type =&quot; xs:date &quot;/>
    • </ xs:extension >
    • </ xs:complexContent >
    • </ xs:complexType >
    • </ xs:element >
    • </ xs:sequence >
    • </ xs:complexType >
    • </ xs:element >
    • < xs:complexType name =&quot; Address &quot;>
    • < xs:sequence >
    • < xs:element name =&quot; Street1 &quot;/>
    • < xs:element name =&quot; Street1 &quot;/>
    • < xs:element name =&quot; City &quot;/>
    • < xs:element name =&quot; Pin &quot;/>
    • < xs:element name =&quot; State &quot;/>
    • </ xs:sequence >
    • </ xs:complexType >
    • </ xs:schema >
  • 69. Declaring Attributes
    • <xsd:element name = “invoice”>
    • <xsd:complexType>
    • <xsd:attribute name = “paid” use = “optional” default = “true”>
    • </xsd:complexType>
    • </xsd:element>
  • 70. Secondary Components
  • 71. Secondary Components
    • Can be classified into
      • Model group definitions
      • Attribute groups
      • Notation declarations
      • Identity constraints
  • 72. Model group definitions
    • Allows creation of group of element definitions and naming them using a name attribute, so that we can use these groups to build content models
    • We need to use the group element, which must be a top-level schema element, i.e. child of the schema element
    • Once a group is specified, we can also indicate whether only one of them should appear ( choice ) or all of them are needed ( all )
  • 73. Choice Example – Using group Syntax
    • <?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?>
    • <!-- W3C Schema generated by XMLSpy v2007 sp2 (http://www.altova.com) -->
    • < xs:schema xmlns:xs =&quot; http://www.w3.org/2001/XMLSchema &quot;>
    • < xs:element name =&quot; FlightDetails &quot;>
    • < xs:complexType >
    • < xs:sequence >
    • < xs:element name =&quot; Name &quot;/>
    • < xs:group ref =&quot; MealOptions &quot;/>
    • </ xs:sequence >
    • </ xs:complexType >
    • </ xs:element >
    • < xs:group name =&quot; MealOptions &quot;>
    • < xs:choice >
    • < xs:element name =&quot; Vegetarian &quot;/>
    • < xs:element name =&quot; Non-Vegetarian &quot;/>
    • < xs:element name =&quot; Salad &quot;/>
    • </ xs:choice >
    • </ xs:group >
    • </ xs:schema >
  • 74. Choice Example – Using Unnamed Syntax
      • <xs:element name=&quot;person&quot;>
      • <xs:complexType>
      • <xs:choice>
      • <xs:element name=&quot;employee&quot; type=&quot;employee&quot;/>
      • <xs:element name=&quot;member&quot; type=&quot;member&quot;/>
      • </xs:choice>
      • </xs:complexType>
      • </xs:element>
  • 75. All Example – Using group Syntax
    • <?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?>
    • <!-- W3C Schema generated by XMLSpy v2007 sp2 (http://www.altova.com) -->
    • < xs:schema xmlns:xs =&quot; http://www.w3.org/2001/XMLSchema &quot;>
    • < xs:group name =&quot; person &quot;>
    • < xs:all >
    • < xs:element name =&quot; firstname &quot; type =&quot; xs:string &quot;/>
    • < xs:element name =&quot; lastname &quot; type =&quot; xs:string &quot;/>
    • </ xs:all >
    • </ xs:group >
    • < xs:element name =&quot; test &quot;>
    • < xs:complexType >
    • < xs:group ref =&quot; person &quot; />
    • </ xs:complexType >
    • </ xs:element >
    • </ xs:schema >
  • 76. All Example – Using Unnamed Syntax
      • <xs:element name=&quot;person&quot;>
      • <xs:complexType>
      • <xs:all>
      • <xs:element name=&quot;firstname&quot; type=&quot;xs:string&quot;/>
      • <xs:element name=&quot;lastname&quot; type=&quot;xs:string&quot;/>
      • </xs:all>
      • </xs:complexType>
      • </xs:element>
    • firstname and lastname are mandatory sub-elements – must occur exactly once
  • 77. All – Another Example
    • <xs:group name = “CreditCardDetails”>
    • <xs:all>
    • <xs:element name = “CardType” />
    • <xs:element name = “CardHolder” />
    • <xs:element name = “CardNumber” />
    • <xs:element name = “CardExpiry” />
    • </xs:all>
    • </xs:group>
  • 78. Sequence Example using group Syntax
    • <?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?>
    • <!-- W3C Schema generated by XMLSpy v2007 sp2 (http://www.altova.com) -->
    • < xs:schema xmlns:xs =&quot; http://www.w3.org/2001/XMLSchema &quot;>
    • < xs:group name =&quot; person &quot;>
    • < xs:sequence >
    • < xs:element name =&quot; firstname &quot; type =&quot; xs:string &quot;/>
    • < xs:element name =&quot; lastname &quot; type =&quot; xs:string &quot;/>
    • </ xs:sequence >
    • </ xs:group >
    • < xs:element name =&quot; test &quot;>
    • < xs:complexType >
    • < xs:group ref =&quot; person &quot; />
    • </ xs:complexType >
    • </ xs:element >
    • </ xs:schema >
  • 79. Sequence Example Using Unnamed Syntax
    • Another element that we can use inside a group is sequence
    • Example
      • <xs:group name=&quot;person&quot;>
      • <xs:complexType>
      • <xs:sequence>
      • <xs:element name=&quot;firstname&quot; type=&quot;xs:string&quot;/>
      • <xs:element name=&quot;lastname&quot; type=&quot;xs:string&quot;/>
      • </xs:sequence>
      • </xs:complexType>
      • </xs:element>
  • 80. Other Details
    • We can use minOccurs, maxOccurs attributes on group, choice, sequence, and all elements.
  • 81. Attribute Groups
    • Just as we can group elements together, we can create a group of attributes
    • Attribute groups allow us to define a set of attributes that would be used on a set of elements
    • Attribute groups can nest other attribute groups inside them
  • 82. Attribute Groups - Example
    • <?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?>
    • <!-- W3C Schema generated by XMLSpy v2007 sp2 (http://www.altova.com) -->
    • < xs:schema xmlns:xs =&quot; http://www.w3.org/2001/XMLSchema &quot;>
    • < xs:element name =&quot; product &quot;>
    • < xs:complexType >
    • < xs:attributeGroup ref =&quot; productDetails &quot;/>
    • </ xs:complexType >
    • </ xs:element >
    • < xs:attributeGroup name =&quot; productDetails &quot;>
    • < xs:attribute name =&quot; productID &quot; use =&quot; required &quot; type =&quot; xs:string &quot;/>
    • < xs:attribute name =&quot; productName &quot; use =&quot; required &quot; type =&quot; xs:string &quot;/>
    • < xs:attribute name =&quot; productDescription &quot; use =&quot; required &quot; type =&quot; xs:string &quot;/>
    • < xs:attribute name =&quot; unit &quot; use =&quot; required &quot; type =&quot; xs:positiveInteger &quot;/>
    • < xs:attribute name =&quot; price &quot; use =&quot; required &quot; type =&quot; xs:decimal &quot;/>
    • < xs:attribute name =&quot; stock &quot; use =&quot; required &quot; type =&quot; xs:integer &quot;/>
    • </ xs:attributeGroup >
    • </ xs:schema >
  • 83. Default or Fixed Element Content – 1
    • Schema
    • <?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?>
    • <!-- W3C Schema generated by XMLSpy v2007 sp2 (http://www.altova.com) -->
    • < xs:schema xmlns:xs =&quot; http://www.w3.org/2001/XMLSchema &quot;>
    • < xs:element name =&quot; product &quot; default =&quot; book &quot; />
    • </ xs:schema >
  • 84. Default or Fixed Element Content – 2
    • XML
    • <?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?>
    • < product lastUpdated =&quot; 2007-04-21 &quot; xmlns:xsi =&quot; http://www.w3.org/2001/XMLSchema-instance &quot; xsi:noNamespaceSchemaLocation =&quot; C:UsersAtulDesktoptest.xsd &quot;>
    • pencil
    • </ product >
    • Or this
    • <?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?>
    • < product lastUpdated =&quot; 2007-04-21 &quot; xmlns:xsi =&quot; http://www.w3.org/2001/XMLSchema-instance &quot; xsi:noNamespaceSchemaLocation =&quot; C:UsersAtulDesktoptest.xsd &quot;>
    • </ product >
  • 85. Default or Fixed Element Content – 2
    • Usage of Fixed in the Schema
    • <?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?>
    • <!-- W3C Schema generated by XMLSpy v2007 sp2 (http://www.altova.com) -->
    • < xs:schema xmlns:xs =&quot; http://www.w3.org/2001/XMLSchema &quot;>
    • < xs:element name =&quot; product &quot; fixed =&quot; book &quot; />
    • </ xs:schema >
    • Now, the value of the element in the XML document must either be book , or it should be empty
  • 86. Nillable Elements
    • We can specify that an element must occur or is optional by using the nillable attribute
    • However, we still need to include the nillable element in the XML document, but with a different syntax, as shown in the example
  • 87. Nillable Element Example – 1
    • Schema
    • <?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?>
    • <!-- W3C Schema generated by XMLSpy v2007 sp2 (http://www.altova.com) -->
    • < xs:schema xmlns:xs =&quot; http://www.w3.org/2001/XMLSchema &quot;>
    • < xs:element name =&quot; product &quot;>
    • < xs:complexType >
    • < xs:sequence >
    • < xs:element name =&quot; productID &quot;/>
    • < xs:element name =&quot; productName &quot; nillable =&quot; true &quot;/>
    • </ xs:sequence >
    • </ xs:complexType >
    • </ xs:element >
    • </ xs:schema >
  • 88. Nillable Element Example – 2
    • XML
    • <?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?>
    • < product xmlns:xsi =&quot; http://www.w3.org/2001/XMLSchema-instance &quot; xsi:noNamespaceSchemaLocation =&quot; C:UsersAtulDesktoptest.xsd &quot;>
    • < productID > test </ productID >
    • < productName xsi:nil =&quot; true &quot;/>
    • </ product >
  • 89. Exercise – 1
    • Create an XML document and write the corresponding schema to represent the following information:
      • Trimester number
      • Student’s details
        • Roll number
        • Name
        • Total maximum marks across all subjects
        • Total actual marks across all subjects
        • Percentage of marks
  • 90. Exercise – 2
    • Modify the above example to take care of the following:
      • For every student, maintain a list of any 5 subjects, each with a maximum of 100 marks and actual marks obtained, along with the earlier information
  • 91. Schema Facets
  • 92. Schema Facets (Restrictions) - 1
    • Restriction on values
      • <xs:element name=&quot;age&quot;>
      • <xs:simpleType>
      • <xs:restriction base=&quot;xs:integer&quot;>
      • <xs:minInclusive value=&quot;0&quot;/>
      • <xs:maxInclusive value=&quot;100&quot;/>
      • </xs:restriction>
      • </xs:simpleType>
      • </xs:element>
  • 93. Schema Facets (Restrictions) - 2
    • Restriction on a set of values
    • <xs:element name=&quot;car“ type=“carType”/ >
      • <xs:simpleType name=“carType” >
      • <xs:restriction base=&quot;xs:string&quot;>
      • <xs:enumeration value=“Santro/>
      • <xs:enumeration value=“Maruti&quot;/>
      • <xs:enumeration value=“Indica/>
      • </xs:restriction>
      • </xs:simpleType>
    • </xs:element>
    • Note: Text shown in blue is optional
  • 94. Schema Facets (Restrictions) - 3
    • Restrictions on a series of values
      • <xs:element name=&quot;letter&quot;>
      • <xs:simpleType>
      • <xs:restriction base=&quot;xs:string&quot;>
      • <xs:pattern value=&quot;[a-z]&quot;/>
      • </xs:restriction>
      • </xs:simpleType>
      • </xs:element>
      • Allows one lowercase alphabet
    • <xs:pattern value=&quot;[A-Z][A-Z][A-Z]&quot;/>
      • Allows three uppercase alphabets
    • <xs:pattern value=&quot;[a-zA-Z][a-zA-Z][a-zA-Z]&quot;/>
      • Allows three uppercase or lowercase alphabets
    • <xs:pattern value=&quot;[xyz]&quot;/>
      • Allows one of the lowercase alphabets x, y, or z
    • <xs:pattern value=&quot;[0-9][0-9][0-9][0-9][0-9]&quot;/>
      • Allows five digits in sequence
  • 95. Schema Facets (Restrictions) - 4
    • Other restrictions on a series of values
      • <xs:element name=&quot;letter&quot;>
      • <xs:simpleType>
      • <xs:restriction base=&quot;xs:string&quot;>
      • <xs:pattern value=&quot;([a-z])*&quot;/>
      • </xs:restriction>
      • </xs:simpleType>
      • </xs:element>
      • Allows 0 or more lowercase alphabets
      • <xs:pattern value=&quot;([a-z][A-Z])+&quot;/>
        • One or more lowercase alphabets followed by an uppercase alphabet
      • <xs:pattern value=“worker|manager&quot;/>
        • Allow either worker or manager
      • <xs:pattern value=&quot;[a-zA-Z0-9]{8}&quot;/>
        • Allow exactly 8 characters, either alphabets or numbers
      • <xs:length value=&quot;8&quot;/>
        • Allow exactly 8 characters
      • <xs:minLength value=&quot;5&quot;/>
      • <xs:maxLength value=&quot;8&quot;/>
        • Allow a minimum of 5 and maximum of 8 characters
  • 96. Schema: Case Study
  • 97. XML Document
    • <?xml version=&quot;1.0&quot; encoding=&quot;ISO-8859-1&quot;?>
    • <shiporder orderid=&quot;889923&quot;
    • xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
    • xsi:noNamespaceSchemaLocation=&quot;shiporder.xsd&quot;>
    • <orderperson>John Smith</orderperson>
    • <shipto>
    • <name>Ola Nordmann</name>
    • <address>Langgt 23</address>
    • <city>4000 Stavanger</city>
    • <country>Norway</country>
    • </shipto>
    • <item>
    • <title>Empire Burlesque</title>
    • <note>Special Edition</note>
    • <quantity>1</quantity>
    • <price>10.90</price>
    • </item>
    • <item>
    • <title>Hide your heart</title>
    • <quantity>1</quantity>
    • <price>9.90</price>
    • </item>
    • </shiporder>
    Specifies that this document should be validated against a schema Specifies the location of the schema (in this case, same as that of the XML file)
  • 98. Start with the Schema Template
    • <?xml version=&quot;1.0&quot;?>
    • <xs:schema xmlns:xs=&quot;http://www.w3.org/2001/XMLSchema&quot;>
    • ...
    • ...
    • </xs:schema>
  • 99. Create the shiporder Element Outline
    • <xs:element name=&quot;shiporder&quot;>
    • <xs:complexType>
    • <xs:sequence>
    • ...
    • ...
    • </xs:sequence>
    • ...
    • </xs:complexType>
    • </xs:element>
  • 100. Define the orderperson Element
    • <xs:element name=&quot;orderperson&quot; type=&quot;xs:string&quot;/>
  • 101. Define the shipto and item Elements
    • <xs:element name=&quot;shipto&quot;>
    • <xs:complexType>
    • <xs:sequence>
    • <xs:element name=&quot;name&quot; type=&quot;xs:string&quot;/>
    • <xs:element name=&quot;address&quot; type=&quot;xs:string&quot;/>
    • <xs:element name=&quot;city&quot; type=&quot;xs:string&quot;/>
    • <xs:element name=&quot;country&quot; type=&quot;xs:string&quot;/>
    • </xs:sequence>
    • </xs:complexType>
    • </xs:element>
    • <xs:element name=&quot;item&quot; maxOccurs=&quot;unbounded&quot;>
    • <xs:complexType>
    • <xs:sequence>
    • <xs:element name=&quot;title&quot; type=&quot;xs:string&quot;/>
    • <xs:element name=&quot;note&quot; type=&quot;xs:string&quot; minOccurs=&quot;0&quot;/>
    • <xs:element name=&quot;quantity&quot; type=&quot;xs:positiveInteger&quot;/>
    • <xs:element name=&quot;price&quot; type=&quot;xs:decimal&quot;/>
    • </xs:sequence>
    • </xs:complexType>
    • </xs:element>
  • 102. Declare the attribute for the shiporder Element
    • <xs:attribute name=&quot;orderid&quot; type=&quot;xs:string&quot; use=&quot;required&quot;/>
  • 103. The Complete Schema
    • <?xml version=&quot;1.0&quot; encoding=&quot;ISO-8859-1&quot; ?>
    • <xs:schema xmlns:xs=&quot;http://www.w3.org/2001/XMLSchema&quot;>
    • <xs:element name=&quot;shiporder&quot;>
    • <xs:complexType>
    • <xs:sequence>
    • <xs:element name=&quot;orderperson&quot; type=&quot;xs:string&quot;/>
    • <xs:element name=&quot;shipto&quot;>
    • <xs:complexType>
    • <xs:sequence>
    • <xs:element name=&quot;name&quot; type=&quot;xs:string&quot;/>
    • <xs:element name=&quot;address&quot; type=&quot;xs:string&quot;/>
    • <xs:element name=&quot;city&quot; type=&quot;xs:string&quot;/>
    • <xs:element name=&quot;country&quot; type=&quot;xs:string&quot;/>
    • </xs:sequence>
    • </xs:complexType>
    • </xs:element>
    • <xs:element name=&quot;item&quot; maxOccurs=&quot;unbounded&quot;>
    • <xs:complexType>
    • <xs:sequence>
    • <xs:element name=&quot;title&quot; type=&quot;xs:string&quot;/>
    • <xs:element name=&quot;note&quot; type=&quot;xs:string&quot; minOccurs=&quot;0&quot;/>
    • <xs:element name=&quot;quantity&quot; type=&quot;xs:positiveInteger&quot;/>
    • <xs:element name=&quot;price&quot; type=&quot;xs:decimal&quot;/>
    • </xs:sequence>
    • </xs:complexType>
    • </xs:element>
    • </xs:sequence>
    • <xs:attribute name=&quot;orderid&quot; type=&quot;xs:string&quot; use=&quot;required&quot;/>
    • </xs:complexType>
    • </xs:element>
    • </xs:schema>
  • 104. Making it more readable
    • <?xml version=&quot;1.0&quot; encoding=&quot;ISO-8859-1&quot; ?>
    • <xs:schema xmlns:xs=&quot;http://www.w3.org/2001/XMLSchema&quot;>
    • <!-- definition of simple elements -->
    • <xs:element name=&quot;orderperson&quot; type=&quot;xs:string&quot;/>
    • <xs:element name=&quot;name&quot; type=&quot;xs:string&quot;/>
    • <xs:element name=&quot;address&quot; type=&quot;xs:string&quot;/>
    • <xs:element name=&quot;city&quot; type=&quot;xs:string&quot;/>
    • <xs:element name=&quot;country&quot; type=&quot;xs:string&quot;/>
    • <xs:element name=&quot;title&quot; type=&quot;xs:string&quot;/>
    • <xs:element name=&quot;note&quot; type=&quot;xs:string&quot;/>
    • <xs:element name=&quot;quantity&quot; type=&quot;xs:positiveInteger&quot;/>
    • <xs:element name=&quot;price&quot; type=&quot;xs:decimal&quot;/>
    • <!-- definition of attributes -->
    • <xs:attribute name=&quot;orderid&quot; type=&quot;xs:string&quot;/>
    • <!-- definition of complex elements -->
    • <xs:element name=&quot;shipto&quot;>
    • <xs:complexType>
    • <xs:sequence>
    • <xs:element ref=&quot;name&quot;/>
    • <xs:element ref=&quot;address&quot;/>
    • <xs:element ref=&quot;city&quot;/>
    • <xs:element ref=&quot;country&quot;/>
    • </xs:sequence>
    • </xs:complexType>
    • </xs:element>
    • <xs:element name=&quot;item&quot;>
    • <xs:complexType>
    • <xs:sequence>
    • <xs:element ref=&quot;title&quot;/>
    • <xs:element ref=&quot;note&quot; minOccurs=&quot;0&quot;/>
    • <xs:element ref=&quot;quantity&quot;/>
    • <xs:element ref=&quot;price&quot;/>
    • </xs:sequence>
    • </xs:complexType>
    • </xs:element>
    • <xs:element name=&quot;shiporder&quot;>
    • <xs:complexType>
    • <xs:sequence>
    • <xs:element ref=&quot;orderperson&quot;/>
    • <xs:element ref=&quot;shipto&quot;/>
    • <xs:element ref=&quot;item&quot; maxOccurs=&quot;unbounded&quot;/>
    • </xs:sequence>
    • <xs:attribute ref=&quot;orderid&quot; use=&quot;required&quot;/>
    • </xs:complexType>
    • </xs:element>
    • </xs:schema>
  • 105. Adding more features
    • <?xml version=&quot;1.0&quot; encoding=&quot;ISO-8859-1&quot; ?>
    • <xs:schema xmlns:xs=&quot;http://www.w3.org/2001/XMLSchema&quot;>
    • <xs:simpleType name=&quot;stringtype&quot;>
    • <xs:restriction base=&quot;xs:string&quot;/>
    • </xs:simpleType>
    • <xs:simpleType name=&quot;inttype&quot;>
    • <xs:restriction base=&quot;xs:positiveInteger&quot;/>
    • </xs:simpleType>
    • <xs:simpleType name=&quot;dectype&quot;>
    • <xs:restriction base=&quot;xs:decimal&quot;/>
    • </xs:simpleType>
    • <xs:simpleType name=&quot;orderidtype&quot;>
    • <xs:restriction base=&quot;xs:string&quot;>
    • <xs:pattern value=&quot;[0-9]{6}&quot;/>
    • </xs:restriction>
    • </xs:simpleType>
    • <xs:complexType name=&quot;shiptotype&quot;>
    • <xs:sequence>
    • <xs:element name=&quot;name&quot; type=&quot;stringtype&quot;/>
    • <xs:element name=&quot;address&quot; type=&quot;stringtype&quot;/>
    • <xs:element name=&quot;city&quot; type=&quot;stringtype&quot;/>
    • <xs:element name=&quot;country&quot; type=&quot;stringtype&quot;/>
    • </xs:sequence>
    • </xs:complexType>
    • <xs:complexType name=&quot;itemtype&quot;>
    • <xs:sequence>
    • <xs:element name=&quot;title&quot; type=&quot;stringtype&quot;/>
    • <xs:element name=&quot;note&quot; type=&quot;stringtype&quot; minOccurs=&quot;0&quot;/>
    • <xs:element name=&quot;quantity&quot; type=&quot;inttype&quot;/>
    • <xs:element name=&quot;price&quot; type=&quot;dectype&quot;/>
    • </xs:sequence>
    • </xs:complexType>
    • <xs:complexType name=&quot;shipordertype&quot;>
    • <xs:sequence>
    • <xs:element name=&quot;orderperson&quot; type=&quot;stringtype&quot;/>
    • <xs:element name=&quot;shipto&quot; type=&quot;shiptotype&quot;/>
    • <xs:element name=&quot;item&quot; maxOccurs=&quot;unbounded&quot; type=&quot;itemtype&quot;/>
    • </xs:sequence>
    • <xs:attribute name=&quot;orderid&quot; type=&quot;orderidtype&quot; use=&quot;required&quot;/>
    • </xs:complexType>
    • <xs:element name=&quot;shiporder&quot; type=&quot;shipordertype&quot;/>
    • </xs:schema>
  • 106. Exercise – 1
    • Create schema for the following XML
    • <?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?>
    • < Order xmlns:xsi =&quot; http://www.w3.org/2001/XMLSchema-instance &quot; xsi:noNamespaceSchemaLocation =&quot; C:UsersAtulDesktoptest.xsd &quot;>
    • < Products >
    • < Product >
    • < Product_ID > 100 </ Product_ID >
    • < Product_Name > Book </ Product_Name >
    • < Product_Units > 2 </ Product_Units >
    • < Product_Price > 400 </ Product_Price >
    • </ Product >
    • < Product >
    • < Product_ID > 200 </ Product_ID >
    • < Product_Name > Pen </ Product_Name >
    • < Product_Units > 5 </ Product_Units >
    • < Product_Price > 10 </ Product_Price >
    • </ Product >
    • </ Products >
    • < Contact >
    • < Name > Reshama Joshi </ Name >
    • < Address > 43 Tilak Road </ Address >
    • < City > Pune </ City >
    • < Pin > 411001 </ Pin >
    • </ Contact >
    • < Payment >
    • < CreditCard >
    • < CardType > VISA </ CardType >
    • < CardNumber > 901010111 </ CardNumber >
    • < ExpiryDate > 2012-10-01 </ ExpiryDate >
    • </ CreditCard >
    • < Amount > 1000 </ Amount >
    • </ Payment >
    • </ Order >
  • 107. Solution – 1
    • <?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?>
    • <!-- W3C Schema generated by XMLSpy v2007 sp2 (http://www.altova.com) -->
    • < xs:schema xmlns:xs =&quot; http://www.w3.org/2001/XMLSchema &quot;>
    • < xs:element name =&quot; Order &quot;>
    • < xs:complexType >
    • < xs:sequence >
    • < xs:element name =&quot; Products &quot; type =&quot; productsType &quot;/>
    • < xs:element name =&quot; Contact &quot; type =&quot; contactType &quot;/>
    • < xs:element name =&quot; Payment &quot; type =&quot; paymentType &quot;/>
    • </ xs:sequence >
    • </ xs:complexType >
    • </ xs:element >
    • < xs:complexType name =&quot; productsType &quot;>
    • < xs:sequence >
    • < xs:element name =&quot; Product &quot; minOccurs =&quot; 1 &quot; maxOccurs =&quot; unbounded &quot; type =&quot; productType &quot;/>
    • </ xs:sequence >
    • </ xs:complexType >
    • < xs:complexType name =&quot; productType &quot;>
    • < xs:sequence >
    • < xs:element name =&quot; Product_ID &quot; type =&quot; xs:string &quot;/>
    • < xs:element name =&quot; Product_Name &quot; type =&quot; xs:string &quot;/>
    • < xs:element name =&quot; Product_Units &quot; type =&quot; xs:positiveInteger &quot;/>
    • < xs:element name =&quot; Product_Price &quot; type =&quot; xs:int &quot;/>
    • </ xs:sequence >
    • </ xs:complexType >
    • < xs:complexType name =&quot; contactType &quot;>
    • < xs:sequence >
    • < xs:element name =&quot; Name &quot; type =&quot; xs:string &quot;/>
    • < xs:element name =&quot; Address &quot; type =&quot; xs:string &quot;/>
    • < xs:element name =&quot; City &quot; type =&quot; xs:string &quot;/>
    • < xs:element name =&quot; Pin &quot; type =&quot; xs:int &quot;/>
    • </ xs:sequence >
    • </ xs:complexType >
    • < xs:complexType name =&quot; paymentType &quot;>
    • < xs:sequence >
    • < xs:choice >
    • < xs:element name =&quot; CreditCard &quot; type =&quot; creditCardType &quot;/>
    • < xs:element name =&quot; Cheque &quot; type =&quot; chequeType &quot;/>
    • </ xs:choice >
    • < xs:element name =&quot; Amount &quot; type =&quot; xs:positiveInteger &quot;/>
    • </ xs:sequence >
    • </ xs:complexType >
    • < xs:complexType name =&quot; creditCardType &quot;>
    • < xs:sequence >
    • < xs:element name =&quot; CardType &quot; type =&quot; cardTypeRestriction &quot;/>
    • < xs:element name =&quot; CardNumber &quot; type =&quot; xs:int &quot;/>
    • < xs:element name =&quot; ExpiryDate &quot; type =&quot; xs:date &quot;/>
    • </ xs:sequence >
    • </ xs:complexType >
    • < xs:simpleType name =&quot; cardTypeRestriction &quot;>
    • < xs:restriction base =&quot; xs:string &quot;>
    • < xs:enumeration value =&quot; VISA &quot;/>
    • < xs:enumeration value =&quot; MASTER &quot;/>
    • < xs:enumeration value =&quot; AMEX &quot;/>
    • </ xs:restriction >
    • </ xs:simpleType >
    • < xs:complexType name =&quot; chequeType &quot;>
    • < xs:sequence >
    • < xs:element name =&quot; ChequeNumber &quot; type =&quot; xs:positiveInteger &quot;/>
    • < xs:element name =&quot; IssuingBank &quot; type =&quot; xs:string &quot;/>
    • < xs:element name =&quot; IssuingBranch &quot; type =&quot; xs:string &quot;/>
    • < xs:element name =&quot; Dated &quot; type =&quot; xs:date &quot;/>
    • </ xs:sequence >
    • </ xs:complexType >
    • </ xs:schema >
  • 108. Exercise – 2
    • Keep information about the scores of a cricket team in a match in the following form:
      • Team name
      • Opposition team name
      • Innings (1 or 2)
        • Batsman’s details
          • Batting position (1 to 11)
          • Batsman name
          • How out (either Caught, Bowled, LBW, or Not Out)
          • Bowler’s name
          • Runs scored (0 to 400)
  • 109. Thank you! Any Questions?