SlideShare a Scribd company logo
1 of 31
Download to read offline
V.M.Prabhakaran,
Department of CSE,
KIT- Coimbatore
XML Schema
What is an XML Schema?
• An XML Schema describes the structure of an XML
document.
• The XML Schema language is also referred to as
XML Schema Definition (XSD).
• XML documents can have a reference to a DTD or
to an XML Schema.
• The purpose of a Schema is to define the legal
building blocks of an XML document, just like a
DTD.
An XML Schema:
• defines elements that can appear in a document
• defines attributes that can appear within elements
• defines which elements are child elements
• defines the sequence in which the child elements can
appear
• defines the number of child elements
• defines whether an element is empty or can include text
• defines default values for attributes
Schema vs. DTD
• XML Schemas are extensible to future
additions
• XML Schemas are richer and more powerful
than DTDs
• XML Schemas are written in XML
• XML Schemas support data types
• XML Schemas support namespaces
Purpose of an XML Schema
• The purpose of an XML Schema is to define
the legal building blocks of an XML
document:
– the elements and attributes that can appear in a
document
– the number of (and order of) child elements
– data types for elements and attributes
– default and fixed values for elements and attributes
XSD - The <schema> Element
• The <schema> element is the root element of
every XML Schema.
• <?xml version="1.0"?>
<xs:schema>
...
...
</xs:schema>
• The <schema> element may contain some
attributes.
Referring to a schema
• To refer to a DTD in an XML document, the reference goes before the root
element:
– <?xml version="1.0"?>
<!DOCTYPE rootElement SYSTEM "url">
<rootElement> ... </rootElement>
• To refer to an XML Schema in an XML document, the reference goes in the
root element:
– <?xml version="1.0"?>
<rootElement
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
(The XML Schema Instance reference is required)
xsi:noNamespaceSchemaLocation="url.xsd">
(This is where your XML Schema definition can be found)
...
</rootElement>
7
“Simple” and “complex” elements
• A “simple” element is one that contains text and
nothing else
– A simple element cannot have attributes
– A simple element cannot contain other elements
– A simple element cannot be empty
– However, the text can be of many different types,
and may have various restrictions applied to it
• If an element isn’t simple, it’s “complex”
– A complex element may have attributes
– A complex element may be empty, or it may contain
text, other elements, or both text and other elements
Defining a simple element
• A simple element is defined as
<xs:element name="name" type="type" />
where:
– name is the name of the element
– the most common values for type are
xs:boolean xs:integer
xs:date xs:string
xs:decimal xs:time
• Other attributes a simple element may have:
– default="default value" if no other value is
specified
– fixed="value" no other value may be
specified
Example of some XML elements:
• <lastname>Refsnes</lastname>
<age>36</age>
<dateborn>1970-03-27</dateborn>
And here are the corresponding simple element
definitions:
• <xs:element name="lastname" type="xs:string"/>
<xs:element name="age" type="xs:integer"/>
<xs:element name="dateborn" type="xs:date"/>
XSD Example
<?xml version="1.0"?>
<xs:schema
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="note">
<xs:complexType>
<xs:sequence>
<xs:element name="to" type="xs:string"/>
<xs:element name="from" type="xs:string"/>
<xs:element name="heading" type="xs:string"/>
<xs:element name="body" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
Default and Fixed Values for Simple Elements
• Simple elements may have a default value OR a fixed
value specified.
• Default value: Automatically assigned to the element
when no other value is specified.
– In the following example the default value is "red":
– <xs:element name="color" type="xs:string"
default="red"/>
• Fixed value : Automatically assigned to the element,
and you cannot specify another value.
– In the following example the fixed value is "red":
– <xs:element name="color" type="xs:string" fixed="red"/>
Defining an attribute
• Attributes themselves are always declared as
simple types
• An attribute is defined as
<xs:attribute name="name" type="type" />
where:
– name and type are the same as for xs:element
• Other attributes a simple element may have:
– default="default value" if no other value is specified
– fixed="value" no other value may be specified
– use="optional" the attribute is not required
(default)
– use="required" the attribute must be present
XML Schemas Secure Data Communication
• When sending data from a sender to a receiver, it is
essential that both parts have the same "expectations" about
the content.
• With XML Schemas, the sender can describe the data in a
way that the receiver will understand.
• A date like: "03-11-2004" will, in some countries, be
interpreted as 3.November and in other countries as
11.March.
• However, an XML element with a data type like this:
• <date type="date">2004-03-11</date>
• ensures a mutual understanding of the content, because the
XML data type "date" requires the format "YYYY-MM-
DD".
XSD Restrictions
• Restrictions on Values
• Restrictions on a Set of Values
• Restrictions on a Series of Values
• Restrictions on Whitespace Characters
• Restrictions on Length
Restrictions on Values
• The general form for putting a restriction on a text value is:
– <xs:element name="name"> (or xs:attribute)
<xs:restriction base="type">
... the restrictions ...
</xs:restriction>
</xs:element>
• For example:
– <xs:element name="age">
<xs:restriction base="xs:integer">
<xs:minInclusive value="0">
<xs:maxInclusive value="140">
</xs:restriction>
</xs:element>
Restrictions on numbers
• minInclusive -- number must be ≥ the given value
• minExclusive -- number must be > the given value
• maxInclusive -- number must be ≤ the given value
• maxExclusive -- number must be < the given value
• totalDigits -- number must have exactly value digits
• fractionDigits -- number must have no more than value
digits after the decimal point
Restrictions on a Set of Values
• An enumeration restricts the value to be one of a fixed set of
values
• The example below defines an element called "car" with a
restriction. The only acceptable values are: Audi, Golf, BMW:
• <xs:element name="car">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:enumeration value="Audi"/>
<xs:enumeration value="Golf"/>
<xs:enumeration value="BMW"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
Restrictions on a Series of Values
• To limit the content of an XML element to define a series of
numbers or letters that can be used, we would use the pattern
constraint.
• The example below defines an element called "letter" with a
restriction. The only acceptable value is ONE of the
LOWERCASE letters from a to z:
• <xs:element name="letter">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="[a-z]"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
Restrictions on a Series of Values
(continued)
• The next example defines an element called "initials"
with a restriction. The only acceptable value is
THREE of the UPPERCASE letters from a to z:
• <xs:element name="initials">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="[A-Z][A-Z][A-Z]"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
Restrictions on a Series of Values
(continued)
• The next example also defines an element called
"initials" with a restriction. The only acceptable value
is THREE of the LOWERCASE OR UPPERCASE
letters from a to z:
• <xs:element name="initials">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="[a-zA-Z][a-zA-Z][a-zA-Z]"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
Restrictions on a Series of Values
(continued)
• The next example defines an element called
"choice" with a restriction. The only acceptable
value is ONE of the following letters: x, y, OR z:
• <xs:element name="choice">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="[xyz]"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
Restrictions on a Series of Values
(continued)
• The next example defines an element called "prodid"
with a restriction. The only acceptable value is FIVE
digits in a sequence, and each digit must be in a range
from 0 to 9:
• <xs:element name="prodid">
<xs:simpleType>
<xs:restriction base="xs:integer">
<xs:pattern value="[0-9][0-9][0-9][0-9][0-9]"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
Restrictions on Whitespace
Characters
• whiteSpace -- not really a
“restriction”--tells what to
do with whitespace
– value="preserve" Keep
all whitespace
– value="replace"
Change all whitespace
characters to spaces
– value="collapse"
Remove leading and trailing
whitespace, and
replace all
sequences of whitespace with
a single space
<xs:element
name="address">
<xs:simpleType>
<xs:restriction
base="xs:string">
<xs:whiteSpace
value="preserve"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
Restrictions on Length
• To limit the length of a value in an element, we would use the
length, maxLength, and minLength constraints.
• This example defines an element called "password" with a
restriction. The value must be exactly eight characters:
• <xs:element name="password">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:length value="8"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
Restrictions on Length (continued)
• This example defines another element called "password" with
a restriction. The value must be minimum five characters
and maximum eight characters:
• <xs:element name="password">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:minLength value="5"/>
<xs:maxLength value="8"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
Predefined date and time types
• xs:date -- A date in the format CCYY-MM-DD,
for example, 2002-11-05
• xs:time -- A date in the format hh:mm:ss (hours,
minutes, seconds)
• xs:dateTime -- Format is CCYY-MM-
DDThh:mm:ss
– The T is part of the syntax
• Allowable restrictions on dates and times:
– enumeration, minInclusive, minExclusive,
maxInclusive, maxExclusive, pattern, whiteSpace
Predefined numeric types
• Here are some of the predefined numeric types:
• Allowable restrictions on numeric types:
– enumeration, minInclusive, minExclusive, maxInclusive, maxExclusive,
fractionDigits, totalDigits, pattern, whiteSpace
xs:decimal xs:positiveInteger
xs:byte xs:negativeInteger
xs:short xs:nonPositiveInteger
xs:int xs:nonNegativeInteger
xs:long
Example: Shipping Order
<?xml version="1.0"?>
<shipOrder>
<shipTo>
<name>Svendson</name>
<street>Oslo St</street>
<address>400 Main</address>
<country>Norway</country>
</shipTo>
<items>
<item>
<title>Wheel</title>
<quantity>1</quantity>
<price>10.90</price>
</item>
<item>
<title>Cam</title>
<quantity>1</quantity>
<price>9.90</price>
</item>
</items>
</shipOrder>
XML Schema for Shipping Order
<xsd:schema xmlns:xsd=http://www.w3.org/1999/XMLSchema>
<xsd:element name="shipOrder" type="order"/>
<xsd:complexType name="order">
<xsd:element name="shipTo" type="shipAddress"/>
<xsd:element name="items" type="cdItems"/>
</xsd:complexType>
<xsd:complexType name="shipAddress">
<xsd:element name="name“ type="xsd:string"/>
<xsd:element name="street" type="xsd:string"/>
<xsd:element name="address" type="xsd:string"/>
<xsd:element name="country" type="xsd:string"/>
</xsd:complexType>
XML Schema - Shipping Order (continued)
<xsd:complexType name="cdItems">
<xsd:element name="item" type="cdItem"/>
</xsd:complexType>
<xsd:complexType name="cdItem">
<xsd:element name="title" type="xsd:string"/>
<xsd:element name="quantity“ type="xsd:positiveInteger"/>
<xsd:element name="price" type="xsd:decimal"/>
</xsd:complexType>
</xsd:schema>

More Related Content

What's hot

Introduction to XML
Introduction to XMLIntroduction to XML
Introduction to XML
yht4ever
 

What's hot (20)

Css
CssCss
Css
 
Xml namespace
Xml namespaceXml namespace
Xml namespace
 
Xml Schema
Xml SchemaXml Schema
Xml Schema
 
Json
JsonJson
Json
 
02 xml schema
02 xml schema02 xml schema
02 xml schema
 
JavaScript - Chapter 12 - Document Object Model
  JavaScript - Chapter 12 - Document Object Model  JavaScript - Chapter 12 - Document Object Model
JavaScript - Chapter 12 - Document Object Model
 
Xml
XmlXml
Xml
 
Document Object Model (DOM)
Document Object Model (DOM)Document Object Model (DOM)
Document Object Model (DOM)
 
JDBC: java DataBase connectivity
JDBC: java DataBase connectivityJDBC: java DataBase connectivity
JDBC: java DataBase connectivity
 
XML Schemas
XML SchemasXML Schemas
XML Schemas
 
Dom(document object model)
Dom(document object model)Dom(document object model)
Dom(document object model)
 
Introduction to XML
Introduction to XMLIntroduction to XML
Introduction to XML
 
XML, DTD & XSD Overview
XML, DTD & XSD OverviewXML, DTD & XSD Overview
XML, DTD & XSD Overview
 
XSD
XSDXSD
XSD
 
Extensible Markup Language (XML)
Extensible Markup Language (XML)Extensible Markup Language (XML)
Extensible Markup Language (XML)
 
UNIFIED MODELING LANGUAGE
UNIFIED MODELING LANGUAGEUNIFIED MODELING LANGUAGE
UNIFIED MODELING LANGUAGE
 
HTML Forms
HTML FormsHTML Forms
HTML Forms
 
01 xml document structure
01 xml document structure01 xml document structure
01 xml document structure
 
Simple object access protocol(soap )
Simple object access protocol(soap )Simple object access protocol(soap )
Simple object access protocol(soap )
 
XML Schema
XML SchemaXML Schema
XML Schema
 

Similar to Xml schema

Similar to Xml schema (20)

Xml schema
Xml schemaXml schema
Xml schema
 
Xml part4
Xml part4Xml part4
Xml part4
 
Schemas 2 - Restricting Values
Schemas 2 - Restricting ValuesSchemas 2 - Restricting Values
Schemas 2 - Restricting Values
 
XSD Incomplete Overview Draft
XSD Incomplete Overview DraftXSD Incomplete Overview Draft
XSD Incomplete Overview Draft
 
Web Service Workshop - 3 days
Web Service Workshop - 3 daysWeb Service Workshop - 3 days
Web Service Workshop - 3 days
 
XML SCHEMAS
XML SCHEMASXML SCHEMAS
XML SCHEMAS
 
Xml 2
Xml  2 Xml  2
Xml 2
 
Xsd examples
Xsd examplesXsd examples
Xsd examples
 
Xml and DTD's
Xml and DTD'sXml and DTD's
Xml and DTD's
 
Service Oriented Architecture-Unit-1-XML Schema
Service Oriented Architecture-Unit-1-XML SchemaService Oriented Architecture-Unit-1-XML Schema
Service Oriented Architecture-Unit-1-XML Schema
 
XML's validation - XML Schema
XML's validation - XML SchemaXML's validation - XML Schema
XML's validation - XML Schema
 
Xsd
XsdXsd
Xsd
 
Xsd
XsdXsd
Xsd
 
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
 

More from Prabhakaran V M (8)

Strings in python
Strings in pythonStrings in python
Strings in python
 
Operators in python
Operators in pythonOperators in python
Operators in python
 
Algorithmic problem solving
Algorithmic problem solvingAlgorithmic problem solving
Algorithmic problem solving
 
Open mp directives
Open mp directivesOpen mp directives
Open mp directives
 
Html 5
Html 5Html 5
Html 5
 
Introduction to Multi-core Architectures
Introduction to Multi-core ArchitecturesIntroduction to Multi-core Architectures
Introduction to Multi-core Architectures
 
Java threads
Java threadsJava threads
Java threads
 
Applets
AppletsApplets
Applets
 

Recently uploaded

%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
masabamasaba
 

Recently uploaded (20)

WSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go PlatformlessWSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go Platformless
 
WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...
WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...
WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...
 
WSO2CON 2024 - How CSI Piemonte Is Apifying the Public Administration
WSO2CON 2024 - How CSI Piemonte Is Apifying the Public AdministrationWSO2CON 2024 - How CSI Piemonte Is Apifying the Public Administration
WSO2CON 2024 - How CSI Piemonte Is Apifying the Public Administration
 
WSO2Con2024 - Low-Code Integration Tooling
WSO2Con2024 - Low-Code Integration ToolingWSO2Con2024 - Low-Code Integration Tooling
WSO2Con2024 - Low-Code Integration Tooling
 
WSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open Source
WSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open SourceWSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open Source
WSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open Source
 
Artyushina_Guest lecture_YorkU CS May 2024.pptx
Artyushina_Guest lecture_YorkU CS May 2024.pptxArtyushina_Guest lecture_YorkU CS May 2024.pptx
Artyushina_Guest lecture_YorkU CS May 2024.pptx
 
WSO2CON 2024 - Not Just Microservices: Rightsize Your Services!
WSO2CON 2024 - Not Just Microservices: Rightsize Your Services!WSO2CON 2024 - Not Just Microservices: Rightsize Your Services!
WSO2CON 2024 - Not Just Microservices: Rightsize Your Services!
 
WSO2Con2024 - From Blueprint to Brilliance: WSO2's Guide to API-First Enginee...
WSO2Con2024 - From Blueprint to Brilliance: WSO2's Guide to API-First Enginee...WSO2Con2024 - From Blueprint to Brilliance: WSO2's Guide to API-First Enginee...
WSO2Con2024 - From Blueprint to Brilliance: WSO2's Guide to API-First Enginee...
 
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
 
WSO2Con204 - Hard Rock Presentation - Keynote
WSO2Con204 - Hard Rock Presentation - KeynoteWSO2Con204 - Hard Rock Presentation - Keynote
WSO2Con204 - Hard Rock Presentation - Keynote
 
WSO2CON 2024 Slides - Open Source to SaaS
WSO2CON 2024 Slides - Open Source to SaaSWSO2CON 2024 Slides - Open Source to SaaS
WSO2CON 2024 Slides - Open Source to SaaS
 
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
 
WSO2CON 2024 - How to Run a Security Program
WSO2CON 2024 - How to Run a Security ProgramWSO2CON 2024 - How to Run a Security Program
WSO2CON 2024 - How to Run a Security Program
 
Announcing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK SoftwareAnnouncing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK Software
 
WSO2CON 2024 - Architecting AI in the Enterprise: APIs and Applications
WSO2CON 2024 - Architecting AI in the Enterprise: APIs and ApplicationsWSO2CON 2024 - Architecting AI in the Enterprise: APIs and Applications
WSO2CON 2024 - Architecting AI in the Enterprise: APIs and Applications
 
AI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplateAI & Machine Learning Presentation Template
AI & Machine Learning Presentation Template
 
AzureNativeQumulo_HPC_Cloud_Native_Benchmarks.pdf
AzureNativeQumulo_HPC_Cloud_Native_Benchmarks.pdfAzureNativeQumulo_HPC_Cloud_Native_Benchmarks.pdf
AzureNativeQumulo_HPC_Cloud_Native_Benchmarks.pdf
 
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
 
WSO2CON 2024 - Designing Event-Driven Enterprises: Stories of Transformation
WSO2CON 2024 - Designing Event-Driven Enterprises: Stories of TransformationWSO2CON 2024 - Designing Event-Driven Enterprises: Stories of Transformation
WSO2CON 2024 - Designing Event-Driven Enterprises: Stories of Transformation
 
WSO2Con2024 - Simplified Integration: Unveiling the Latest Features in WSO2 L...
WSO2Con2024 - Simplified Integration: Unveiling the Latest Features in WSO2 L...WSO2Con2024 - Simplified Integration: Unveiling the Latest Features in WSO2 L...
WSO2Con2024 - Simplified Integration: Unveiling the Latest Features in WSO2 L...
 

Xml schema

  • 2. What is an XML Schema? • An XML Schema describes the structure of an XML document. • The XML Schema language is also referred to as XML Schema Definition (XSD). • XML documents can have a reference to a DTD or to an XML Schema. • The purpose of a Schema is to define the legal building blocks of an XML document, just like a DTD.
  • 3. An XML Schema: • defines elements that can appear in a document • defines attributes that can appear within elements • defines which elements are child elements • defines the sequence in which the child elements can appear • defines the number of child elements • defines whether an element is empty or can include text • defines default values for attributes
  • 4. Schema vs. DTD • XML Schemas are extensible to future additions • XML Schemas are richer and more powerful than DTDs • XML Schemas are written in XML • XML Schemas support data types • XML Schemas support namespaces
  • 5. Purpose of an XML Schema • The purpose of an XML Schema is to define the legal building blocks of an XML document: – the elements and attributes that can appear in a document – the number of (and order of) child elements – data types for elements and attributes – default and fixed values for elements and attributes
  • 6. XSD - The <schema> Element • The <schema> element is the root element of every XML Schema. • <?xml version="1.0"?> <xs:schema> ... ... </xs:schema> • The <schema> element may contain some attributes.
  • 7. Referring to a schema • To refer to a DTD in an XML document, the reference goes before the root element: – <?xml version="1.0"?> <!DOCTYPE rootElement SYSTEM "url"> <rootElement> ... </rootElement> • To refer to an XML Schema in an XML document, the reference goes in the root element: – <?xml version="1.0"?> <rootElement xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" (The XML Schema Instance reference is required) xsi:noNamespaceSchemaLocation="url.xsd"> (This is where your XML Schema definition can be found) ... </rootElement> 7
  • 8. “Simple” and “complex” elements • A “simple” element is one that contains text and nothing else – A simple element cannot have attributes – A simple element cannot contain other elements – A simple element cannot be empty – However, the text can be of many different types, and may have various restrictions applied to it • If an element isn’t simple, it’s “complex” – A complex element may have attributes – A complex element may be empty, or it may contain text, other elements, or both text and other elements
  • 9. Defining a simple element • A simple element is defined as <xs:element name="name" type="type" /> where: – name is the name of the element – the most common values for type are xs:boolean xs:integer xs:date xs:string xs:decimal xs:time • Other attributes a simple element may have: – default="default value" if no other value is specified – fixed="value" no other value may be specified
  • 10. Example of some XML elements: • <lastname>Refsnes</lastname> <age>36</age> <dateborn>1970-03-27</dateborn> And here are the corresponding simple element definitions: • <xs:element name="lastname" type="xs:string"/> <xs:element name="age" type="xs:integer"/> <xs:element name="dateborn" type="xs:date"/>
  • 11. XSD Example <?xml version="1.0"?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xs:element name="note"> <xs:complexType> <xs:sequence> <xs:element name="to" type="xs:string"/> <xs:element name="from" type="xs:string"/> <xs:element name="heading" type="xs:string"/> <xs:element name="body" type="xs:string"/> </xs:sequence> </xs:complexType> </xs:element> </xs:schema>
  • 12. Default and Fixed Values for Simple Elements • Simple elements may have a default value OR a fixed value specified. • Default value: Automatically assigned to the element when no other value is specified. – In the following example the default value is "red": – <xs:element name="color" type="xs:string" default="red"/> • Fixed value : Automatically assigned to the element, and you cannot specify another value. – In the following example the fixed value is "red": – <xs:element name="color" type="xs:string" fixed="red"/>
  • 13. Defining an attribute • Attributes themselves are always declared as simple types • An attribute is defined as <xs:attribute name="name" type="type" /> where: – name and type are the same as for xs:element • Other attributes a simple element may have: – default="default value" if no other value is specified – fixed="value" no other value may be specified – use="optional" the attribute is not required (default) – use="required" the attribute must be present
  • 14. XML Schemas Secure Data Communication • When sending data from a sender to a receiver, it is essential that both parts have the same "expectations" about the content. • With XML Schemas, the sender can describe the data in a way that the receiver will understand. • A date like: "03-11-2004" will, in some countries, be interpreted as 3.November and in other countries as 11.March. • However, an XML element with a data type like this: • <date type="date">2004-03-11</date> • ensures a mutual understanding of the content, because the XML data type "date" requires the format "YYYY-MM- DD".
  • 15. XSD Restrictions • Restrictions on Values • Restrictions on a Set of Values • Restrictions on a Series of Values • Restrictions on Whitespace Characters • Restrictions on Length
  • 16. Restrictions on Values • The general form for putting a restriction on a text value is: – <xs:element name="name"> (or xs:attribute) <xs:restriction base="type"> ... the restrictions ... </xs:restriction> </xs:element> • For example: – <xs:element name="age"> <xs:restriction base="xs:integer"> <xs:minInclusive value="0"> <xs:maxInclusive value="140"> </xs:restriction> </xs:element>
  • 17. Restrictions on numbers • minInclusive -- number must be ≥ the given value • minExclusive -- number must be > the given value • maxInclusive -- number must be ≤ the given value • maxExclusive -- number must be < the given value • totalDigits -- number must have exactly value digits • fractionDigits -- number must have no more than value digits after the decimal point
  • 18. Restrictions on a Set of Values • An enumeration restricts the value to be one of a fixed set of values • The example below defines an element called "car" with a restriction. The only acceptable values are: Audi, Golf, BMW: • <xs:element name="car"> <xs:simpleType> <xs:restriction base="xs:string"> <xs:enumeration value="Audi"/> <xs:enumeration value="Golf"/> <xs:enumeration value="BMW"/> </xs:restriction> </xs:simpleType> </xs:element>
  • 19. Restrictions on a Series of Values • To limit the content of an XML element to define a series of numbers or letters that can be used, we would use the pattern constraint. • The example below defines an element called "letter" with a restriction. The only acceptable value is ONE of the LOWERCASE letters from a to z: • <xs:element name="letter"> <xs:simpleType> <xs:restriction base="xs:string"> <xs:pattern value="[a-z]"/> </xs:restriction> </xs:simpleType> </xs:element>
  • 20. Restrictions on a Series of Values (continued) • The next example defines an element called "initials" with a restriction. The only acceptable value is THREE of the UPPERCASE letters from a to z: • <xs:element name="initials"> <xs:simpleType> <xs:restriction base="xs:string"> <xs:pattern value="[A-Z][A-Z][A-Z]"/> </xs:restriction> </xs:simpleType> </xs:element>
  • 21. Restrictions on a Series of Values (continued) • The next example also defines an element called "initials" with a restriction. The only acceptable value is THREE of the LOWERCASE OR UPPERCASE letters from a to z: • <xs:element name="initials"> <xs:simpleType> <xs:restriction base="xs:string"> <xs:pattern value="[a-zA-Z][a-zA-Z][a-zA-Z]"/> </xs:restriction> </xs:simpleType> </xs:element>
  • 22. Restrictions on a Series of Values (continued) • The next example defines an element called "choice" with a restriction. The only acceptable value is ONE of the following letters: x, y, OR z: • <xs:element name="choice"> <xs:simpleType> <xs:restriction base="xs:string"> <xs:pattern value="[xyz]"/> </xs:restriction> </xs:simpleType> </xs:element>
  • 23. Restrictions on a Series of Values (continued) • The next example defines an element called "prodid" with a restriction. The only acceptable value is FIVE digits in a sequence, and each digit must be in a range from 0 to 9: • <xs:element name="prodid"> <xs:simpleType> <xs:restriction base="xs:integer"> <xs:pattern value="[0-9][0-9][0-9][0-9][0-9]"/> </xs:restriction> </xs:simpleType> </xs:element>
  • 24. Restrictions on Whitespace Characters • whiteSpace -- not really a “restriction”--tells what to do with whitespace – value="preserve" Keep all whitespace – value="replace" Change all whitespace characters to spaces – value="collapse" Remove leading and trailing whitespace, and replace all sequences of whitespace with a single space <xs:element name="address"> <xs:simpleType> <xs:restriction base="xs:string"> <xs:whiteSpace value="preserve"/> </xs:restriction> </xs:simpleType> </xs:element>
  • 25. Restrictions on Length • To limit the length of a value in an element, we would use the length, maxLength, and minLength constraints. • This example defines an element called "password" with a restriction. The value must be exactly eight characters: • <xs:element name="password"> <xs:simpleType> <xs:restriction base="xs:string"> <xs:length value="8"/> </xs:restriction> </xs:simpleType> </xs:element>
  • 26. Restrictions on Length (continued) • This example defines another element called "password" with a restriction. The value must be minimum five characters and maximum eight characters: • <xs:element name="password"> <xs:simpleType> <xs:restriction base="xs:string"> <xs:minLength value="5"/> <xs:maxLength value="8"/> </xs:restriction> </xs:simpleType> </xs:element>
  • 27. Predefined date and time types • xs:date -- A date in the format CCYY-MM-DD, for example, 2002-11-05 • xs:time -- A date in the format hh:mm:ss (hours, minutes, seconds) • xs:dateTime -- Format is CCYY-MM- DDThh:mm:ss – The T is part of the syntax • Allowable restrictions on dates and times: – enumeration, minInclusive, minExclusive, maxInclusive, maxExclusive, pattern, whiteSpace
  • 28. Predefined numeric types • Here are some of the predefined numeric types: • Allowable restrictions on numeric types: – enumeration, minInclusive, minExclusive, maxInclusive, maxExclusive, fractionDigits, totalDigits, pattern, whiteSpace xs:decimal xs:positiveInteger xs:byte xs:negativeInteger xs:short xs:nonPositiveInteger xs:int xs:nonNegativeInteger xs:long
  • 29. Example: Shipping Order <?xml version="1.0"?> <shipOrder> <shipTo> <name>Svendson</name> <street>Oslo St</street> <address>400 Main</address> <country>Norway</country> </shipTo> <items> <item> <title>Wheel</title> <quantity>1</quantity> <price>10.90</price> </item> <item> <title>Cam</title> <quantity>1</quantity> <price>9.90</price> </item> </items> </shipOrder>
  • 30. XML Schema for Shipping Order <xsd:schema xmlns:xsd=http://www.w3.org/1999/XMLSchema> <xsd:element name="shipOrder" type="order"/> <xsd:complexType name="order"> <xsd:element name="shipTo" type="shipAddress"/> <xsd:element name="items" type="cdItems"/> </xsd:complexType> <xsd:complexType name="shipAddress"> <xsd:element name="name“ type="xsd:string"/> <xsd:element name="street" type="xsd:string"/> <xsd:element name="address" type="xsd:string"/> <xsd:element name="country" type="xsd:string"/> </xsd:complexType>
  • 31. XML Schema - Shipping Order (continued) <xsd:complexType name="cdItems"> <xsd:element name="item" type="cdItem"/> </xsd:complexType> <xsd:complexType name="cdItem"> <xsd:element name="title" type="xsd:string"/> <xsd:element name="quantity“ type="xsd:positiveInteger"/> <xsd:element name="price" type="xsd:decimal"/> </xsd:complexType> </xsd:schema>