SlideShare a Scribd company logo
XML Namespaces and XML Schema Atul Kahate [email_address]
XML Namespaces
Need for Namespaces XML allows document authors to create custom elements At times, this leads to ambiguity/collisions Example: <subject>Geometry</subject> <subject>Cardiology</subject> The first subject is the one we study in college, the other one in medicine – but there is nothing to differentiate between them
Using Namespaces We can resolve this problem by using namespaces as follows: <highschool:subject>Geometry</highschool:subject> <medicine:subject>Cardiology</medicine:subject> Both  highschool  and  medicine  are  namespace prefixes We can provide any namespace prefix we want, except for the reserve words
Namespace Example <?xml version = &quot;1.0&quot;?> <text:directory xmlns:text = &quot;www.one.com&quot; xmlns:image = &quot;www.two.com&quot;> <text:file filename = &quot;book.xml&quot;> <text:description>A book list</text:description> </text:file> <image:file filename = &quot;book.xml&quot;> <image:description>A funny picture</image:description> <image:size width = &quot;200&quot; height = 100&quot; /> </image:file> </text:directory>
Using Default Namespace <?xml version = &quot;1.0&quot;?> <directory xmlns = &quot;www.one.com&quot; xmlns:image = &quot;www.two.com&quot;> <file filename = &quot;book.xml&quot;> <description>A book list</description> </file> <image:file filename = &quot;book.xml&quot;> <image:description>A funny picture</image:description> <image:size width = &quot;200&quot; height = 100&quot; /> </image:file> </directory> Now,  directory  is an element that has a default namespace (i.e. no prefix attached).
Well-formed and Valid XML
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
XML Schemas
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
DTD Versus Schema: An Example
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 >
DTD Example <!ELEMENT BOOK (BOOK_NAME, AUTHOR, PUBLISHER, CATEGORY)> <!ELEMENT BOOK_NAME (#PCDATA)> <!ELEMENT AUTHOR (#PCDATA)> <!ELEMENT PUBLISHER (#PCDATA)> <!ELEMENT CATEGORY (#PCDATA)>
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 >
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>
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.
Exercise – 2 Modify the above example to split the name into first and last name
Schema Basics and Syntaxes
The  <schema>  Element Root element of every XML schema <?xml version=&quot;1.0&quot;?> <xsd: schema > ... ... </xsd: schema > Usually contains attributes, as shown next
Attributes in the  <schema>  Element <?xml version=&quot;1.0&quot;?> <xsd: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;> ... ... </xsd: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  xsd: 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
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
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 <xsd:element name=&quot;xxx&quot; type=&quot;yyy&quot;/>  Examples <lastname>Tanenbaum</lastname>  <age>60</age>  <dateborn>1945-10-08</dateborn>  Corresponding schema declarations <xsd:element name=&quot;lastname&quot; type=&quot;xsd:string&quot;/> <xsd:element name=&quot;age&quot; type=&quot;xsd:integer&quot;/>  <xsd:element name=&quot;dateborn&quot; type=&quot;xsd:date&quot;/>
Schema Data Types and Default or Constant Values Basic data types xsd:string  xsd:decimal  xsd:integer  xsd:boolean  xsd:date  xsd:time  Declaring default values <xsd:element name=&quot;color&quot; type=&quot;xsd:string&quot; default=&quot;red&quot;/> Declaring constant values <xsd:element name=&quot;color&quot; type=&quot;xsd:string&quot; fixed=&quot;red&quot;/>
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 <xsd:attribute name=&quot;xxx&quot; type=&quot;yyy&quot;/> Example <lastname lang=&quot;EN&quot;>Joshi</lastname>  Corresponding schema declaration <xsd:attribute name=&quot;lang&quot; type=&quot;xsd:string&quot;/>
More on Attributes Default values <xsd:attribute name=&quot;lang&quot; type=&quot;xsd:string&quot; default=&quot;EN&quot;/>  Fixed values <xsd:attribute name=&quot;lang&quot; type=&quot;xsd:string&quot; fixed=&quot;EN&quot;/>  Mandatory attributes <xsd:attribute name=&quot;lang&quot; type=&quot;xsd:string&quot; use=&quot;required&quot;/>  Optional attributes <xsd:attribute name=&quot;lang&quot; type=&quot;xsd:string&quot; use=&quot;optional&quot;/>
Schema Example – See the difference between “ref” and “type”
XML <?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot; ?> <Employee lastUpdated=&quot;2007-04-21&quot;  xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot; xsi:noNamespaceSchemaLocation=&quot;emp_first.xsd&quot;> <!-- <Employee lastUpdated=&quot;2007-04-21&quot;> --> <Name>Manish Potdar</Name> <Contact> <EmpAddress> <Street>12 East Street</Street> <City>Pune</City> <Pin>411001</Pin> <State>Maharashtra</State> </EmpAddress> <HomePhone>02025530834</HomePhone> <Email>manish@yahoo.com</Email> </Contact> </Employee>
Schema <?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?> <xsd:schema xmlns:xs=&quot;http://www.w3.org/2001/XMLSchema&quot; elementFormDefault=&quot;qualified&quot; attributeFormDefault=&quot;unqualified&quot;> <xsd:element name=&quot;Employee&quot;> <xsd:complexType> <xsd:sequence> <xsd:element name=&quot;Name&quot;/> <xsd:element name=&quot;Contact&quot;> <xsd:complexType> <xsd:sequence> <xsd:element name = &quot;EmpAddress&quot; type=&quot;Address&quot;/> <xsd:element name=&quot;HomePhone&quot;/> <xsd:element name=&quot;Email&quot;/> </xsd:sequence> </xsd:complexType> </xsd:element> </xsd:sequence> <xsd:attribute name=&quot;lastUpdated&quot;/> </xsd:complexType> </xsd:element> <xsd:complexType name=&quot;Address&quot;> <xsd:sequence> <xsd:element name=&quot;Street&quot;/> <xsd:element name=&quot;City&quot;/> <xsd:element name=&quot;Pin&quot;/> <xsd:element name=&quot;State&quot;/> </xsd:sequence> </xsd:complexType> </xsd:schema>
Modified XML (emp_first.xml) <?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot; ?> <Employee lastUpdated=&quot;2007-04-21&quot;  xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot; xsi:noNamespaceSchemaLocation=&quot;emp_first.xsd&quot;> <Name>Manish Potdar</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>
Modified Schema (emp_first.xsd) <?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?> <xsd:schema xmlns:xs=&quot;http://www.w3.org/2001/XMLSchema&quot;> <xsd:element name=&quot;Employee&quot;> <xsd:complexType> <xsd:sequence> <xsd:element name=&quot;Name&quot;/> <xsd:element name=&quot;Contact&quot;> <xsd:complexType> <xsd:sequence> <xsd:element ref=&quot;Address&quot;/> <xsd:element name=&quot;HomePhone&quot;/> <xsd:element name=&quot;Email&quot;/> </xsd:sequence> </xsd:complexType> </xsd:element> </xsd:sequence> <xsd:attribute name=&quot;lastUpdated&quot;/> </xsd:complexType> </xsd:element> <xsd:element name=&quot;Address&quot;> <xsd:complexType> <xsd:sequence> <xsd:element name=&quot;Street&quot;/> <xsd:element name=&quot;City&quot;/> <xsd:element name=&quot;Pin&quot;/> <xsd:element name=&quot;State&quot;/> </xsd:sequence> </xsd:complexType> </xsd:element> </xsd:schema>
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.
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>
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
Schema Components
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
Primary Components
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
Declaring Elements Example <xsd:element name = “dateReceived”/> Or, with data type <xsd:element name = “dateReceived” type = “xsd:date”/>
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”/>
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
Simple Types Contain information that cannot be split further Example XML <myElement>I am simple</myElement> Schema < xsd:element  name =&quot; myElement &quot;  type =&quot; xsd:string &quot;/>
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
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” />
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>
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) --> < xsd:schema  xmlns:xs =&quot; http://www.w3.org/2001/XMLSchema &quot;> < xsd:element  name =&quot; ContactDetails &quot;> < xsd:complexType > < xsd:sequence > < xsd:element  ref =&quot; Contact &quot;  minOccurs =&quot; 1 &quot;  maxOccurs =&quot; unbounded &quot;/> </ xsd:sequence > </ xsd:complexType > </ xsd:element > < xsd:element  name =&quot; Contact &quot;> < xsd:complexType > < xsd:sequence > < 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:element > < xsd:element  name =&quot; billingAddress &quot;  type =&quot; Address &quot;/> < xsd:element  name =&quot; mailingAddress &quot;  type =&quot; Address &quot;/> </ xsd:sequence > </ xsd:complexType > </ xsd:element > < xsd:complexType  name =&quot; Address &quot;> < xsd:sequence > < xsd:element  name =&quot; Street1 &quot;/> < xsd:element  name =&quot; Street1 &quot;/> < xsd:element  name =&quot; City &quot;/> < xsd:element  name =&quot; Pin &quot;/> < xsd:element  name =&quot; State &quot;/> </ xsd:sequence > </ xsd:complexType > </ xsd:schema >
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) --> < xsd:schema  xmlns:xs =&quot; http://www.w3.org/2001/XMLSchema &quot;> < xsd:element  name =&quot; ContactDetails &quot;> < xsd:complexType > < xsd:sequence > < xsd:element  ref =&quot; Contact &quot;  minOccurs =&quot; 1 &quot;  maxOccurs =&quot; unbounded &quot;/> </ xsd:sequence > </ xsd:complexType > </ xsd:element > < xsd:element  name =&quot; Contact &quot;> < xsd:complexType > < xsd:sequence > < 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:element > < xsd:element  name =&quot; billingAddress &quot;  type =&quot; Address &quot;/> < xsd:element  name =&quot; mailingAddress &quot;  type =&quot; Address &quot;/> < xsd:element  name =&quot; CreditAddressReference &quot;> < xsd:complexType > < xsd:complexContent > < xsd:extension  base =&quot; Address &quot;> < xsd:attribute  name =&quot; residentFrom &quot;  type =&quot; xsd:date &quot;/> < xsd:attribute  name =&quot; residentTo &quot;  type =&quot; xsd:date &quot;/> </ xsd:extension > </ xsd:complexContent > </ xsd:complexType > </ xsd:element > </ xsd:sequence > </ xsd:complexType > </ xsd:element > < xsd:complexType  name =&quot; Address &quot;> < xsd:sequence > < xsd:element  name =&quot; Street1 &quot;/> < xsd:element  name =&quot; Street1 &quot;/> < xsd:element  name =&quot; City &quot;/> < xsd:element  name =&quot; Pin &quot;/> < xsd:element  name =&quot; State &quot;/> </ xsd:sequence > </ xsd:complexType > </ xsd:schema >
Declaring Attributes <xsd:element name = “invoice”> <xsd:complexType> <xsd:attribute name = “paid” use =  “optional” default = “true”> </xsd:complexType> </xsd:element>
Secondary Components
Secondary Components Can be classified into Model group definitions Attribute groups Notation declarations Identity constraints
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 )
group example <?xml version=&quot;1.0&quot;?> <xsd:schema xmlns:xs=&quot;http://www.w3.org/2001/XMLSchema&quot;> <xsd:group name=&quot;custGroup&quot;> <xsd:sequence> <xsd:element name=&quot;customer&quot; type=&quot;xsd:string&quot;/> <xsd:element name=&quot;orderdetails&quot; type=&quot;xsd:string&quot;/> <xsd:element name=&quot;billto&quot; type=&quot;xsd:string&quot;/> <xsd:element name=&quot;shipto&quot; type=&quot;xsd:string&quot;/> </xsd:sequence> </xsd:group> <xsd:element name=&quot;order&quot; type=&quot;ordertype&quot;/> <xsd:complexType name=&quot;ordertype&quot;> <xsd:group ref=&quot;custGroup&quot;/> <xsd:attribute name=&quot;status&quot; type=&quot;xsd:string&quot;/> </xsd:complexType> </xsd:schema>
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) --> < xsd:schema  xmlns:xs =&quot; http://www.w3.org/2001/XMLSchema &quot;> < xsd:element  name =&quot; FlightDetails &quot;> < xsd:complexType > < xsd:sequence > < xsd:element  name =&quot; Name &quot;/> < xsd:group  ref =&quot; MealOptions &quot;/> </ xsd:sequence > </ xsd:complexType > </ xsd:element > < xsd:group  name =&quot; MealOptions &quot;> < xsd:choice > < xsd:element  name =&quot; Vegetarian &quot;/> < xsd:element  name =&quot; Non-Vegetarian &quot;/> < xsd:element  name =&quot; Salad &quot;/> </ xsd:choice > </ xsd:group > </ xsd:schema >
Choice Example – Using Unnamed Syntax <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>
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) --> < xsd:schema  xmlns:xs =&quot; http://www.w3.org/2001/XMLSchema &quot;> < xsd:group  name =&quot; person &quot;> < xsd:all > < xsd:element  name =&quot; firstname &quot;  type =&quot; xsd:string &quot;/> < xsd:element  name =&quot; lastname &quot;  type =&quot; xsd:string &quot;/> </ xsd:all > </ xsd:group > < xsd:element  name =&quot; test &quot;> < xsd:complexType > < xsd:group  ref  =&quot; person &quot;   /> </ xsd:complexType > </ xsd:element > </ xsd:schema >
All Example – Using Unnamed Syntax <xsd:element name=&quot;person&quot;> <xsd:complexType> <xsd:all> <xsd:element name=&quot;firstname&quot; type=&quot;xsd:string&quot;/> <xsd:element name=&quot;lastname&quot; type=&quot;xsd:string&quot;/> </xsd:all> </xsd:complexType> </xsd:element> firstname and lastname are mandatory sub-elements – must occur exactly once
All – Another Example <xsd:group name = “CreditCardDetails”> <xsd:all> <xsd:element name = “CardType” /> <xsd:element name = “CardHolder” /> <xsd:element name = “CardNumber” /> <xsd:element name = “CardExpiry” /> </xsd:all> </xsd:group>
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) --> < xsd:schema  xmlns:xs =&quot; http://www.w3.org/2001/XMLSchema &quot;> < xsd:group  name =&quot; person &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:group > < xsd:element  name =&quot; test &quot;> < xsd:complexType > < xsd:group  ref  =&quot; person &quot;   /> </ xsd:complexType > </ xsd:element > </ xsd:schema >
Sequence Example Using Unnamed Syntax Another element that we can use inside a  group  is  sequence Example <xsd:group name=&quot;person&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>
Other Details We can use minOccurs, maxOccurs attributes on group, choice, sequence, and all elements.
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
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) --> < xsd:schema  xmlns:xs =&quot; http://www.w3.org/2001/XMLSchema &quot;> < xsd:element  name =&quot; product &quot;> < xsd:complexType > < xsd:attributeGroup  ref =&quot; productDetails &quot;/> </ xsd:complexType > </ xsd:element > < xsd:attributeGroup  name =&quot; productDetails &quot;> < xsd:attribute  name =&quot; productID &quot;  use =&quot; required &quot;  type =&quot; xsd:string &quot;/> < xsd:attribute  name =&quot; productName &quot;  use =&quot; required &quot;  type =&quot; xsd:string &quot;/> < xsd:attribute  name =&quot; productDescription &quot;  use =&quot; required &quot;  type =&quot; xsd:string &quot;/> < xsd:attribute  name =&quot; unit &quot;  use =&quot; required &quot;  type =&quot; xsd:positiveInteger &quot;/> < xsd:attribute  name =&quot; price &quot;  use =&quot; required &quot;  type =&quot; xsd:decimal &quot;/> < xsd:attribute  name =&quot; stock &quot;  use =&quot; required &quot;  type =&quot; xsd:integer &quot;/> </ xsd:attributeGroup > </ xsd:schema >
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) --> < xsd:schema  xmlns:xs =&quot; http://www.w3.org/2001/XMLSchema &quot;> < xsd:element  name =&quot; product &quot;  default =&quot; book &quot;   /> </ xsd:schema >
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:\Users\Atul\Desktop\test.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:\Users\Atul\Desktop\test.xsd &quot;> </ product >
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) --> < xsd:schema  xmlns:xs =&quot; http://www.w3.org/2001/XMLSchema &quot;> < xsd:element  name =&quot; product &quot;  fixed =&quot; book &quot;   /> </ xsd:schema > Now, the value of the element in the XML document must either be  book , or it should be empty
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
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) --> < xsd:schema  xmlns:xs =&quot; http://www.w3.org/2001/XMLSchema &quot;> < xsd:element  name =&quot; product &quot;> < xsd:complexType > < xsd:sequence > < xsd:element  name =&quot; productID &quot;/> < xsd:element  name =&quot; productName &quot;  nillable =&quot; true &quot;/> </ xsd:sequence > </ xsd:complexType > </ xsd:element > </ xsd:schema >
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:\Users\Atul\Desktop\test.xsd &quot;> < productID > test </ productID > < productName  xsi:nil =&quot; true &quot;/> </ product >
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
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
Schema Facets
Schema Facets (Restrictions) - 1 Restriction on values <xsd:element name=&quot;age&quot;> <xsd:simpleType> <xsd:restriction base=&quot;xsd:integer&quot;> <xsd:minInclusive value=&quot;0&quot;/> <xsd:maxInclusive value=&quot;100&quot;/> </xsd:restriction> </xsd:simpleType> </xsd:element>
Schema Facets (Restrictions) - 2 Restriction on a set of values <xsd:element name=&quot;car“  / > <xsd:simpleType> <xsd:restriction base=&quot;xsd:string&quot;> <xsd:enumeration value=“Santro/> <xsd:enumeration value=“Maruti&quot;/> <xsd:enumeration value=“Indica/> </xsd:restriction> </xsd:simpleType> </xsd:element>
Schema Facets (Restrictions) - 3 Restrictions on a series of values <xsd:element name=&quot;letter&quot;> <xsd:simpleType> <xsd:restriction base=&quot;xsd:string&quot;> <xsd:pattern value=&quot;[a-z]&quot;/> </xsd:restriction> </xsd:simpleType> </xsd:element>   Allows one lowercase alphabet <xsd:pattern value=&quot;[A-Z][A-Z][A-Z]&quot;/> Allows three uppercase alphabets <xsd:pattern value=&quot;[a-zA-Z][a-zA-Z][a-zA-Z]&quot;/>   Allows three uppercase or lowercase alphabets <xsd:pattern value=&quot;[xyz]&quot;/>   Allows one of the lowercase alphabets x, y, or z <xsd:pattern value=&quot;[0-9][0-9][0-9][0-9][0-9]&quot;/>   Allows five digits in sequence
Schema Facets (Restrictions) - 4 Other restrictions on a series of values <xsd:element name=&quot;letter&quot;> <xsd:simpleType> <xsd:restriction base=&quot;xsd:string&quot;> <xsd:pattern value=&quot;([a-z])*&quot;/> </xsd:restriction> </xsd:simpleType> </xsd:element>   Allows 0 or more lowercase alphabets <xsd:pattern value=&quot;([a-z][A-Z])+&quot;/>   One or more lowercase alphabets followed by an uppercase alphabet <xsd:pattern value=“worker|manager&quot;/>  Allow either worker or manager <xsd:pattern value=&quot;[a-zA-Z0-9]{8}&quot;/>   Allow exactly 8 characters, either alphabets or numbers <xsd:length value=&quot;8&quot;/>   Allow exactly 8 characters <xsd:minLength value=&quot;5&quot;/> <xsd:maxLength value=&quot;8&quot;/> Allow a minimum of 5 and maximum of 8 characters
Schema: Case Study
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)
Start with the Schema Template <?xml version=&quot;1.0&quot;?> <xsd:schema xmlns:xs=&quot;http://www.w3.org/2001/XMLSchema&quot;> ... ... </xsd:schema>
Create the  shiporder  Element Outline <xsd:element name=&quot;shiporder&quot;> <xsd:complexType> <xsd:sequence> ... ... </xsd:sequence> ... </xsd:complexType> </xsd:element>
Define the  orderperson  Element <xsd:element name=&quot;orderperson&quot; type=&quot;xsd:string&quot;/>
Define the  shipto  and  item  Elements <xsd:element name=&quot;shipto&quot;> <xsd:complexType> <xsd:sequence> <xsd:element name=&quot;name&quot; type=&quot;xsd:string&quot;/> <xsd:element name=&quot;address&quot; type=&quot;xsd:string&quot;/> <xsd:element name=&quot;city&quot; type=&quot;xsd:string&quot;/> <xsd:element name=&quot;country&quot; type=&quot;xsd:string&quot;/> </xsd:sequence> </xsd:complexType> </xsd:element> <xsd:element name=&quot;item&quot; maxOccurs=&quot;unbounded&quot;> <xsd:complexType> <xsd:sequence> <xsd:element name=&quot;title&quot; type=&quot;xsd:string&quot;/> <xsd:element name=&quot;note&quot; type=&quot;xsd:string&quot; minOccurs=&quot;0&quot;/> <xsd:element name=&quot;quantity&quot; type=&quot;xsd:positiveInteger&quot;/> <xsd:element name=&quot;price&quot; type=&quot;xsd:decimal&quot;/> </xsd:sequence> </xsd:complexType> </xsd:element>
Declare the attribute for the  shiporder  Element <xsd:attribute name=&quot;orderid&quot; type=&quot;xsd:string&quot; use=&quot;required&quot;/>
The Complete Schema <?xml version=&quot;1.0&quot; encoding=&quot;ISO-8859-1&quot; ?> <xsd:schema xmlns:xs=&quot;http://www.w3.org/2001/XMLSchema&quot;> <xsd:element name=&quot;shiporder&quot;> <xsd:complexType> <xsd:sequence> <xsd:element name=&quot;orderperson&quot; type=&quot;xsd:string&quot;/> <xsd:element name=&quot;shipto&quot;> <xsd:complexType> <xsd:sequence> <xsd:element name=&quot;name&quot; type=&quot;xsd:string&quot;/> <xsd:element name=&quot;address&quot; type=&quot;xsd:string&quot;/> <xsd:element name=&quot;city&quot; type=&quot;xsd:string&quot;/> <xsd:element name=&quot;country&quot; type=&quot;xsd:string&quot;/> </xsd:sequence> </xsd:complexType> </xsd:element> <xsd:element name=&quot;item&quot; maxOccurs=&quot;unbounded&quot;> <xsd:complexType> <xsd:sequence> <xsd:element name=&quot;title&quot; type=&quot;xsd:string&quot;/> <xsd:element name=&quot;note&quot; type=&quot;xsd:string&quot; minOccurs=&quot;0&quot;/> <xsd:element name=&quot;quantity&quot; type=&quot;xsd:positiveInteger&quot;/> <xsd:element name=&quot;price&quot; type=&quot;xsd:decimal&quot;/> </xsd:sequence> </xsd:complexType> </xsd:element> </xsd:sequence> <xsd:attribute name=&quot;orderid&quot; type=&quot;xsd:string&quot; use=&quot;required&quot;/> </xsd:complexType> </xsd:element> </xsd:schema>
Making it more readable <?xml version=&quot;1.0&quot; encoding=&quot;ISO-8859-1&quot; ?> <xsd:schema xmlns:xs=&quot;http://www.w3.org/2001/XMLSchema&quot;> <!-- definition of simple elements --> <xsd:element name=&quot;orderperson&quot; type=&quot;xsd:string&quot;/> <xsd:element name=&quot;name&quot; type=&quot;xsd:string&quot;/> <xsd:element name=&quot;address&quot; type=&quot;xsd:string&quot;/> <xsd:element name=&quot;city&quot; type=&quot;xsd:string&quot;/> <xsd:element name=&quot;country&quot; type=&quot;xsd:string&quot;/> <xsd:element name=&quot;title&quot; type=&quot;xsd:string&quot;/> <xsd:element name=&quot;note&quot; type=&quot;xsd:string&quot;/> <xsd:element name=&quot;quantity&quot; type=&quot;xsd:positiveInteger&quot;/> <xsd:element name=&quot;price&quot; type=&quot;xsd:decimal&quot;/> <!-- definition of attributes --> <xsd:attribute name=&quot;orderid&quot; type=&quot;xsd:string&quot;/> <!-- definition of complex elements --> <xsd:element name=&quot;shipto&quot;> <xsd:complexType> <xsd:sequence> <xsd:element ref=&quot;name&quot;/> <xsd:element ref=&quot;address&quot;/> <xsd:element ref=&quot;city&quot;/> <xsd:element ref=&quot;country&quot;/> </xsd:sequence> </xsd:complexType> </xsd:element> <xsd:element name=&quot;item&quot;> <xsd:complexType> <xsd:sequence> <xsd:element ref=&quot;title&quot;/> <xsd:element ref=&quot;note&quot; minOccurs=&quot;0&quot;/> <xsd:element ref=&quot;quantity&quot;/> <xsd:element ref=&quot;price&quot;/> </xsd:sequence> </xsd:complexType> </xsd:element> <xsd:element name=&quot;shiporder&quot;> <xsd:complexType> <xsd:sequence> <xsd:element ref=&quot;orderperson&quot;/> <xsd:element ref=&quot;shipto&quot;/> <xsd:element ref=&quot;item&quot; maxOccurs=&quot;unbounded&quot;/> </xsd:sequence> <xsd:attribute ref=&quot;orderid&quot; use=&quot;required&quot;/> </xsd:complexType> </xsd:element> </xsd:schema>
Adding more features <?xml version=&quot;1.0&quot; encoding=&quot;ISO-8859-1&quot; ?> <xsd:schema xmlns:xs=&quot;http://www.w3.org/2001/XMLSchema&quot;> <xsd:simpleType name=&quot;stringtype&quot;> <xsd:restriction base=&quot;xsd:string&quot;/> </xsd:simpleType> <xsd:simpleType name=&quot;inttype&quot;> <xsd:restriction base=&quot;xsd:positiveInteger&quot;/> </xsd:simpleType> <xsd:simpleType name=&quot;dectype&quot;> <xsd:restriction base=&quot;xsd:decimal&quot;/> </xsd:simpleType> <xsd:simpleType name=&quot;orderidtype&quot;> <xsd:restriction base=&quot;xsd:string&quot;> <xsd:pattern value=&quot;[0-9]{6}&quot;/> </xsd:restriction> </xsd:simpleType> <xsd:complexType name=&quot;shiptotype&quot;> <xsd:sequence> <xsd:element name=&quot;name&quot; type=&quot;stringtype&quot;/> <xsd:element name=&quot;address&quot; type=&quot;stringtype&quot;/> <xsd:element name=&quot;city&quot; type=&quot;stringtype&quot;/> <xsd:element name=&quot;country&quot; type=&quot;stringtype&quot;/> </xsd:sequence> </xsd:complexType> <xsd:complexType name=&quot;itemtype&quot;> <xsd:sequence> <xsd:element name=&quot;title&quot; type=&quot;stringtype&quot;/> <xsd:element name=&quot;note&quot; type=&quot;stringtype&quot; minOccurs=&quot;0&quot;/> <xsd:element name=&quot;quantity&quot; type=&quot;inttype&quot;/> <xsd:element name=&quot;price&quot; type=&quot;dectype&quot;/> </xsd:sequence> </xsd:complexType> <xsd:complexType name=&quot;shipordertype&quot;> <xsd:sequence> <xsd:element name=&quot;orderperson&quot; type=&quot;stringtype&quot;/> <xsd:element name=&quot;shipto&quot; type=&quot;shiptotype&quot;/> <xsd:element name=&quot;item&quot; maxOccurs=&quot;unbounded&quot; type=&quot;itemtype&quot;/> </xsd:sequence> <xsd:attribute name=&quot;orderid&quot; type=&quot;orderidtype&quot; use=&quot;required&quot;/> </xsd:complexType> <xsd:element name=&quot;shiporder&quot; type=&quot;shipordertype&quot;/> </xsd:schema>
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:\Users\Atul\Desktop\test.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 >
Solution – 1 <?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?> <!-- W3C Schema generated by XMLSpy v2007 sp2 (http://www.altova.com) --> < xsd:schema  xmlns:xs =&quot; http://www.w3.org/2001/XMLSchema &quot;> < xsd:element  name =&quot; Order &quot;> < xsd:complexType > < xsd:sequence > < xsd:element  name =&quot; Products &quot;  type =&quot; productsType &quot;/> < xsd:element  name =&quot; Contact &quot;  type =&quot; contactType &quot;/> < xsd:element  name =&quot; Payment &quot;  type =&quot; paymentType &quot;/> </ xsd:sequence > </ xsd:complexType > </ xsd:element > < xsd:complexType  name =&quot; productsType &quot;> < xsd:sequence > < xsd:element  name =&quot; Product &quot;  minOccurs =&quot; 1 &quot;  maxOccurs =&quot; unbounded &quot;  type =&quot; productType &quot;/> </ xsd:sequence > </ xsd:complexType > < xsd:complexType  name =&quot; productType &quot;> < xsd:sequence > < xsd:element  name =&quot; Product_ID &quot;  type =&quot; xsd:string &quot;/> < xsd:element  name =&quot; Product_Name &quot;  type =&quot; xsd:string &quot;/> < xsd:element  name =&quot; Product_Units &quot;  type =&quot; xsd:positiveInteger &quot;/> < xsd:element  name =&quot; Product_Price &quot;  type =&quot; xsd:int &quot;/> </ xsd:sequence > </ xsd:complexType > < xsd:complexType  name =&quot; contactType &quot;> < xsd:sequence > < xsd:element  name =&quot; Name &quot;  type =&quot; xsd:string &quot;/> < xsd:element  name =&quot; Address &quot;  type =&quot; xsd:string &quot;/> < xsd:element  name =&quot; City &quot;  type =&quot; xsd:string &quot;/> < xsd:element  name =&quot; Pin &quot;  type =&quot; xsd:int &quot;/> </ xsd:sequence > </ xsd:complexType > < xsd:complexType  name =&quot; paymentType &quot;> < xsd:sequence > < xsd:choice > < xsd:element  name =&quot; CreditCard &quot;  type =&quot; creditCardType &quot;/> < xsd:element  name =&quot; Cheque &quot;  type =&quot; chequeType &quot;/> </ xsd:choice > < xsd:element  name =&quot; Amount &quot;  type =&quot; xsd:positiveInteger &quot;/> </ xsd:sequence > </ xsd:complexType > < xsd:complexType  name =&quot; creditCardType &quot;> < xsd:sequence > < xsd:element  name =&quot; CardType &quot;  type =&quot; cardTypeRestriction &quot;/> < xsd:element  name =&quot; CardNumber &quot;  type =&quot; xsd:int &quot;/> < xsd:element  name =&quot; ExpiryDate &quot;  type =&quot; xsd:date &quot;/> </ xsd:sequence > </ xsd:complexType > < xsd:simpleType  name =&quot; cardTypeRestriction &quot;> < xsd:restriction  base =&quot; xsd:string &quot;> < xsd:enumeration  value =&quot; VISA &quot;/> < xsd:enumeration  value =&quot; MASTER &quot;/> < xsd:enumeration  value =&quot; AMEX &quot;/> </ xsd:restriction > </ xsd:simpleType > < xsd:complexType  name =&quot; chequeType &quot;> < xsd:sequence > < xsd:element  name =&quot; ChequeNumber &quot;  type =&quot; xsd:positiveInteger &quot;/> < xsd:element  name =&quot; IssuingBank &quot;  type =&quot; xsd:string &quot;/> < xsd:element  name =&quot; IssuingBranch &quot;  type =&quot; xsd:string &quot;/> < xsd:element  name =&quot; Dated &quot;  type =&quot; xsd:date &quot;/> </ xsd:sequence > </ xsd:complexType > </ xsd:schema >
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)
XML Schemas: Case Study
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>
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 ”>
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
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
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>
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>
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.
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>
Thank you! Any Questions?

More Related Content

PPT
Sliding window protocol
PPTX
Application layer protocols
PPT
Circuit and packet_switching
PPTX
Computer Network - Network Layer
PDF
Transport layer services
PPTX
Constructor ppt
Sliding window protocol
Application layer protocols
Circuit and packet_switching
Computer Network - Network Layer
Transport layer services
Constructor ppt

What's hot (20)

PPTX
HDLC and Point to point protocol
PPTX
arrays and pointers
PPT
IPV4 Frame Format
PPTX
Presentation on c structures
PPTX
Multiple access protocol
PPTX
Document Object Model (DOM)
PPT
Lecture 01 introduction to database
PPTX
presentation in html,css,javascript
PPT
PPT
Domain name system
PPTX
Xml presentation
PPTX
Introduction to HTML
PPTX
Html formatting
PPTX
Data members and member functions
PPTX
Command line arguments
PPTX
Introduction to TCL Programming : Tcl/Tk by Gaurav Roy
PPTX
INLINE FUNCTION IN C++
PPTX
PPT on Basic of Gateway
PPT
Class and object in C++
PPT
Php Ppt
HDLC and Point to point protocol
arrays and pointers
IPV4 Frame Format
Presentation on c structures
Multiple access protocol
Document Object Model (DOM)
Lecture 01 introduction to database
presentation in html,css,javascript
Domain name system
Xml presentation
Introduction to HTML
Html formatting
Data members and member functions
Command line arguments
Introduction to TCL Programming : Tcl/Tk by Gaurav Roy
INLINE FUNCTION IN C++
PPT on Basic of Gateway
Class and object in C++
Php Ppt
Ad

Similar to 3 xml namespaces and xml schema (20)

PPT
Xml Schema
PPT
XML and Web Services with PHP5 and PEAR
PPT
XML Schema
PPT
PPTX
35 schemas
PPT
PPT
Introduction To Xml
PPT
PPT
XML processing with perl
PPT
Digital + Container List
PPT
Everything You Always Wanted To Know About XML But Were Afraid To Ask
PPT
Introduction to XML
PPT
2 dtd - validating xml documents
PPTX
What is xml
PPT
Relax NG, a Schema Language for XML
PPT
PPT
PPTX
Xml For Dummies Chapter 12 Handling Transformations With Xsl it-slideshares...
PPT
XML and XSLT
PPT
Inroduction to XSLT with PHP4
Xml Schema
XML and Web Services with PHP5 and PEAR
XML Schema
35 schemas
Introduction To Xml
XML processing with perl
Digital + Container List
Everything You Always Wanted To Know About XML But Were Afraid To Ask
Introduction to XML
2 dtd - validating xml documents
What is xml
Relax NG, a Schema Language for XML
Xml For Dummies Chapter 12 Handling Transformations With Xsl it-slideshares...
XML and XSLT
Inroduction to XSLT with PHP4
Ad

More from gauravashq (17)

DOC
Spring.pdf
PDF
Spring
PDF
Spring
PDF
Spring
PDF
PPT
6 xml parsing
PPT
5 xsl (formatting xml documents)
PPT
5 xml parsing
PPT
4 xslt
PPT
4 xml namespaces and xml schema
PPT
1 introduction to xml
PPT
1 electronic data interchange (edi)
PDF
AK 5 JSF 21 july 2008
PDF
AK 4 JSF
PPT
AK 3 web services using apache axis
PPT
AK css
PPT
AK html
Spring.pdf
Spring
Spring
Spring
6 xml parsing
5 xsl (formatting xml documents)
5 xml parsing
4 xslt
4 xml namespaces and xml schema
1 introduction to xml
1 electronic data interchange (edi)
AK 5 JSF 21 july 2008
AK 4 JSF
AK 3 web services using apache axis
AK css
AK html

Recently uploaded (20)

PPTX
Microbial diseases, their pathogenesis and prophylaxis
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PDF
01-Introduction-to-Information-Management.pdf
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PDF
Anesthesia in Laparoscopic Surgery in India
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PPTX
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
PDF
Pre independence Education in Inndia.pdf
PDF
Basic Mud Logging Guide for educational purpose
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PPTX
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PPTX
BOWEL ELIMINATION FACTORS AFFECTING AND TYPES
PDF
Complications of Minimal Access Surgery at WLH
PDF
O7-L3 Supply Chain Operations - ICLT Program
PDF
Sports Quiz easy sports quiz sports quiz
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PPTX
master seminar digital applications in india
Microbial diseases, their pathogenesis and prophylaxis
Module 4: Burden of Disease Tutorial Slides S2 2025
01-Introduction-to-Information-Management.pdf
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
Anesthesia in Laparoscopic Surgery in India
Final Presentation General Medicine 03-08-2024.pptx
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
Pre independence Education in Inndia.pdf
Basic Mud Logging Guide for educational purpose
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
BOWEL ELIMINATION FACTORS AFFECTING AND TYPES
Complications of Minimal Access Surgery at WLH
O7-L3 Supply Chain Operations - ICLT Program
Sports Quiz easy sports quiz sports quiz
2.FourierTransform-ShortQuestionswithAnswers.pdf
human mycosis Human fungal infections are called human mycosis..pptx
Pharmacology of Heart Failure /Pharmacotherapy of CHF
master seminar digital applications in india

3 xml namespaces and xml schema

  • 1. XML Namespaces and XML Schema Atul Kahate [email_address]
  • 3. Need for Namespaces XML allows document authors to create custom elements At times, this leads to ambiguity/collisions Example: <subject>Geometry</subject> <subject>Cardiology</subject> The first subject is the one we study in college, the other one in medicine – but there is nothing to differentiate between them
  • 4. Using Namespaces We can resolve this problem by using namespaces as follows: <highschool:subject>Geometry</highschool:subject> <medicine:subject>Cardiology</medicine:subject> Both highschool and medicine are namespace prefixes We can provide any namespace prefix we want, except for the reserve words
  • 5. Namespace Example <?xml version = &quot;1.0&quot;?> <text:directory xmlns:text = &quot;www.one.com&quot; xmlns:image = &quot;www.two.com&quot;> <text:file filename = &quot;book.xml&quot;> <text:description>A book list</text:description> </text:file> <image:file filename = &quot;book.xml&quot;> <image:description>A funny picture</image:description> <image:size width = &quot;200&quot; height = 100&quot; /> </image:file> </text:directory>
  • 6. Using Default Namespace <?xml version = &quot;1.0&quot;?> <directory xmlns = &quot;www.one.com&quot; xmlns:image = &quot;www.two.com&quot;> <file filename = &quot;book.xml&quot;> <description>A book list</description> </file> <image:file filename = &quot;book.xml&quot;> <image:description>A funny picture</image:description> <image:size width = &quot;200&quot; height = 100&quot; /> </image:file> </directory> Now, directory is an element that has a default namespace (i.e. no prefix attached).
  • 8. 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
  • 10. 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
  • 11. DTD Versus Schema: An Example
  • 12. 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 >
  • 13. DTD Example <!ELEMENT BOOK (BOOK_NAME, AUTHOR, PUBLISHER, CATEGORY)> <!ELEMENT BOOK_NAME (#PCDATA)> <!ELEMENT AUTHOR (#PCDATA)> <!ELEMENT PUBLISHER (#PCDATA)> <!ELEMENT CATEGORY (#PCDATA)>
  • 14. 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 >
  • 15. 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>
  • 16. 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.
  • 17. Exercise – 2 Modify the above example to split the name into first and last name
  • 18. Schema Basics and Syntaxes
  • 19. The <schema> Element Root element of every XML schema <?xml version=&quot;1.0&quot;?> <xsd: schema > ... ... </xsd: schema > Usually contains attributes, as shown next
  • 20. Attributes in the <schema> Element <?xml version=&quot;1.0&quot;?> <xsd: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;> ... ... </xsd: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 xsd: 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
  • 21. 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
  • 22. 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 <xsd:element name=&quot;xxx&quot; type=&quot;yyy&quot;/> Examples <lastname>Tanenbaum</lastname> <age>60</age> <dateborn>1945-10-08</dateborn> Corresponding schema declarations <xsd:element name=&quot;lastname&quot; type=&quot;xsd:string&quot;/> <xsd:element name=&quot;age&quot; type=&quot;xsd:integer&quot;/> <xsd:element name=&quot;dateborn&quot; type=&quot;xsd:date&quot;/>
  • 23. Schema Data Types and Default or Constant Values Basic data types xsd:string xsd:decimal xsd:integer xsd:boolean xsd:date xsd:time Declaring default values <xsd:element name=&quot;color&quot; type=&quot;xsd:string&quot; default=&quot;red&quot;/> Declaring constant values <xsd:element name=&quot;color&quot; type=&quot;xsd:string&quot; fixed=&quot;red&quot;/>
  • 24. 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 <xsd:attribute name=&quot;xxx&quot; type=&quot;yyy&quot;/> Example <lastname lang=&quot;EN&quot;>Joshi</lastname> Corresponding schema declaration <xsd:attribute name=&quot;lang&quot; type=&quot;xsd:string&quot;/>
  • 25. More on Attributes Default values <xsd:attribute name=&quot;lang&quot; type=&quot;xsd:string&quot; default=&quot;EN&quot;/> Fixed values <xsd:attribute name=&quot;lang&quot; type=&quot;xsd:string&quot; fixed=&quot;EN&quot;/> Mandatory attributes <xsd:attribute name=&quot;lang&quot; type=&quot;xsd:string&quot; use=&quot;required&quot;/> Optional attributes <xsd:attribute name=&quot;lang&quot; type=&quot;xsd:string&quot; use=&quot;optional&quot;/>
  • 26. Schema Example – See the difference between “ref” and “type”
  • 27. XML <?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot; ?> <Employee lastUpdated=&quot;2007-04-21&quot; xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot; xsi:noNamespaceSchemaLocation=&quot;emp_first.xsd&quot;> <!-- <Employee lastUpdated=&quot;2007-04-21&quot;> --> <Name>Manish Potdar</Name> <Contact> <EmpAddress> <Street>12 East Street</Street> <City>Pune</City> <Pin>411001</Pin> <State>Maharashtra</State> </EmpAddress> <HomePhone>02025530834</HomePhone> <Email>manish@yahoo.com</Email> </Contact> </Employee>
  • 28. Schema <?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?> <xsd:schema xmlns:xs=&quot;http://www.w3.org/2001/XMLSchema&quot; elementFormDefault=&quot;qualified&quot; attributeFormDefault=&quot;unqualified&quot;> <xsd:element name=&quot;Employee&quot;> <xsd:complexType> <xsd:sequence> <xsd:element name=&quot;Name&quot;/> <xsd:element name=&quot;Contact&quot;> <xsd:complexType> <xsd:sequence> <xsd:element name = &quot;EmpAddress&quot; type=&quot;Address&quot;/> <xsd:element name=&quot;HomePhone&quot;/> <xsd:element name=&quot;Email&quot;/> </xsd:sequence> </xsd:complexType> </xsd:element> </xsd:sequence> <xsd:attribute name=&quot;lastUpdated&quot;/> </xsd:complexType> </xsd:element> <xsd:complexType name=&quot;Address&quot;> <xsd:sequence> <xsd:element name=&quot;Street&quot;/> <xsd:element name=&quot;City&quot;/> <xsd:element name=&quot;Pin&quot;/> <xsd:element name=&quot;State&quot;/> </xsd:sequence> </xsd:complexType> </xsd:schema>
  • 29. Modified XML (emp_first.xml) <?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot; ?> <Employee lastUpdated=&quot;2007-04-21&quot; xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot; xsi:noNamespaceSchemaLocation=&quot;emp_first.xsd&quot;> <Name>Manish Potdar</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>
  • 30. Modified Schema (emp_first.xsd) <?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?> <xsd:schema xmlns:xs=&quot;http://www.w3.org/2001/XMLSchema&quot;> <xsd:element name=&quot;Employee&quot;> <xsd:complexType> <xsd:sequence> <xsd:element name=&quot;Name&quot;/> <xsd:element name=&quot;Contact&quot;> <xsd:complexType> <xsd:sequence> <xsd:element ref=&quot;Address&quot;/> <xsd:element name=&quot;HomePhone&quot;/> <xsd:element name=&quot;Email&quot;/> </xsd:sequence> </xsd:complexType> </xsd:element> </xsd:sequence> <xsd:attribute name=&quot;lastUpdated&quot;/> </xsd:complexType> </xsd:element> <xsd:element name=&quot;Address&quot;> <xsd:complexType> <xsd:sequence> <xsd:element name=&quot;Street&quot;/> <xsd:element name=&quot;City&quot;/> <xsd:element name=&quot;Pin&quot;/> <xsd:element name=&quot;State&quot;/> </xsd:sequence> </xsd:complexType> </xsd:element> </xsd:schema>
  • 31. 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.
  • 32. 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>
  • 33. 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
  • 35. 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
  • 37. 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
  • 38. Declaring Elements Example <xsd:element name = “dateReceived”/> Or, with data type <xsd:element name = “dateReceived” type = “xsd:date”/>
  • 39. 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”/>
  • 40. 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
  • 41. Simple Types Contain information that cannot be split further Example XML <myElement>I am simple</myElement> Schema < xsd:element name =&quot; myElement &quot; type =&quot; xsd:string &quot;/>
  • 42. 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
  • 43. 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” />
  • 44. 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>
  • 45. 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) --> < xsd:schema xmlns:xs =&quot; http://www.w3.org/2001/XMLSchema &quot;> < xsd:element name =&quot; ContactDetails &quot;> < xsd:complexType > < xsd:sequence > < xsd:element ref =&quot; Contact &quot; minOccurs =&quot; 1 &quot; maxOccurs =&quot; unbounded &quot;/> </ xsd:sequence > </ xsd:complexType > </ xsd:element > < xsd:element name =&quot; Contact &quot;> < xsd:complexType > < xsd:sequence > < 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:element > < xsd:element name =&quot; billingAddress &quot; type =&quot; Address &quot;/> < xsd:element name =&quot; mailingAddress &quot; type =&quot; Address &quot;/> </ xsd:sequence > </ xsd:complexType > </ xsd:element > < xsd:complexType name =&quot; Address &quot;> < xsd:sequence > < xsd:element name =&quot; Street1 &quot;/> < xsd:element name =&quot; Street1 &quot;/> < xsd:element name =&quot; City &quot;/> < xsd:element name =&quot; Pin &quot;/> < xsd:element name =&quot; State &quot;/> </ xsd:sequence > </ xsd:complexType > </ xsd:schema >
  • 46. 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) --> < xsd:schema xmlns:xs =&quot; http://www.w3.org/2001/XMLSchema &quot;> < xsd:element name =&quot; ContactDetails &quot;> < xsd:complexType > < xsd:sequence > < xsd:element ref =&quot; Contact &quot; minOccurs =&quot; 1 &quot; maxOccurs =&quot; unbounded &quot;/> </ xsd:sequence > </ xsd:complexType > </ xsd:element > < xsd:element name =&quot; Contact &quot;> < xsd:complexType > < xsd:sequence > < 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:element > < xsd:element name =&quot; billingAddress &quot; type =&quot; Address &quot;/> < xsd:element name =&quot; mailingAddress &quot; type =&quot; Address &quot;/> < xsd:element name =&quot; CreditAddressReference &quot;> < xsd:complexType > < xsd:complexContent > < xsd:extension base =&quot; Address &quot;> < xsd:attribute name =&quot; residentFrom &quot; type =&quot; xsd:date &quot;/> < xsd:attribute name =&quot; residentTo &quot; type =&quot; xsd:date &quot;/> </ xsd:extension > </ xsd:complexContent > </ xsd:complexType > </ xsd:element > </ xsd:sequence > </ xsd:complexType > </ xsd:element > < xsd:complexType name =&quot; Address &quot;> < xsd:sequence > < xsd:element name =&quot; Street1 &quot;/> < xsd:element name =&quot; Street1 &quot;/> < xsd:element name =&quot; City &quot;/> < xsd:element name =&quot; Pin &quot;/> < xsd:element name =&quot; State &quot;/> </ xsd:sequence > </ xsd:complexType > </ xsd:schema >
  • 47. Declaring Attributes <xsd:element name = “invoice”> <xsd:complexType> <xsd:attribute name = “paid” use = “optional” default = “true”> </xsd:complexType> </xsd:element>
  • 49. Secondary Components Can be classified into Model group definitions Attribute groups Notation declarations Identity constraints
  • 50. 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 )
  • 51. group example <?xml version=&quot;1.0&quot;?> <xsd:schema xmlns:xs=&quot;http://www.w3.org/2001/XMLSchema&quot;> <xsd:group name=&quot;custGroup&quot;> <xsd:sequence> <xsd:element name=&quot;customer&quot; type=&quot;xsd:string&quot;/> <xsd:element name=&quot;orderdetails&quot; type=&quot;xsd:string&quot;/> <xsd:element name=&quot;billto&quot; type=&quot;xsd:string&quot;/> <xsd:element name=&quot;shipto&quot; type=&quot;xsd:string&quot;/> </xsd:sequence> </xsd:group> <xsd:element name=&quot;order&quot; type=&quot;ordertype&quot;/> <xsd:complexType name=&quot;ordertype&quot;> <xsd:group ref=&quot;custGroup&quot;/> <xsd:attribute name=&quot;status&quot; type=&quot;xsd:string&quot;/> </xsd:complexType> </xsd:schema>
  • 52. 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) --> < xsd:schema xmlns:xs =&quot; http://www.w3.org/2001/XMLSchema &quot;> < xsd:element name =&quot; FlightDetails &quot;> < xsd:complexType > < xsd:sequence > < xsd:element name =&quot; Name &quot;/> < xsd:group ref =&quot; MealOptions &quot;/> </ xsd:sequence > </ xsd:complexType > </ xsd:element > < xsd:group name =&quot; MealOptions &quot;> < xsd:choice > < xsd:element name =&quot; Vegetarian &quot;/> < xsd:element name =&quot; Non-Vegetarian &quot;/> < xsd:element name =&quot; Salad &quot;/> </ xsd:choice > </ xsd:group > </ xsd:schema >
  • 53. Choice Example – Using Unnamed Syntax <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>
  • 54. 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) --> < xsd:schema xmlns:xs =&quot; http://www.w3.org/2001/XMLSchema &quot;> < xsd:group name =&quot; person &quot;> < xsd:all > < xsd:element name =&quot; firstname &quot; type =&quot; xsd:string &quot;/> < xsd:element name =&quot; lastname &quot; type =&quot; xsd:string &quot;/> </ xsd:all > </ xsd:group > < xsd:element name =&quot; test &quot;> < xsd:complexType > < xsd:group ref =&quot; person &quot; /> </ xsd:complexType > </ xsd:element > </ xsd:schema >
  • 55. All Example – Using Unnamed Syntax <xsd:element name=&quot;person&quot;> <xsd:complexType> <xsd:all> <xsd:element name=&quot;firstname&quot; type=&quot;xsd:string&quot;/> <xsd:element name=&quot;lastname&quot; type=&quot;xsd:string&quot;/> </xsd:all> </xsd:complexType> </xsd:element> firstname and lastname are mandatory sub-elements – must occur exactly once
  • 56. All – Another Example <xsd:group name = “CreditCardDetails”> <xsd:all> <xsd:element name = “CardType” /> <xsd:element name = “CardHolder” /> <xsd:element name = “CardNumber” /> <xsd:element name = “CardExpiry” /> </xsd:all> </xsd:group>
  • 57. 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) --> < xsd:schema xmlns:xs =&quot; http://www.w3.org/2001/XMLSchema &quot;> < xsd:group name =&quot; person &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:group > < xsd:element name =&quot; test &quot;> < xsd:complexType > < xsd:group ref =&quot; person &quot; /> </ xsd:complexType > </ xsd:element > </ xsd:schema >
  • 58. Sequence Example Using Unnamed Syntax Another element that we can use inside a group is sequence Example <xsd:group name=&quot;person&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>
  • 59. Other Details We can use minOccurs, maxOccurs attributes on group, choice, sequence, and all elements.
  • 60. 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
  • 61. 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) --> < xsd:schema xmlns:xs =&quot; http://www.w3.org/2001/XMLSchema &quot;> < xsd:element name =&quot; product &quot;> < xsd:complexType > < xsd:attributeGroup ref =&quot; productDetails &quot;/> </ xsd:complexType > </ xsd:element > < xsd:attributeGroup name =&quot; productDetails &quot;> < xsd:attribute name =&quot; productID &quot; use =&quot; required &quot; type =&quot; xsd:string &quot;/> < xsd:attribute name =&quot; productName &quot; use =&quot; required &quot; type =&quot; xsd:string &quot;/> < xsd:attribute name =&quot; productDescription &quot; use =&quot; required &quot; type =&quot; xsd:string &quot;/> < xsd:attribute name =&quot; unit &quot; use =&quot; required &quot; type =&quot; xsd:positiveInteger &quot;/> < xsd:attribute name =&quot; price &quot; use =&quot; required &quot; type =&quot; xsd:decimal &quot;/> < xsd:attribute name =&quot; stock &quot; use =&quot; required &quot; type =&quot; xsd:integer &quot;/> </ xsd:attributeGroup > </ xsd:schema >
  • 62. 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) --> < xsd:schema xmlns:xs =&quot; http://www.w3.org/2001/XMLSchema &quot;> < xsd:element name =&quot; product &quot; default =&quot; book &quot; /> </ xsd:schema >
  • 63. 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:\Users\Atul\Desktop\test.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:\Users\Atul\Desktop\test.xsd &quot;> </ product >
  • 64. 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) --> < xsd:schema xmlns:xs =&quot; http://www.w3.org/2001/XMLSchema &quot;> < xsd:element name =&quot; product &quot; fixed =&quot; book &quot; /> </ xsd:schema > Now, the value of the element in the XML document must either be book , or it should be empty
  • 65. 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
  • 66. 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) --> < xsd:schema xmlns:xs =&quot; http://www.w3.org/2001/XMLSchema &quot;> < xsd:element name =&quot; product &quot;> < xsd:complexType > < xsd:sequence > < xsd:element name =&quot; productID &quot;/> < xsd:element name =&quot; productName &quot; nillable =&quot; true &quot;/> </ xsd:sequence > </ xsd:complexType > </ xsd:element > </ xsd:schema >
  • 67. 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:\Users\Atul\Desktop\test.xsd &quot;> < productID > test </ productID > < productName xsi:nil =&quot; true &quot;/> </ product >
  • 68. 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
  • 69. 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
  • 71. Schema Facets (Restrictions) - 1 Restriction on values <xsd:element name=&quot;age&quot;> <xsd:simpleType> <xsd:restriction base=&quot;xsd:integer&quot;> <xsd:minInclusive value=&quot;0&quot;/> <xsd:maxInclusive value=&quot;100&quot;/> </xsd:restriction> </xsd:simpleType> </xsd:element>
  • 72. Schema Facets (Restrictions) - 2 Restriction on a set of values <xsd:element name=&quot;car“ / > <xsd:simpleType> <xsd:restriction base=&quot;xsd:string&quot;> <xsd:enumeration value=“Santro/> <xsd:enumeration value=“Maruti&quot;/> <xsd:enumeration value=“Indica/> </xsd:restriction> </xsd:simpleType> </xsd:element>
  • 73. Schema Facets (Restrictions) - 3 Restrictions on a series of values <xsd:element name=&quot;letter&quot;> <xsd:simpleType> <xsd:restriction base=&quot;xsd:string&quot;> <xsd:pattern value=&quot;[a-z]&quot;/> </xsd:restriction> </xsd:simpleType> </xsd:element> Allows one lowercase alphabet <xsd:pattern value=&quot;[A-Z][A-Z][A-Z]&quot;/> Allows three uppercase alphabets <xsd:pattern value=&quot;[a-zA-Z][a-zA-Z][a-zA-Z]&quot;/> Allows three uppercase or lowercase alphabets <xsd:pattern value=&quot;[xyz]&quot;/> Allows one of the lowercase alphabets x, y, or z <xsd:pattern value=&quot;[0-9][0-9][0-9][0-9][0-9]&quot;/> Allows five digits in sequence
  • 74. Schema Facets (Restrictions) - 4 Other restrictions on a series of values <xsd:element name=&quot;letter&quot;> <xsd:simpleType> <xsd:restriction base=&quot;xsd:string&quot;> <xsd:pattern value=&quot;([a-z])*&quot;/> </xsd:restriction> </xsd:simpleType> </xsd:element> Allows 0 or more lowercase alphabets <xsd:pattern value=&quot;([a-z][A-Z])+&quot;/> One or more lowercase alphabets followed by an uppercase alphabet <xsd:pattern value=“worker|manager&quot;/> Allow either worker or manager <xsd:pattern value=&quot;[a-zA-Z0-9]{8}&quot;/> Allow exactly 8 characters, either alphabets or numbers <xsd:length value=&quot;8&quot;/> Allow exactly 8 characters <xsd:minLength value=&quot;5&quot;/> <xsd:maxLength value=&quot;8&quot;/> Allow a minimum of 5 and maximum of 8 characters
  • 76. 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)
  • 77. Start with the Schema Template <?xml version=&quot;1.0&quot;?> <xsd:schema xmlns:xs=&quot;http://www.w3.org/2001/XMLSchema&quot;> ... ... </xsd:schema>
  • 78. Create the shiporder Element Outline <xsd:element name=&quot;shiporder&quot;> <xsd:complexType> <xsd:sequence> ... ... </xsd:sequence> ... </xsd:complexType> </xsd:element>
  • 79. Define the orderperson Element <xsd:element name=&quot;orderperson&quot; type=&quot;xsd:string&quot;/>
  • 80. Define the shipto and item Elements <xsd:element name=&quot;shipto&quot;> <xsd:complexType> <xsd:sequence> <xsd:element name=&quot;name&quot; type=&quot;xsd:string&quot;/> <xsd:element name=&quot;address&quot; type=&quot;xsd:string&quot;/> <xsd:element name=&quot;city&quot; type=&quot;xsd:string&quot;/> <xsd:element name=&quot;country&quot; type=&quot;xsd:string&quot;/> </xsd:sequence> </xsd:complexType> </xsd:element> <xsd:element name=&quot;item&quot; maxOccurs=&quot;unbounded&quot;> <xsd:complexType> <xsd:sequence> <xsd:element name=&quot;title&quot; type=&quot;xsd:string&quot;/> <xsd:element name=&quot;note&quot; type=&quot;xsd:string&quot; minOccurs=&quot;0&quot;/> <xsd:element name=&quot;quantity&quot; type=&quot;xsd:positiveInteger&quot;/> <xsd:element name=&quot;price&quot; type=&quot;xsd:decimal&quot;/> </xsd:sequence> </xsd:complexType> </xsd:element>
  • 81. Declare the attribute for the shiporder Element <xsd:attribute name=&quot;orderid&quot; type=&quot;xsd:string&quot; use=&quot;required&quot;/>
  • 82. The Complete Schema <?xml version=&quot;1.0&quot; encoding=&quot;ISO-8859-1&quot; ?> <xsd:schema xmlns:xs=&quot;http://www.w3.org/2001/XMLSchema&quot;> <xsd:element name=&quot;shiporder&quot;> <xsd:complexType> <xsd:sequence> <xsd:element name=&quot;orderperson&quot; type=&quot;xsd:string&quot;/> <xsd:element name=&quot;shipto&quot;> <xsd:complexType> <xsd:sequence> <xsd:element name=&quot;name&quot; type=&quot;xsd:string&quot;/> <xsd:element name=&quot;address&quot; type=&quot;xsd:string&quot;/> <xsd:element name=&quot;city&quot; type=&quot;xsd:string&quot;/> <xsd:element name=&quot;country&quot; type=&quot;xsd:string&quot;/> </xsd:sequence> </xsd:complexType> </xsd:element> <xsd:element name=&quot;item&quot; maxOccurs=&quot;unbounded&quot;> <xsd:complexType> <xsd:sequence> <xsd:element name=&quot;title&quot; type=&quot;xsd:string&quot;/> <xsd:element name=&quot;note&quot; type=&quot;xsd:string&quot; minOccurs=&quot;0&quot;/> <xsd:element name=&quot;quantity&quot; type=&quot;xsd:positiveInteger&quot;/> <xsd:element name=&quot;price&quot; type=&quot;xsd:decimal&quot;/> </xsd:sequence> </xsd:complexType> </xsd:element> </xsd:sequence> <xsd:attribute name=&quot;orderid&quot; type=&quot;xsd:string&quot; use=&quot;required&quot;/> </xsd:complexType> </xsd:element> </xsd:schema>
  • 83. Making it more readable <?xml version=&quot;1.0&quot; encoding=&quot;ISO-8859-1&quot; ?> <xsd:schema xmlns:xs=&quot;http://www.w3.org/2001/XMLSchema&quot;> <!-- definition of simple elements --> <xsd:element name=&quot;orderperson&quot; type=&quot;xsd:string&quot;/> <xsd:element name=&quot;name&quot; type=&quot;xsd:string&quot;/> <xsd:element name=&quot;address&quot; type=&quot;xsd:string&quot;/> <xsd:element name=&quot;city&quot; type=&quot;xsd:string&quot;/> <xsd:element name=&quot;country&quot; type=&quot;xsd:string&quot;/> <xsd:element name=&quot;title&quot; type=&quot;xsd:string&quot;/> <xsd:element name=&quot;note&quot; type=&quot;xsd:string&quot;/> <xsd:element name=&quot;quantity&quot; type=&quot;xsd:positiveInteger&quot;/> <xsd:element name=&quot;price&quot; type=&quot;xsd:decimal&quot;/> <!-- definition of attributes --> <xsd:attribute name=&quot;orderid&quot; type=&quot;xsd:string&quot;/> <!-- definition of complex elements --> <xsd:element name=&quot;shipto&quot;> <xsd:complexType> <xsd:sequence> <xsd:element ref=&quot;name&quot;/> <xsd:element ref=&quot;address&quot;/> <xsd:element ref=&quot;city&quot;/> <xsd:element ref=&quot;country&quot;/> </xsd:sequence> </xsd:complexType> </xsd:element> <xsd:element name=&quot;item&quot;> <xsd:complexType> <xsd:sequence> <xsd:element ref=&quot;title&quot;/> <xsd:element ref=&quot;note&quot; minOccurs=&quot;0&quot;/> <xsd:element ref=&quot;quantity&quot;/> <xsd:element ref=&quot;price&quot;/> </xsd:sequence> </xsd:complexType> </xsd:element> <xsd:element name=&quot;shiporder&quot;> <xsd:complexType> <xsd:sequence> <xsd:element ref=&quot;orderperson&quot;/> <xsd:element ref=&quot;shipto&quot;/> <xsd:element ref=&quot;item&quot; maxOccurs=&quot;unbounded&quot;/> </xsd:sequence> <xsd:attribute ref=&quot;orderid&quot; use=&quot;required&quot;/> </xsd:complexType> </xsd:element> </xsd:schema>
  • 84. Adding more features <?xml version=&quot;1.0&quot; encoding=&quot;ISO-8859-1&quot; ?> <xsd:schema xmlns:xs=&quot;http://www.w3.org/2001/XMLSchema&quot;> <xsd:simpleType name=&quot;stringtype&quot;> <xsd:restriction base=&quot;xsd:string&quot;/> </xsd:simpleType> <xsd:simpleType name=&quot;inttype&quot;> <xsd:restriction base=&quot;xsd:positiveInteger&quot;/> </xsd:simpleType> <xsd:simpleType name=&quot;dectype&quot;> <xsd:restriction base=&quot;xsd:decimal&quot;/> </xsd:simpleType> <xsd:simpleType name=&quot;orderidtype&quot;> <xsd:restriction base=&quot;xsd:string&quot;> <xsd:pattern value=&quot;[0-9]{6}&quot;/> </xsd:restriction> </xsd:simpleType> <xsd:complexType name=&quot;shiptotype&quot;> <xsd:sequence> <xsd:element name=&quot;name&quot; type=&quot;stringtype&quot;/> <xsd:element name=&quot;address&quot; type=&quot;stringtype&quot;/> <xsd:element name=&quot;city&quot; type=&quot;stringtype&quot;/> <xsd:element name=&quot;country&quot; type=&quot;stringtype&quot;/> </xsd:sequence> </xsd:complexType> <xsd:complexType name=&quot;itemtype&quot;> <xsd:sequence> <xsd:element name=&quot;title&quot; type=&quot;stringtype&quot;/> <xsd:element name=&quot;note&quot; type=&quot;stringtype&quot; minOccurs=&quot;0&quot;/> <xsd:element name=&quot;quantity&quot; type=&quot;inttype&quot;/> <xsd:element name=&quot;price&quot; type=&quot;dectype&quot;/> </xsd:sequence> </xsd:complexType> <xsd:complexType name=&quot;shipordertype&quot;> <xsd:sequence> <xsd:element name=&quot;orderperson&quot; type=&quot;stringtype&quot;/> <xsd:element name=&quot;shipto&quot; type=&quot;shiptotype&quot;/> <xsd:element name=&quot;item&quot; maxOccurs=&quot;unbounded&quot; type=&quot;itemtype&quot;/> </xsd:sequence> <xsd:attribute name=&quot;orderid&quot; type=&quot;orderidtype&quot; use=&quot;required&quot;/> </xsd:complexType> <xsd:element name=&quot;shiporder&quot; type=&quot;shipordertype&quot;/> </xsd:schema>
  • 85. 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:\Users\Atul\Desktop\test.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 >
  • 86. Solution – 1 <?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?> <!-- W3C Schema generated by XMLSpy v2007 sp2 (http://www.altova.com) --> < xsd:schema xmlns:xs =&quot; http://www.w3.org/2001/XMLSchema &quot;> < xsd:element name =&quot; Order &quot;> < xsd:complexType > < xsd:sequence > < xsd:element name =&quot; Products &quot; type =&quot; productsType &quot;/> < xsd:element name =&quot; Contact &quot; type =&quot; contactType &quot;/> < xsd:element name =&quot; Payment &quot; type =&quot; paymentType &quot;/> </ xsd:sequence > </ xsd:complexType > </ xsd:element > < xsd:complexType name =&quot; productsType &quot;> < xsd:sequence > < xsd:element name =&quot; Product &quot; minOccurs =&quot; 1 &quot; maxOccurs =&quot; unbounded &quot; type =&quot; productType &quot;/> </ xsd:sequence > </ xsd:complexType > < xsd:complexType name =&quot; productType &quot;> < xsd:sequence > < xsd:element name =&quot; Product_ID &quot; type =&quot; xsd:string &quot;/> < xsd:element name =&quot; Product_Name &quot; type =&quot; xsd:string &quot;/> < xsd:element name =&quot; Product_Units &quot; type =&quot; xsd:positiveInteger &quot;/> < xsd:element name =&quot; Product_Price &quot; type =&quot; xsd:int &quot;/> </ xsd:sequence > </ xsd:complexType > < xsd:complexType name =&quot; contactType &quot;> < xsd:sequence > < xsd:element name =&quot; Name &quot; type =&quot; xsd:string &quot;/> < xsd:element name =&quot; Address &quot; type =&quot; xsd:string &quot;/> < xsd:element name =&quot; City &quot; type =&quot; xsd:string &quot;/> < xsd:element name =&quot; Pin &quot; type =&quot; xsd:int &quot;/> </ xsd:sequence > </ xsd:complexType > < xsd:complexType name =&quot; paymentType &quot;> < xsd:sequence > < xsd:choice > < xsd:element name =&quot; CreditCard &quot; type =&quot; creditCardType &quot;/> < xsd:element name =&quot; Cheque &quot; type =&quot; chequeType &quot;/> </ xsd:choice > < xsd:element name =&quot; Amount &quot; type =&quot; xsd:positiveInteger &quot;/> </ xsd:sequence > </ xsd:complexType > < xsd:complexType name =&quot; creditCardType &quot;> < xsd:sequence > < xsd:element name =&quot; CardType &quot; type =&quot; cardTypeRestriction &quot;/> < xsd:element name =&quot; CardNumber &quot; type =&quot; xsd:int &quot;/> < xsd:element name =&quot; ExpiryDate &quot; type =&quot; xsd:date &quot;/> </ xsd:sequence > </ xsd:complexType > < xsd:simpleType name =&quot; cardTypeRestriction &quot;> < xsd:restriction base =&quot; xsd:string &quot;> < xsd:enumeration value =&quot; VISA &quot;/> < xsd:enumeration value =&quot; MASTER &quot;/> < xsd:enumeration value =&quot; AMEX &quot;/> </ xsd:restriction > </ xsd:simpleType > < xsd:complexType name =&quot; chequeType &quot;> < xsd:sequence > < xsd:element name =&quot; ChequeNumber &quot; type =&quot; xsd:positiveInteger &quot;/> < xsd:element name =&quot; IssuingBank &quot; type =&quot; xsd:string &quot;/> < xsd:element name =&quot; IssuingBranch &quot; type =&quot; xsd:string &quot;/> < xsd:element name =&quot; Dated &quot; type =&quot; xsd:date &quot;/> </ xsd:sequence > </ xsd:complexType > </ xsd:schema >
  • 87. 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)
  • 89. 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>
  • 90. 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 ”>
  • 91. 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
  • 92. 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
  • 93. 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>
  • 94. 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>
  • 95. 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.
  • 96. 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>
  • 97. Thank you! Any Questions?