SlideShare a Scribd company logo
1 of 54
XML Schema
Document Model
• Features of XML –
– create our own markup language
– defining elements and attributes that best fit the
information you want to encapsulate
• What’s still missing is
– a way to define the language in a formal way
– to restrict the vocabulary of elements and attributes to a
manageable set
– to control the grammar of elements
• The process of formally defining a language in XML is
called document modeling
• Two ways to model a document
– DTD (describe a document's structure with declarative
rules)
– XML Schema
DTD Overview
• DTD's syntax inherited from SGML
• DTDs are not XML document
• DTDS can not be parsed
• DTDS can not be manipulated (e.g., searched,
transformed into different representation)
• DTD describes the structure
• It has non-extensible content model
• Only content type is PCDATA
• Attributes have also non-extensible types
• Absence of user defined types
Contd..
• <quantity> 5 </quantity> and
<quantity> HELLO </quantity> are valid
• One will like to restrict quantity to be numeric only and will
expect the parser to detect the type violation
• With XML Schema, element quantity’s data can indeed be
described as numeric.
• When the preceding markup examples are validated
against an XML Schema that specifies element quantity’s
data must be numeric, 5 conforms and hello fails.
• An XML document that conforms to a schema document is
schema valid and a document that does not conform is
invalid.
• DTD and Schema both coexist
XML Schema
• Schema is an alternative modeling language
• Schema technology is still evolving
• Major schema models: XDR and XSD
• The XML schema defines
– the shape or structure of the XML document,
– rules for data content
– semantics such as
• what fields an element can contain,
• which sub elements it can contain and
• how many items can be present.
– the type and values that can be placed in each element or
attribute.
– XML data constraints (facets) includes rules such as min
and max length.
Some Observations
• Schema document uses XML syntax
• Schema's are XML documents
• Schema documents conform to DTDs
• Schemas are valid documents
• Schema processor provides additional information
to application
DTD vs XSD
• DTD has a simple syntax for content definition
• DTD has limitations when using XML for a variety
of complex purposes
• W3C recommended "XML Schema" as a schema
definition language to replace DTD.
• XML schema, commonly known as an XML
Schema Definition (XSD), describes what a given
XML document can contain.
Contd..
• Example XML :
<employees>
<employee id=”101”>
<name> Tom </name>
<department> CSA </department>
<salary> 35000 </salary>
<email> tom.peter@gmail.com</email>
</employee>
…...
</employees>
DTD:
<!ELEMENT employees (Employee)*>
<!ELEMENT employee (name, department, salary, email)>
<!ELEMENT name (#PCDATA)>
<!ELEMENT department (#PCDATA)>
<!ELEMENT salary (#PCDATA)>
<!ELEMENT email (#PCDATA)>
<!ATTLIST employee id CDATA #REQUIRED>
Example: Census
<census date="1999-04-29">
<censustaker>738</censustaker>
<address>
<number>510</number>
<street>Yellowbrick Road</street>
<city>Munchkinville</city>
<province>Negbo</province>
</address>
<occupants>
<occupant status="adult">
<firstname>Floyd</firstname>
<surname>Fleegle</surname>
<age>61</age>
</occupant>
<occupant>
<firstname>Phylis</firstname>
<surname>Fleegle</surname>
<age>52</age>
</occupant>
<occupant>
<firstname>Filbert</firstname>
<surname>Fleegle</surname>
<age>22</age>
<occupant>
</occupants>
</census>
Schema
<xsd:schema xmlns:xsd="http://www.w3.org/1999/XMLSchema">
<xsd:annotation>
<xsd:documentation>Census form for the Republic of Oz Department of Paperwork,
Emerald City</xsd:documentation>
</xsd:annotation>
<xsd:element name="census" type="CensusType"/>
<xsd:complexType name="CensusType">
<xsd:element name="censustaker" type="xsd:decimal" minoccurs="0"/>
<xsd:element name="address" type="Address"/>
<xsd:element name="occupants" type="Occupants"/>
<xsd:attribute name="date" type="xsd:date"/>
</xsd:complexType>
<xsd:complexType name="Address">
<xsd:element name="number" type="xsd:decimal"/>
<xsd:element name="street" type="xsd:string"/>
<xsd:element name="city" type="xsd:string"/>
<xsd:element name="province" type="xsd:string"/>
<xsd:attribute name="postalcode" type="PCode"/>
</xsd:complexType>
<xsd:simpleType name="PCode" base="xsd:string">
<xsd:pattern value="[A-Z]-d{3}"/>
</xsd:simpleType>
<xsd:complexType name="Occupants">
<xsd:element name="occupant" minOccurs="1" maxOccurs="50">
<xsd:complexType>
<xsd:element name="firstname" type="xsd:string"/>
<xsd:element name="surname" type="xsd:string"/>
<xsd:element name="age">
<xsd:simpleType base="xsd:positive-integer">
<xsd:maxExclusive value="200"/>
</xsd:simpleType>
</xsd:element>
</xsd:complexType>
</xsd:element>
</xsd:complexType>
</xsd:schema>
Pros and Cons in DTD
• Disadvantages in DTD
– Not written in XML
– Lacks strong typing capabilities
– Cannot validate the content to data types
• These disadvantage are made advantage in
XSD.
XSD
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" >
<xs:element name="employees">
<xs:complexType>
<xs:sequence>
<xs:element name="employee" minOccurs="1" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="name" type="xs:string"></xs:element>
<xs:element name="department" type="xs:string"></xs:element>
<xs:element name="salary" type="xs:decimal"></xs:element>
<xs:element name="email" type="xs:string"></xs:element>
</xs:sequence>
<xs:attribute name="id" type="xs:positiveInteger"> </xs:attribute>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
XSD
• The XSD structure starts with the root element
named “schema”
<xs:schema></xs:schema>
• The schema declaration looks like :
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.example.org/employee"
elementFormDefault="qualified">
...
..
</xs:schema>
Attributes of XSD
• xmlns:xs=http://www.w3.org/2001/XMLSchema
– the elements used and the data types used in the schema
are used from the namespace
– the prefix for these elements and the datatypes will be "xs"
• targetNamespace=" http://www.example.org/employee“
– the elements defined by this schema come from
"http://www.example.org/employee "
• elementFormDefault="qualified"
– any elements used by the XML document which were
declared in this schema must be namespace qualified
Element Declaration
• The elements of the xml document are defined
with the schema element declaration.
• The elements can be either simple or complex.
• Simple element
– contains only text
– cannot contain any other element or attribute.
– eg: <name> Tom </name>
• Complex element
– contains other elements in it.
– the elements can have attributes also
Contd..
Element
Simple
Type
Complex
Type
Empty
Simple
Content
Complex
Content
Sequence All Choice
Simple Type
• Syntax –
– <xs:element name="xxx" type="yyy"/>
– ‘xxx’ is the name of the element and ‘yyy’ is the
data type of the element.
Data Types
• There are many data types in XSD. Data types
are classified into
– XSD Strings
– XSD Numeric
– XSD Date
XSD Strings
• A String data types contains characters like
alphabets, numbers and special characters,
line feed, carriage returns and tab spaces
Data Types Description
string A string
name A string which contains a valid name
normalizedString A string that does not contain line feeds, carriage
returns, or tabs
XSD Numeric
• These data types contains numbers which may
be a whole number or decimal number.
Data types Description
Integer Contains integer value
Decimal Contains decimal value
positiveInteger Contains integer value which is only positive
XSD Date
• This data type contains date and time values.
• Format of the date is “YYYY-MM-DD”
• All are mandatory
• The format for time is “hh:mm:ss”
Data types Description
Date Defines the date value (YYYY-MM-DD)
Time Defines the time value (hh:mm:ss)
DateTime Defines both data and time (yyyy-mm-ddThh:mm:ss)
Simple Type
• Example:
<name> Johan </name>
<age> 28 </age>
<dob> 1985-07-27 </dob>
• DTD for the above
<!ELEMENT name (#PCDATA)>
<!ELEMENT age (#PCDATA)>
<!ELEMENT dob (#PCDATA)>
• XSD for the above
<xs:element name="name" type="xs:string"/>
<xs:element name="age" type="xs:integer"/>
<xs:element name="dob" type="xs:date"/>
Simple Type - Default / Fixed Value
• Simple elements might have default or fixed values that can
be specified in the schema definition
• In default, this value will be inserted if no other value is
given else will take the value given in the XML document.
• In fixed, the value given in the schema definition is only
assigned and no other value can be given in the XML
document.
• Example:
<xs:element name="salary" type="xs:integer"
default="20000"/>
<xs:element name="color" type="xs:string"
fixed="yellow"/>
Attribute
• Attribute are properties that define a XML
element
• Attributes are themselves a simple type.
• Simple element cannot have attribute.
• An element with attribute becomes a complex
type
• Attributes also has data types, default and fixed
values
• Example:
<employee id=”101”>Tom </employee>
The Schema definition of the "id" attribute :
<xs:attribute name=”id” type=”xs:integer”/>
Contd..
• Required and Optional in attributes
– By default the attributes are optional
– To make it mandatory add an attribute named “use”.
<xs:attribute name="id" type="xs:integer" use="required"/>
• Restrictions
– Restrictions are conditions that are applied on an
element.
– Restriction makes the element to be defined within a
boundary.
– For example, the age should be within 18 to 58. This
restriction cannot be given when defining the XML
Schema of the “age” element.
Restriction Description
Enumeration Defines a list of values for an element
Length Defines the exact number of characters or list elements that are allowed. The
value of this length must equal to or greater than zero.
maxExclusive Defines the upper limit for numeric values (the value must be less than this
value)
maxInclusive defines the upper limit for numeric values (the value must be greater than or
equal to this value)
maxLength Defines the maximum number of characters or list items that is allowed.
Must be equal to or greater than zero
minExclusive Defines the lower limit for numeric values (the value must be greater than
this value)
minInclusive defines the lower limit for numeric values (the value must be greater than or
equal to this value)
minLength Defines the minimum number of characters or list items allowed. Must be
equal to or greater than zero
Pattern Defines the exact sequence of characters that are acceptable
whiteSpace Defines how white space (line feeds, tabs, spaces, and carriage returns) is
handled
totalDigits Defines the exact number of digits allowed. Must be greater than zero
Simple Type - Example
• Simple element
– restriction for a simple element “age”.
<xs:element name="age">
<xs:simpleType>
<xs:restriction base="xs:integer">
<xs:minInclusive value="18"/>
<xs:maxInclusive value="58"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
Contd..
• Using enumeration
<xs:element name="department">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:enumeration value="CSA"/>
<xs:enumeration value="Sales"/>
<xs:enumeration value="Development"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
Contd..
• Using range of data
<xs:element name="status">
<xs:simpleType>
<xs:restriction base="xs:integer">
<xs:pattern value="[0-9]"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
• The element “status” can accept an integer
which can be between 0 to 9.
Contd..
• Using OR " | “
<xs:element name="flag">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value=”true|false"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
• The element “flag” can have either the value
“true” or “false”
Contd..
• Restriction
<xs:element name="productId">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="[a-z]{2}[0-9]{4}"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
• the element “productId” should have totally 8
characters in which the first 2 are smaller case
alphabets and the remaining 4 are numbers
<productID>cs1234</product> – valid data value
<productID>CS123</product> – invalid data value
Complex Elements
• Complex elements contains other elements and
attributes within them
<employee id=”101”>
<name> Johan </name>
<age> 28 </name>
<salary> 35000 </salary>
</employee>
Complex Element
Empty Elements
Elements that
contain only sub
elements
Elements that
contain only text
Elements that
contains both text
and other
elements
Complex : Empty Element
<employee id=”101”/>
• This element “employee” does not have any element
inside them but do have an attribute named “id”
• This makes the element as a complex element
• The schema for this represented as
<xs:element name="employee">
<xs:complexType>
<xs:attribute name="id" type="xs:positiveInteger"/>
</xs:complexType>
</xs:element>
Complex Elements
• Elements that contain elements
<employee>
<name> Tom </name>
<age> 28 </name>
</employee>
Here complex element contains sub elements within them
• Schema for the above :
<xs:element name="employee">
<xs:complexType>
<xs:sequence>
<xs:element name="name" type="xs:string"/>
<xs:element name="age" type="xs:integer"/>
</xs:sequence>
</xs:complexType>
</xs:element>
Contd..
• Mixed type element
– contains sub elements, attributes and text in it
<xs:element name="MarkedUpDesc">
<xs:complexType mixed="true">
<xs:sequence>
<xs:element name="Bold" type="xs:string" />
<xs:element name="Italic" type="xs:string" />
</xs:sequence>
</xs:complexType>
</xs:element>
<MarkedUpDesc>
This is an <Bold>Example</Bold> of <Italic>Mixed</Italic>
Content. Note there are elements mixed in with the elements
data.
</MarkedUpDesc>
Contd..
Indicators
Order
Indicators
sequence All choice
Occurrence
Indicator
minOccurs maxOccurs
Order Indicators
• Sequence indicator
– ensures that all the sub elements are defined
– can be defined in the same order as given in the XSD
<xs:element name="employee">
<xs:complexType>
<xs:sequence>
<xs:element name="name" type="xs:string"/>
<xs:element name="age" type="xs:integer"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<employee>
<name> Tom </name>
<age> 28 </age>
</employee>
<employee>
<age> 28 </age>
<name> Tom </name>
</employee>
Correct
Incorrect
Contd..
• All indicator
– ensures that all the sub elements are defined
– can be defined in any order
<xs:element name="employee">
<xs:complexType>
<xs:all>
<xs:element name="name" type="xs:string"/>
<xs:element name="age" type="xs:integer"/>
</xs:all>
</xs:complexType>
</xs:element>
<employee>
<age> 28 </age>
<name> Tom </name>
</employee>
<employee>
<name> Tom </name>
<age> 28 </age>
</employee>
Correct Correct
Contd..
• Choice indicator
– defines that either one of the child element must occur
within the element
<xs:element name="employee">
<xs:complexType>
<xs:choice>
<xs:element name="name" type="xs:string"/>
<xs:element name="age" type="xs:integer"/>
</xs:choice>
</xs:complexType>
</xs:element>
<employee>
<age> 28 </age>
</employee>
<employee>
<name> Tom </name>
</employee>
Correct Correct
<employee>
<name> Tom </name>
<age> 28 </age>
</employee>
Incorrect
Occurrence Indicators
• Defines the number of times an element can
occur
<xs:element name="employee">
<xs:complexType>
<xs:sequence>
<xs:element name="name" type="xs:string"/>
<xs:element name=“childname" type="xs:integer“
minOccurs=”0” maxOccurs="5"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<employee>
<name> Tom </name>
</employee>
<employee>
<name> Tom </name>
<childname>A</childname>
</employee>
Correct Correct
Group
• Defines a group of elements
• may contain one or more sequence, choice
and/or all elements
• can occur within complexType, sequence,
choice, and restriction
Examples: group and sequence
<xsd:group name=“personalinfo">
<xsd:sequence>
<xsd:element name=“firstname" type=“xsd:string"/>
<xsd:element name=“lastname" type=“xsd:string"/>
</xsd:sequence>
</xsd:group>
<xsd:complexType name=“person">
<xsd:group ref=“personalinfo"/>
<xsd:attribute name=“citizenship" type=“xsd:string"/>
<!-- other elements -->
</xsd:complexType>
<xsd:sequence minOccurs=“min" maxOccurs=“max">
- - -
</xsd:sequence>
Example:
<?xml version = “1.0"?>
<!-- schema.xsd -->
<!-- Example W3C XML Schema -->
<xsd:schema
xmlns:xsd=http://www.w3.org/2000/10/XMLSchema">
<xsd:element name=“message" type=“xsd:string"/>
<xsd:element name=“greeting" type=“greetingType"/>
<xsd:complexType name=“greetingType“ content=mixed">
<xsd:element ref=“message"/>
</xsd:complexType>
<xsd:element name=“myMessage" type=“myMessageType"/>
<xsd:complexType name=“myMessageType">
<xsd:element ref=“greeting" minOccurs=“0"
maxOccurs=“1"/>
<xsd:element ref= “message" minOccurs=“1"
maxOccurs=“unbounded"/>
</xsd:complexType>
</xsd:schema>
Associating XML with XSD
• Define an XSD to create an XML file which
contains employee’s information like name,
department, salary and email.
• There can be many employee details present
in the XML file)
Employee.xsd
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" >
<xs:element name="employees">
<xs:complexType>
<xs:sequence>
<xs:element name="employee" minOccurs="1"
maxOccurs="unbounded“>
<xs:complexType>
<xs:sequence>
<xs:element name="name" type="xs:string"></xs:element>
<xs:element name="dept" type="xs:string"></xs:element>
<xs:element name="salary" type="xs:float"></xs:element>
<xs:element name="email" type="xs:string"></xs:element>
</xs:sequence>
<xs:attribute name="id" type="xs:positiveInteger"></xs:attribute>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
Employee.xml
<?xml version="1.0"?>
<employees xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="employee.xsd">
<employee id="101">
<name> Tom </name>
<department> CSA </department>
<salary> 35000 </salary>
<email> tom.peter@gmail.com</email>
</employee>
<employee id="102">
<name>Sam</name>
<department>AC</department>
<salary>45000</salary>
<email>sam.johan@gmail.com</email>
</employee>
</employees>
Dividing the XML Schema
• The previous XML Schema is very simple
• But it becomes very difficult to read it, and
maintain the XML document.
• Avoid this by dividing the XML Schema as
– define the elements and attributes first and then
– make use of them using the “ref” keyword.
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<!-- defining simple elements -->
<xs:element name="name" type="xs:string"/>
<xs:element name="department" type="xs:string"/>
<xs:element name="salary" type="xs:float"/>
<xs:element name="email" type="xs:string"/>
<!-- defining attributes -->
<xs:attribute name="id" type="xs:positiveInteger"/>
<!-- defining Complex element -->
<xs:element name="employee">
<xs:complexType>
<xs:sequence>
<xs:element ref="name"/>
<xs:element ref="department" />
<xs:element ref="salary" />
<xs:element ref="email" />
</xs:sequence>
Contd..
<xs:attribute ref="id"></xs:attribute>
</xs:complexType>
</xs:element>
<xs:element name="employees">
<xs:complexType>
<xs:sequence>
<xs:element ref="employee" minOccurs="1"
maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
<xsd:complexType name=“fullname">
<xsd:element name=“firstname" type=“xsd:string"/>
<xsd:element name=“lastname" type=“xsd:string"/>
</xsd:complexType>
Alternative:
<xsd:element name=“firstname" type=“xsd:string"/>
<xsd:element name=“lastname" type=“xsd:string"/>
<xsd:complexType name=“fullname">
<xsd:element ref=“firstname"/>
<xsd:element ref=“lastname" />
</xsd:complexType>
Using Named Types
• defines types, that enables you to reuse element
definitions
• done by giving names to the simpleTypes and
complexTypes elements
• make them point through the type attribute of
the element.
Contd..
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" >
<xs:simpleType name="stringtype">
<xs:restriction base="xs:string"/>
</xs:simpleType>
<xs:simpleType name="floattype">
<xs:restriction base="xs:float"></xs:restriction>
</xs:simpleType>
<xs:simpleType name="idtype">
<xs:restriction base="xs:positiveInteger">
<xs:pattern value="[0-9]{3}"></xs:pattern>
</xs:restriction>
</xs:simpleType>
<xs:simpleType name="emailtype">
<xs:restriction base="xs:string">
<xs:maxLength value="30"/>
</xs:restriction>
</xs:simpleType>
<xs:complexType name="employeetype">
<xs:sequence>
<xs:element name="name" type="stringtype"/>
<xs:element name="department" type="stringtype"/>
<xs:element name="salary" type="floattype"/>
<xs:element name="email" type="emailtype"/>
</xs:sequence>
<xs:attribute name="id" type="idtype"/>
</xs:complexType>
<xs:complexType name="employeestype">
<xs:sequence>
<xs:element name="employee" type="employeetype"
maxOccurs="unbounded" minOccurs="1"/>
</xs:sequence>
</xs:complexType>
<xs:element name="employees" type="employeestype"/>
</xs:schema>

More Related Content

What's hot (20)

An Overview of HTML, CSS & Java Script
An Overview of HTML, CSS & Java ScriptAn Overview of HTML, CSS & Java Script
An Overview of HTML, CSS & Java Script
 
cascading style sheet ppt
cascading style sheet pptcascading style sheet ppt
cascading style sheet ppt
 
Functions in javascript
Functions in javascriptFunctions in javascript
Functions in javascript
 
HTML, CSS and Java Scripts Basics
HTML, CSS and Java Scripts BasicsHTML, CSS and Java Scripts Basics
HTML, CSS and Java Scripts Basics
 
Intro to HTML and CSS basics
Intro to HTML and CSS basicsIntro to HTML and CSS basics
Intro to HTML and CSS basics
 
jQuery for beginners
jQuery for beginnersjQuery for beginners
jQuery for beginners
 
css.ppt
css.pptcss.ppt
css.ppt
 
Hushang Gaikwad
Hushang GaikwadHushang Gaikwad
Hushang Gaikwad
 
Introduction to JavaScript Basics.
Introduction to JavaScript Basics.Introduction to JavaScript Basics.
Introduction to JavaScript Basics.
 
Javascript
JavascriptJavascript
Javascript
 
HTML Forms
HTML FormsHTML Forms
HTML Forms
 
HTML presentation for beginners
HTML presentation for beginnersHTML presentation for beginners
HTML presentation for beginners
 
Extensible Markup Language (XML)
Extensible Markup Language (XML)Extensible Markup Language (XML)
Extensible Markup Language (XML)
 
CSS
CSSCSS
CSS
 
Java Script ppt
Java Script pptJava Script ppt
Java Script ppt
 
Javascript basics
Javascript basicsJavascript basics
Javascript basics
 
HTML/HTML5
HTML/HTML5HTML/HTML5
HTML/HTML5
 
(Fast) Introduction to HTML & CSS
(Fast) Introduction to HTML & CSS (Fast) Introduction to HTML & CSS
(Fast) Introduction to HTML & CSS
 
CSS
CSSCSS
CSS
 
XSLT
XSLTXSLT
XSLT
 

Viewers also liked (20)

XML Schema (W3C)
XML Schema (W3C)XML Schema (W3C)
XML Schema (W3C)
 
XML, DTD & XSD Overview
XML, DTD & XSD OverviewXML, DTD & XSD Overview
XML, DTD & XSD Overview
 
4 xml namespaces and xml schema
4   xml namespaces and xml schema4   xml namespaces and xml schema
4 xml namespaces and xml schema
 
XML Schema (2002)
XML Schema (2002)XML Schema (2002)
XML Schema (2002)
 
Xsd examples
Xsd examplesXsd examples
Xsd examples
 
3 xml namespaces and xml schema
3   xml namespaces and xml schema3   xml namespaces and xml schema
3 xml namespaces and xml schema
 
XML's validation - XML Schema
XML's validation - XML SchemaXML's validation - XML Schema
XML's validation - XML Schema
 
XML and DTD
XML and DTDXML and DTD
XML and DTD
 
XML- Schéma
XML- SchémaXML- Schéma
XML- Schéma
 
Xsd
XsdXsd
Xsd
 
XSD
XSDXSD
XSD
 
Xml Schema
Xml SchemaXml Schema
Xml Schema
 
XML Schemas
XML SchemasXML Schemas
XML Schemas
 
Jaxb
JaxbJaxb
Jaxb
 
Xml material
Xml materialXml material
Xml material
 
Xsd Basics R&D with ORACLE SOA
Xsd Basics R&D with ORACLE SOAXsd Basics R&D with ORACLE SOA
Xsd Basics R&D with ORACLE SOA
 
XML Schema Part 1
XML Schema Part 1XML Schema Part 1
XML Schema Part 1
 
XML Schema Part 2
XML Schema Part 2XML Schema Part 2
XML Schema Part 2
 
Publishing xml
Publishing xmlPublishing xml
Publishing xml
 
Applying xml
Applying xmlApplying xml
Applying xml
 

Similar to XML Schema

Similar to XML Schema (20)

Xsd
XsdXsd
Xsd
 
Xml and DTD's
Xml and DTD'sXml and DTD's
Xml and DTD's
 
Xml and webdata
Xml and webdataXml and webdata
Xml and webdata
 
Xml and webdata
Xml and webdataXml and webdata
Xml and webdata
 
Xml and webdata
Xml and webdataXml and webdata
Xml and webdata
 
Xml and webdata
Xml and webdataXml and webdata
Xml and webdata
 
Xml and webdata
Xml and webdataXml and webdata
Xml and webdata
 
Xml and webdata
Xml and webdataXml and webdata
Xml and webdata
 
Xml and webdata
Xml and webdataXml and webdata
Xml and webdata
 
Web Technologies Unit 2 Print.pdf
Web Technologies Unit 2 Print.pdfWeb Technologies Unit 2 Print.pdf
Web Technologies Unit 2 Print.pdf
 
01 xml document structure
01 xml document structure01 xml document structure
01 xml document structure
 
Xml
XmlXml
Xml
 
Introduction to XML
Introduction to XMLIntroduction to XML
Introduction to XML
 
Xml schema
Xml schemaXml schema
Xml schema
 
XML Schema.pdf
XML Schema.pdfXML Schema.pdf
XML Schema.pdf
 
XML Schema.pptx
XML Schema.pptxXML Schema.pptx
XML Schema.pptx
 
1 xml fundamentals
1 xml fundamentals1 xml fundamentals
1 xml fundamentals
 
Xml schema
Xml schemaXml schema
Xml schema
 
XML SCHEMAS
XML SCHEMASXML SCHEMAS
XML SCHEMAS
 
distributed system concerned lab sessions
distributed system concerned lab sessionsdistributed system concerned lab sessions
distributed system concerned lab sessions
 

More from Kumar

Graphics devices
Graphics devicesGraphics devices
Graphics devicesKumar
 
Fill area algorithms
Fill area algorithmsFill area algorithms
Fill area algorithmsKumar
 
region-filling
region-fillingregion-filling
region-fillingKumar
 
Bresenham derivation
Bresenham derivationBresenham derivation
Bresenham derivationKumar
 
Bresenham circles and polygons derication
Bresenham circles and polygons dericationBresenham circles and polygons derication
Bresenham circles and polygons dericationKumar
 
Introductionto xslt
Introductionto xsltIntroductionto xslt
Introductionto xsltKumar
 
Extracting data from xml
Extracting data from xmlExtracting data from xml
Extracting data from xmlKumar
 
Xml basics
Xml basicsXml basics
Xml basicsKumar
 
Introduction to XML
Introduction to XMLIntroduction to XML
Introduction to XMLKumar
 
How to deploy a j2ee application
How to deploy a j2ee applicationHow to deploy a j2ee application
How to deploy a j2ee applicationKumar
 
JNDI, JMS, JPA, XML
JNDI, JMS, JPA, XMLJNDI, JMS, JPA, XML
JNDI, JMS, JPA, XMLKumar
 
EJB Fundmentals
EJB FundmentalsEJB Fundmentals
EJB FundmentalsKumar
 
JSP and struts programming
JSP and struts programmingJSP and struts programming
JSP and struts programmingKumar
 
java servlet and servlet programming
java servlet and servlet programmingjava servlet and servlet programming
java servlet and servlet programmingKumar
 
Introduction to JDBC and JDBC Drivers
Introduction to JDBC and JDBC DriversIntroduction to JDBC and JDBC Drivers
Introduction to JDBC and JDBC DriversKumar
 
Introduction to J2EE
Introduction to J2EEIntroduction to J2EE
Introduction to J2EEKumar
 
Android tutorial (2)
Android tutorial (2)Android tutorial (2)
Android tutorial (2)Kumar
 
Android structure
Android structureAndroid structure
Android structureKumar
 
Android lifecycle
Android lifecycleAndroid lifecycle
Android lifecycleKumar
 

More from Kumar (20)

Graphics devices
Graphics devicesGraphics devices
Graphics devices
 
Fill area algorithms
Fill area algorithmsFill area algorithms
Fill area algorithms
 
region-filling
region-fillingregion-filling
region-filling
 
Bresenham derivation
Bresenham derivationBresenham derivation
Bresenham derivation
 
Bresenham circles and polygons derication
Bresenham circles and polygons dericationBresenham circles and polygons derication
Bresenham circles and polygons derication
 
Introductionto xslt
Introductionto xsltIntroductionto xslt
Introductionto xslt
 
Extracting data from xml
Extracting data from xmlExtracting data from xml
Extracting data from xml
 
Xml basics
Xml basicsXml basics
Xml basics
 
DTD
DTDDTD
DTD
 
Introduction to XML
Introduction to XMLIntroduction to XML
Introduction to XML
 
How to deploy a j2ee application
How to deploy a j2ee applicationHow to deploy a j2ee application
How to deploy a j2ee application
 
JNDI, JMS, JPA, XML
JNDI, JMS, JPA, XMLJNDI, JMS, JPA, XML
JNDI, JMS, JPA, XML
 
EJB Fundmentals
EJB FundmentalsEJB Fundmentals
EJB Fundmentals
 
JSP and struts programming
JSP and struts programmingJSP and struts programming
JSP and struts programming
 
java servlet and servlet programming
java servlet and servlet programmingjava servlet and servlet programming
java servlet and servlet programming
 
Introduction to JDBC and JDBC Drivers
Introduction to JDBC and JDBC DriversIntroduction to JDBC and JDBC Drivers
Introduction to JDBC and JDBC Drivers
 
Introduction to J2EE
Introduction to J2EEIntroduction to J2EE
Introduction to J2EE
 
Android tutorial (2)
Android tutorial (2)Android tutorial (2)
Android tutorial (2)
 
Android structure
Android structureAndroid structure
Android structure
 
Android lifecycle
Android lifecycleAndroid lifecycle
Android lifecycle
 

Recently uploaded

Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3JemimahLaneBuaron
 
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991RKavithamani
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104misteraugie
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdfQucHHunhnh
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Sapana Sha
 
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxRoyAbrique
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfciinovamais
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...Marc Dusseiller Dusjagr
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfchloefrazer622
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpinRaunakKeshri1
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactPECB
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxmanuelaromero2013
 

Recently uploaded (20)

Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3
 
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdf
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpin
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptx
 

XML Schema

  • 2. Document Model • Features of XML – – create our own markup language – defining elements and attributes that best fit the information you want to encapsulate • What’s still missing is – a way to define the language in a formal way – to restrict the vocabulary of elements and attributes to a manageable set – to control the grammar of elements • The process of formally defining a language in XML is called document modeling • Two ways to model a document – DTD (describe a document's structure with declarative rules) – XML Schema
  • 3. DTD Overview • DTD's syntax inherited from SGML • DTDs are not XML document • DTDS can not be parsed • DTDS can not be manipulated (e.g., searched, transformed into different representation) • DTD describes the structure • It has non-extensible content model • Only content type is PCDATA • Attributes have also non-extensible types • Absence of user defined types
  • 4. Contd.. • <quantity> 5 </quantity> and <quantity> HELLO </quantity> are valid • One will like to restrict quantity to be numeric only and will expect the parser to detect the type violation • With XML Schema, element quantity’s data can indeed be described as numeric. • When the preceding markup examples are validated against an XML Schema that specifies element quantity’s data must be numeric, 5 conforms and hello fails. • An XML document that conforms to a schema document is schema valid and a document that does not conform is invalid. • DTD and Schema both coexist
  • 5. XML Schema • Schema is an alternative modeling language • Schema technology is still evolving • Major schema models: XDR and XSD • The XML schema defines – the shape or structure of the XML document, – rules for data content – semantics such as • what fields an element can contain, • which sub elements it can contain and • how many items can be present. – the type and values that can be placed in each element or attribute. – XML data constraints (facets) includes rules such as min and max length.
  • 6. Some Observations • Schema document uses XML syntax • Schema's are XML documents • Schema documents conform to DTDs • Schemas are valid documents • Schema processor provides additional information to application
  • 7. DTD vs XSD • DTD has a simple syntax for content definition • DTD has limitations when using XML for a variety of complex purposes • W3C recommended "XML Schema" as a schema definition language to replace DTD. • XML schema, commonly known as an XML Schema Definition (XSD), describes what a given XML document can contain.
  • 8. Contd.. • Example XML : <employees> <employee id=”101”> <name> Tom </name> <department> CSA </department> <salary> 35000 </salary> <email> tom.peter@gmail.com</email> </employee> …... </employees> DTD: <!ELEMENT employees (Employee)*> <!ELEMENT employee (name, department, salary, email)> <!ELEMENT name (#PCDATA)> <!ELEMENT department (#PCDATA)> <!ELEMENT salary (#PCDATA)> <!ELEMENT email (#PCDATA)> <!ATTLIST employee id CDATA #REQUIRED>
  • 9. Example: Census <census date="1999-04-29"> <censustaker>738</censustaker> <address> <number>510</number> <street>Yellowbrick Road</street> <city>Munchkinville</city> <province>Negbo</province> </address> <occupants> <occupant status="adult"> <firstname>Floyd</firstname> <surname>Fleegle</surname> <age>61</age> </occupant> <occupant> <firstname>Phylis</firstname> <surname>Fleegle</surname> <age>52</age> </occupant> <occupant> <firstname>Filbert</firstname> <surname>Fleegle</surname> <age>22</age> <occupant> </occupants> </census>
  • 10. Schema <xsd:schema xmlns:xsd="http://www.w3.org/1999/XMLSchema"> <xsd:annotation> <xsd:documentation>Census form for the Republic of Oz Department of Paperwork, Emerald City</xsd:documentation> </xsd:annotation> <xsd:element name="census" type="CensusType"/> <xsd:complexType name="CensusType"> <xsd:element name="censustaker" type="xsd:decimal" minoccurs="0"/> <xsd:element name="address" type="Address"/> <xsd:element name="occupants" type="Occupants"/> <xsd:attribute name="date" type="xsd:date"/> </xsd:complexType> <xsd:complexType name="Address"> <xsd:element name="number" type="xsd:decimal"/> <xsd:element name="street" type="xsd:string"/> <xsd:element name="city" type="xsd:string"/> <xsd:element name="province" type="xsd:string"/> <xsd:attribute name="postalcode" type="PCode"/> </xsd:complexType>
  • 11. <xsd:simpleType name="PCode" base="xsd:string"> <xsd:pattern value="[A-Z]-d{3}"/> </xsd:simpleType> <xsd:complexType name="Occupants"> <xsd:element name="occupant" minOccurs="1" maxOccurs="50"> <xsd:complexType> <xsd:element name="firstname" type="xsd:string"/> <xsd:element name="surname" type="xsd:string"/> <xsd:element name="age"> <xsd:simpleType base="xsd:positive-integer"> <xsd:maxExclusive value="200"/> </xsd:simpleType> </xsd:element> </xsd:complexType> </xsd:element> </xsd:complexType> </xsd:schema>
  • 12. Pros and Cons in DTD • Disadvantages in DTD – Not written in XML – Lacks strong typing capabilities – Cannot validate the content to data types • These disadvantage are made advantage in XSD.
  • 13. XSD <?xml version="1.0"?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" > <xs:element name="employees"> <xs:complexType> <xs:sequence> <xs:element name="employee" minOccurs="1" maxOccurs="unbounded"> <xs:complexType> <xs:sequence> <xs:element name="name" type="xs:string"></xs:element> <xs:element name="department" type="xs:string"></xs:element> <xs:element name="salary" type="xs:decimal"></xs:element> <xs:element name="email" type="xs:string"></xs:element> </xs:sequence> <xs:attribute name="id" type="xs:positiveInteger"> </xs:attribute> </xs:complexType> </xs:element> </xs:sequence> </xs:complexType> </xs:element> </xs:schema>
  • 14. XSD • The XSD structure starts with the root element named “schema” <xs:schema></xs:schema> • The schema declaration looks like : <?xml version="1.0"?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.example.org/employee" elementFormDefault="qualified"> ... .. </xs:schema>
  • 15. Attributes of XSD • xmlns:xs=http://www.w3.org/2001/XMLSchema – the elements used and the data types used in the schema are used from the namespace – the prefix for these elements and the datatypes will be "xs" • targetNamespace=" http://www.example.org/employee“ – the elements defined by this schema come from "http://www.example.org/employee " • elementFormDefault="qualified" – any elements used by the XML document which were declared in this schema must be namespace qualified
  • 16. Element Declaration • The elements of the xml document are defined with the schema element declaration. • The elements can be either simple or complex. • Simple element – contains only text – cannot contain any other element or attribute. – eg: <name> Tom </name> • Complex element – contains other elements in it. – the elements can have attributes also
  • 18. Simple Type • Syntax – – <xs:element name="xxx" type="yyy"/> – ‘xxx’ is the name of the element and ‘yyy’ is the data type of the element.
  • 19. Data Types • There are many data types in XSD. Data types are classified into – XSD Strings – XSD Numeric – XSD Date
  • 20. XSD Strings • A String data types contains characters like alphabets, numbers and special characters, line feed, carriage returns and tab spaces Data Types Description string A string name A string which contains a valid name normalizedString A string that does not contain line feeds, carriage returns, or tabs
  • 21. XSD Numeric • These data types contains numbers which may be a whole number or decimal number. Data types Description Integer Contains integer value Decimal Contains decimal value positiveInteger Contains integer value which is only positive
  • 22. XSD Date • This data type contains date and time values. • Format of the date is “YYYY-MM-DD” • All are mandatory • The format for time is “hh:mm:ss” Data types Description Date Defines the date value (YYYY-MM-DD) Time Defines the time value (hh:mm:ss) DateTime Defines both data and time (yyyy-mm-ddThh:mm:ss)
  • 23. Simple Type • Example: <name> Johan </name> <age> 28 </age> <dob> 1985-07-27 </dob> • DTD for the above <!ELEMENT name (#PCDATA)> <!ELEMENT age (#PCDATA)> <!ELEMENT dob (#PCDATA)> • XSD for the above <xs:element name="name" type="xs:string"/> <xs:element name="age" type="xs:integer"/> <xs:element name="dob" type="xs:date"/>
  • 24. Simple Type - Default / Fixed Value • Simple elements might have default or fixed values that can be specified in the schema definition • In default, this value will be inserted if no other value is given else will take the value given in the XML document. • In fixed, the value given in the schema definition is only assigned and no other value can be given in the XML document. • Example: <xs:element name="salary" type="xs:integer" default="20000"/> <xs:element name="color" type="xs:string" fixed="yellow"/>
  • 25. Attribute • Attribute are properties that define a XML element • Attributes are themselves a simple type. • Simple element cannot have attribute. • An element with attribute becomes a complex type • Attributes also has data types, default and fixed values • Example: <employee id=”101”>Tom </employee> The Schema definition of the "id" attribute : <xs:attribute name=”id” type=”xs:integer”/>
  • 26. Contd.. • Required and Optional in attributes – By default the attributes are optional – To make it mandatory add an attribute named “use”. <xs:attribute name="id" type="xs:integer" use="required"/> • Restrictions – Restrictions are conditions that are applied on an element. – Restriction makes the element to be defined within a boundary. – For example, the age should be within 18 to 58. This restriction cannot be given when defining the XML Schema of the “age” element.
  • 27. Restriction Description Enumeration Defines a list of values for an element Length Defines the exact number of characters or list elements that are allowed. The value of this length must equal to or greater than zero. maxExclusive Defines the upper limit for numeric values (the value must be less than this value) maxInclusive defines the upper limit for numeric values (the value must be greater than or equal to this value) maxLength Defines the maximum number of characters or list items that is allowed. Must be equal to or greater than zero minExclusive Defines the lower limit for numeric values (the value must be greater than this value) minInclusive defines the lower limit for numeric values (the value must be greater than or equal to this value) minLength Defines the minimum number of characters or list items allowed. Must be equal to or greater than zero Pattern Defines the exact sequence of characters that are acceptable whiteSpace Defines how white space (line feeds, tabs, spaces, and carriage returns) is handled totalDigits Defines the exact number of digits allowed. Must be greater than zero
  • 28. Simple Type - Example • Simple element – restriction for a simple element “age”. <xs:element name="age"> <xs:simpleType> <xs:restriction base="xs:integer"> <xs:minInclusive value="18"/> <xs:maxInclusive value="58"/> </xs:restriction> </xs:simpleType> </xs:element>
  • 29. Contd.. • Using enumeration <xs:element name="department"> <xs:simpleType> <xs:restriction base="xs:string"> <xs:enumeration value="CSA"/> <xs:enumeration value="Sales"/> <xs:enumeration value="Development"/> </xs:restriction> </xs:simpleType> </xs:element>
  • 30. Contd.. • Using range of data <xs:element name="status"> <xs:simpleType> <xs:restriction base="xs:integer"> <xs:pattern value="[0-9]"/> </xs:restriction> </xs:simpleType> </xs:element> • The element “status” can accept an integer which can be between 0 to 9.
  • 31. Contd.. • Using OR " | “ <xs:element name="flag"> <xs:simpleType> <xs:restriction base="xs:string"> <xs:pattern value=”true|false"/> </xs:restriction> </xs:simpleType> </xs:element> • The element “flag” can have either the value “true” or “false”
  • 32. Contd.. • Restriction <xs:element name="productId"> <xs:simpleType> <xs:restriction base="xs:string"> <xs:pattern value="[a-z]{2}[0-9]{4}"/> </xs:restriction> </xs:simpleType> </xs:element> • the element “productId” should have totally 8 characters in which the first 2 are smaller case alphabets and the remaining 4 are numbers <productID>cs1234</product> – valid data value <productID>CS123</product> – invalid data value
  • 33. Complex Elements • Complex elements contains other elements and attributes within them <employee id=”101”> <name> Johan </name> <age> 28 </name> <salary> 35000 </salary> </employee> Complex Element Empty Elements Elements that contain only sub elements Elements that contain only text Elements that contains both text and other elements
  • 34. Complex : Empty Element <employee id=”101”/> • This element “employee” does not have any element inside them but do have an attribute named “id” • This makes the element as a complex element • The schema for this represented as <xs:element name="employee"> <xs:complexType> <xs:attribute name="id" type="xs:positiveInteger"/> </xs:complexType> </xs:element>
  • 35. Complex Elements • Elements that contain elements <employee> <name> Tom </name> <age> 28 </name> </employee> Here complex element contains sub elements within them • Schema for the above : <xs:element name="employee"> <xs:complexType> <xs:sequence> <xs:element name="name" type="xs:string"/> <xs:element name="age" type="xs:integer"/> </xs:sequence> </xs:complexType> </xs:element>
  • 36. Contd.. • Mixed type element – contains sub elements, attributes and text in it <xs:element name="MarkedUpDesc"> <xs:complexType mixed="true"> <xs:sequence> <xs:element name="Bold" type="xs:string" /> <xs:element name="Italic" type="xs:string" /> </xs:sequence> </xs:complexType> </xs:element> <MarkedUpDesc> This is an <Bold>Example</Bold> of <Italic>Mixed</Italic> Content. Note there are elements mixed in with the elements data. </MarkedUpDesc>
  • 38. Order Indicators • Sequence indicator – ensures that all the sub elements are defined – can be defined in the same order as given in the XSD <xs:element name="employee"> <xs:complexType> <xs:sequence> <xs:element name="name" type="xs:string"/> <xs:element name="age" type="xs:integer"/> </xs:sequence> </xs:complexType> </xs:element> <employee> <name> Tom </name> <age> 28 </age> </employee> <employee> <age> 28 </age> <name> Tom </name> </employee> Correct Incorrect
  • 39. Contd.. • All indicator – ensures that all the sub elements are defined – can be defined in any order <xs:element name="employee"> <xs:complexType> <xs:all> <xs:element name="name" type="xs:string"/> <xs:element name="age" type="xs:integer"/> </xs:all> </xs:complexType> </xs:element> <employee> <age> 28 </age> <name> Tom </name> </employee> <employee> <name> Tom </name> <age> 28 </age> </employee> Correct Correct
  • 40. Contd.. • Choice indicator – defines that either one of the child element must occur within the element <xs:element name="employee"> <xs:complexType> <xs:choice> <xs:element name="name" type="xs:string"/> <xs:element name="age" type="xs:integer"/> </xs:choice> </xs:complexType> </xs:element> <employee> <age> 28 </age> </employee> <employee> <name> Tom </name> </employee> Correct Correct <employee> <name> Tom </name> <age> 28 </age> </employee> Incorrect
  • 41. Occurrence Indicators • Defines the number of times an element can occur <xs:element name="employee"> <xs:complexType> <xs:sequence> <xs:element name="name" type="xs:string"/> <xs:element name=“childname" type="xs:integer“ minOccurs=”0” maxOccurs="5"/> </xs:sequence> </xs:complexType> </xs:element> <employee> <name> Tom </name> </employee> <employee> <name> Tom </name> <childname>A</childname> </employee> Correct Correct
  • 42. Group • Defines a group of elements • may contain one or more sequence, choice and/or all elements • can occur within complexType, sequence, choice, and restriction
  • 43. Examples: group and sequence <xsd:group name=“personalinfo"> <xsd:sequence> <xsd:element name=“firstname" type=“xsd:string"/> <xsd:element name=“lastname" type=“xsd:string"/> </xsd:sequence> </xsd:group> <xsd:complexType name=“person"> <xsd:group ref=“personalinfo"/> <xsd:attribute name=“citizenship" type=“xsd:string"/> <!-- other elements --> </xsd:complexType> <xsd:sequence minOccurs=“min" maxOccurs=“max"> - - - </xsd:sequence>
  • 44. Example: <?xml version = “1.0"?> <!-- schema.xsd --> <!-- Example W3C XML Schema --> <xsd:schema xmlns:xsd=http://www.w3.org/2000/10/XMLSchema"> <xsd:element name=“message" type=“xsd:string"/> <xsd:element name=“greeting" type=“greetingType"/> <xsd:complexType name=“greetingType“ content=mixed"> <xsd:element ref=“message"/> </xsd:complexType> <xsd:element name=“myMessage" type=“myMessageType"/> <xsd:complexType name=“myMessageType"> <xsd:element ref=“greeting" minOccurs=“0" maxOccurs=“1"/> <xsd:element ref= “message" minOccurs=“1" maxOccurs=“unbounded"/> </xsd:complexType> </xsd:schema>
  • 45. Associating XML with XSD • Define an XSD to create an XML file which contains employee’s information like name, department, salary and email. • There can be many employee details present in the XML file)
  • 46. Employee.xsd <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" > <xs:element name="employees"> <xs:complexType> <xs:sequence> <xs:element name="employee" minOccurs="1" maxOccurs="unbounded“> <xs:complexType> <xs:sequence> <xs:element name="name" type="xs:string"></xs:element> <xs:element name="dept" type="xs:string"></xs:element> <xs:element name="salary" type="xs:float"></xs:element> <xs:element name="email" type="xs:string"></xs:element> </xs:sequence> <xs:attribute name="id" type="xs:positiveInteger"></xs:attribute> </xs:complexType> </xs:element> </xs:sequence> </xs:complexType> </xs:element> </xs:schema>
  • 47. Employee.xml <?xml version="1.0"?> <employees xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="employee.xsd"> <employee id="101"> <name> Tom </name> <department> CSA </department> <salary> 35000 </salary> <email> tom.peter@gmail.com</email> </employee> <employee id="102"> <name>Sam</name> <department>AC</department> <salary>45000</salary> <email>sam.johan@gmail.com</email> </employee> </employees>
  • 48. Dividing the XML Schema • The previous XML Schema is very simple • But it becomes very difficult to read it, and maintain the XML document. • Avoid this by dividing the XML Schema as – define the elements and attributes first and then – make use of them using the “ref” keyword.
  • 49. <?xml version="1.0" encoding="UTF-8"?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> <!-- defining simple elements --> <xs:element name="name" type="xs:string"/> <xs:element name="department" type="xs:string"/> <xs:element name="salary" type="xs:float"/> <xs:element name="email" type="xs:string"/> <!-- defining attributes --> <xs:attribute name="id" type="xs:positiveInteger"/> <!-- defining Complex element --> <xs:element name="employee"> <xs:complexType> <xs:sequence> <xs:element ref="name"/> <xs:element ref="department" /> <xs:element ref="salary" /> <xs:element ref="email" /> </xs:sequence>
  • 50. Contd.. <xs:attribute ref="id"></xs:attribute> </xs:complexType> </xs:element> <xs:element name="employees"> <xs:complexType> <xs:sequence> <xs:element ref="employee" minOccurs="1" maxOccurs="unbounded"/> </xs:sequence> </xs:complexType> </xs:element> </xs:schema>
  • 51. <xsd:complexType name=“fullname"> <xsd:element name=“firstname" type=“xsd:string"/> <xsd:element name=“lastname" type=“xsd:string"/> </xsd:complexType> Alternative: <xsd:element name=“firstname" type=“xsd:string"/> <xsd:element name=“lastname" type=“xsd:string"/> <xsd:complexType name=“fullname"> <xsd:element ref=“firstname"/> <xsd:element ref=“lastname" /> </xsd:complexType>
  • 52. Using Named Types • defines types, that enables you to reuse element definitions • done by giving names to the simpleTypes and complexTypes elements • make them point through the type attribute of the element.
  • 53. Contd.. <?xml version="1.0"?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" > <xs:simpleType name="stringtype"> <xs:restriction base="xs:string"/> </xs:simpleType> <xs:simpleType name="floattype"> <xs:restriction base="xs:float"></xs:restriction> </xs:simpleType> <xs:simpleType name="idtype"> <xs:restriction base="xs:positiveInteger"> <xs:pattern value="[0-9]{3}"></xs:pattern> </xs:restriction> </xs:simpleType> <xs:simpleType name="emailtype"> <xs:restriction base="xs:string"> <xs:maxLength value="30"/> </xs:restriction> </xs:simpleType>
  • 54. <xs:complexType name="employeetype"> <xs:sequence> <xs:element name="name" type="stringtype"/> <xs:element name="department" type="stringtype"/> <xs:element name="salary" type="floattype"/> <xs:element name="email" type="emailtype"/> </xs:sequence> <xs:attribute name="id" type="idtype"/> </xs:complexType> <xs:complexType name="employeestype"> <xs:sequence> <xs:element name="employee" type="employeetype" maxOccurs="unbounded" minOccurs="1"/> </xs:sequence> </xs:complexType> <xs:element name="employees" type="employeestype"/> </xs:schema>