SlideShare a Scribd company logo
XML SCHEMAS
Tutorial 4
LIMITATION OF DTD
• DTD has limitation on the number of data type can be used.
• Can not recognize the namespace.
SCHEMAS
● A schema is an XML document that defines the content and structure of one or
more XML documents.
● The XML document that will be validated is called the instance document.
● Why Use an XML Schema?
● With XML Schema, your XML files can carry a description of its own
format.
COMPARING SCHEMASAND DTDS
XML SCHEMA V.S DTD
• XML Schemas are More Powerful than DTD:
• XML Schemas are written in XML.
• XML Schema supports a collection of built-in data types and allows
programmers to define their own data types.
• XML Schemas support data types.
• XML Schemas support namespaces.
STARTING A SCHEMA FILE
• A schema is always placed in a separate XML document that is referenced
by the instance document.
• A file written in XML Schema has the extension .xsd
• Since the Schema file is actually an XML file, it must start with an XML
declaration.
• The root element in any XML Schema document is the schema element ,
and it must declare a namespace for the XML Schema.
<?xml version=“1.0” encoding=“UTF=8”?>
<xs:schema xmlns:xs=”http://www.w3.org/2001/XMLSchema”>
schema content
</xs:schema>
ELEMENTSANDATTRIBUTES OFTHE PATIENTS DOCUMENT
Xml part4
SCHEMATYPES
XML Schema recognize two categories of element types that are:
1. A complex type element has one or more attributes, or is the parent to one or more child
elements.
2. A simple type element contains only character data and has no attributes.
SCHEMATYPES
ELEMENTSANDATTRIBUTES OFTHE PATIENTS DOCUMENT
1- SIMPLE TYPE
ELEMENTS
SIMPLE TYPE ELEMENTS
• Use the following syntax to declare a simple type element in XML Schema:
<xs:element name=“name” type =“xs:type”/>
● Here, name is the name of the element in the instance document and type
is the datatype of the element.
● XML Schema tags must be qualified with the namespace prefix.
Xml part4
DECLARINGANATTRIBUTE
• An attribute is always a simple type. The syntax to define an attribute is
<xs:attribute name="name" type="xs:type” />
• Where name is the name of the attribute and type is the data type.
• You can also define a default value and a fixed value for the attribute:
<xs:attribute name="name" type="xs:type” default=“default value”
fixed=“fixed value" />
• The default and fixed attributes are optional.
* notice that we did not specify which element the attribute belongs to
2- COMPLEX TYPE
ELEMENTS
COMPLEX TYPE ELEMENTS
The basic structure for defining a complex type element with XML Schema is
<xs:element name="name">
<xs:complexType>
declarations
</xs:complexType>
</xs:element>
Where name is the name of the element and declarations are the declarations of the child elements or attributes
associated with the element.
COMPLEX TYPE ELEMENTS
Five complex type elements that usually appear in an instance document are
the following:
1–The element is an empty element and contains attributes only.
2–The element contains child elements only.
3–The element contains both child elements and attributes.
4–The element contains both child elements and text.
5–The element contains both attributes and text but no child element.
1- ELEMENTS WITHATTRIBUTES ONLY
• The code to declare an element with attributes only:
<xs:element name="name">
<xs:complexType>
attributes
</xs:complexType>
</xs:element>
• Where attributes is the set of declarations that define the attributes associated with the
element. For example, the empty element:
<subject name="Cynthia Dibbs" age="62" />
• The code for this complex type element has the following structure:
<xs:element name="subject">
<xs:complexType>
<xs:attribute name="name" type="xs:string" />
<xs:attribute name="age" type="xs:string" />
</xs:complexType>
</xs:element>
SPECIFYING THE USE OFANATTRIBUTE
• An attribute may or may not be required with a particular element. To indicate whether an
attribute is required, you add the “use” attribute to the element declaration. The “use” attribute
has the following values:
• required: The attribute must always appear with the element
• optional: The use of the attribute is optional with the element
• prohibited: The attribute cannot be used with the element
• If you neglect to add the “use” attribute to an element declaration, the parser assumes that the
attribute is optional (meaning that “optional” is the default value for the “use” attribute).
• Example:
2-ELEMENTS WITH CHILD ELEMENTS ONLY
To define child elements, use the code structure:
<xs:element name="name">
<xs:complexType>
<xs:compositor>
elements
</xs:compositor>
</xs:complexType>
</xs:element>
Where elements is the list of simple type element declarations for each child element, and compositor defines
how the child elements are organized.
USING COMPOSITORS
XML Schema supports the following compositors:
• sequence: defines a specific order for the child elements
• choice: allows any one of the child elements to appear in the instance
document
• all: allows any of the child elements to appear in any order in the
instance document; however, they must appear at least once.
2-ELEMENTS WITH CHILD ELEMENTS ONLY(SEQUANCE EXAMPLE)
<element name="address">
<xs:complexType>
<xs:sequence>
<xs:element name="street" type="xs:string"/>
<xs:element name="city" type="xs:string"/>
<xs:element name="state" type="xs:string"/>
<xs:element name="country" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
2-ELEMENTS WITH CHILD ELEMENTS ONLY
Example for choice compositor:
Example for all compositor:
COMBINING/NESTING COMPOSITORS
<xs:element name=“employee”>
<xs:complexType>
<xs:sequence>
<xs:element name=“name” type=“xs:string”/>
<xs:choice>
<xs:element name=“age” type=“xs:integer”/>
<xs:element name=“DOB” type=“xs:date”/>
</xs:choice>
<xs:all>
<xs:element name=“phone” type=“xs:string”/>
<xs:element name=“mobile” type=“xs:string”/>
<xs:element name=“email” type=“xs:string”/>
</xs:all>
</xs:sequence>
</xs:complexType>
</xs:element>
3- ELEMENTS WITH CHILD ELEMENTSANDATTRIBUTES
The code for a complex type element that contains both attributes and child elements is
<xs:element name="name">
<xs:complexType>
<xs:compositor>
Child Elements
</xs:compositor>
attributes
</xs:complexType>
</xs:element>
• The patient element contains two attributes (patID and onStudy) and seven child elements
(lastName, firstName, dateOfBirth, age, stage, comment, and performance.)
<xs:element name="patient">
<xs:complexType>
<xs:sequence>
<xs:element ref="lastName"/>
<xs:element ref="firstName"/>
<xs:element ref="dateOfBirth"/>
<xs:element ref="age"/>
<xs:element ref="stage"/>
<xs:element ref="comment" minOccurs="0" maxOccurs="unbounded"/>
<xs:element ref="performance"/>
</xs:sequence>
<xs:attribute ref="patID" use="required"/>
<xs:attribute ref="onStudy" use="required"/>
</xs:complexType>
</xs:element>
3- ELEMENTS WITH CHILD ELEMENTSANDATTRIBUTES
3- ELEMENTS WITH CHILD ELEMENTSANDATTRIBUTES
4- SPECIFYING MIXED CONTENT
• To declare an element that contains child elements and text, add the “mixed”
attribute and set it to “true”.
• Note that XML Schema allows content text to appear before, between, and after any
of the child elements.
For example, the XML content:
<Summary>
Patient <Name>Cynthia Davis</Name> was enrolled in
the <Study>Tamoxifen Study</Study> on 8/15/2003.
</Summary>
Can be declared in the schema file using the following complex type:
<xs:element name="Summary">
<xs:complexType mixed="true">
<xs:sequence>
<xs:element name="Name" type=“xs:string"/>
<xs:element name="Study" type=“xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
4- SPECIFYING MIXED CONTENT
5- ELEMENTS WITH TEXTANDATTRIBUTES BUT NO CHILD ELEMENTS
If an element contains text and attributes (but no child elements), the structure of the
complex type element is slightly different.
<xs:element name="name">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="xs:type">
attributes
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
For example, the empty element:
<performance scale="Karnofsky"> 0.81 </performance>
The code to associate the scale attribute with the performance element would
therefore be
<xs:element name="performance">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="xs:string">
<xs:attribute name="scale"
type="xs:string" />
</xs:extension>
</xs:simpleContent>
</xs:complexType>
5- ELEMENTS WITH TEXTANDATTRIBUTES BUT NO CHILD
ELEMENTS
REFERENCINGAN ELEMENT ORATTRIBUTE
XML Schema allows for a great deal of flexibility in designing complex types. Rather
than nesting the attribute declaration within the element, you can create a reference to
it. The code to create a reference to an element or attribute declaration is
<xs:element ref="elemName" />
<xs:attribute ref="attName" />
Where elemName is the name used in an element declaration and attName is the name
used in an attribute declaration
EXAMPLE OFREFERENCINGAN ELEMENT ORATTRIBUTE
EXAMPLE OFREFERENCINGAN ELEMENT ORATTRIBUTE
SPECIFYING THE OCCURRENCE OFAN ITEM
• You can specify the occurrence of an element using the attributes minOccurs and maxOccurs.
• The minOccurs and maxOccurs can be any positive value
• The maxOccurs can have a value of “unbounded” for unlimited occurrences of the child
element
if minOccurs=”0” and maxOccurs is omitted ==> maxOccurs=”1” ==> the element is optional
if minOccurs=”x” and maxOccurs is omitted ==> maxOccurs=”x” (where x is any number greater
than 0)
if minOccurs and maxOccurs are both omitted ==> minOccurs=”1” and maxOccurs=”1”
*The default value for minOccurs and maxOccurs is “1”
“+” in DTDs is : minOccurs=“1” maxOccurs=“unbound”
“*” in DTDs is : minOccurs=“0” maxOccurs=“unbound”
“?” in DTDs is : minOccurs=“0” maxOccurs=“1”
SPECIFYING THE OCCURRENCE OFAN ITEM
SPECIFYING THE OCCURRENCE OFASEQUENCE
minOccurs and maxOccurs attributes can also be used with compositors to
repeat entire sequences of items
The Scope of an Element
Global Scope: Declarations that are placed as children of the root schema
element have a global scope, and can be referenced throughout the schema file.
Local Scope: Declarations that are nested within a complex type have a local
scope; they cannot be referenced elsewhere.
*Note:
• The attribute “use” for attributes is set locally.
• The attributes minOccurs and maxOccurs for elements are set locally.
• We declare the element or attribute globally, then specify the restrictions
locally.
Xml part4
XML SCHEMA DATA TYPES
• XML Schema supports two general categories of data types:
• A built-in data type is part of the XML Schema language and is
available to all XML Schema authors.
• A user-derived data type is created by a schema author for
specific data values in an instance document.
XML SCHEMA DATA TYPES
• XML Schema divides its built-in data types into two classes:
• A primitive data type, also called a base type, is one of 19 fundamental
data types not defined in terms of other types.
• A derived data type is a collection of 25 data types that the XML
Schema developers created based on the 19 primitive types.
• Unlike DTDs, schemas use the same data types for both elements and
attributes.
Xml part4
STRING DATA TYPES
STRING DATA TYPES
the string data types that we will use are: string , ID
NUMERIC DATA TYPES
NUMERIC DATA TYPES
DATES AND TIMES
DATES AND TIMES
the date and time data types we will use are: date , time
PATIENT.XSD USING THE “REF”
PATIENT.XSD WITHOUT USING THE “REF”
APPLYINGASCHEMATOAN XMLDOCUMENT
● To attach a schema to the document, you must do the following:
–Declare a namespace for XML Schema in the instance document.
–Indicate the location of the schema file.
● To declare the XML Schema namespace in the instance document, you add the
following attribute to the document’s root element:
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
● If there is no namespace for the contents of the instance document, add the
following attribute to the root element:
xsi:noNamespaceSchemaLocation="schemaFile.xsd"
APPLYINGASCHEMATOAN XMLDOCUMENT
SUMMARY: SCHEMA ELEMENTS AND
ATTRIBUTES
Element Attribute
xs:schema xmlns:xs
xs:element name, type, minOccurs, maxOccurs, ref
xs:attribute name, type, use, default, fixed
xs:complexType mixed
xs:sequence minOccurs, maxOccurs
xs:choice minOccurs, maxOccurs
xs:all minOccurs, maxOccurs

More Related Content

What's hot (20)

PDF
Xml schema
Prabhakaran V M
 
PPTX
XSLT
Surinder Kaur
 
PPT
Xsd examples
Bình Trọng Án
 
PPTX
Xml schema
sana mateen
 
PPT
Xml Schema
vikram singh
 
PPTX
Xslt
Mahara Jothi
 
PPTX
Introduction to SQL
Amin Choroomi
 
PDF
XSD Incomplete Overview Draft
Pedro De Almeida
 
PDF
XSLT. Basic.
Alexander Kirillov
 
PPTX
Xslt tutorial
Bijoy Kureekkal
 
PPT
Xslt
Manav Prasad
 
PPTX
Xml 2
pavishkumarsingh
 
PDF
Xsd tutorial
Ashoka Vanjare
 
PPTX
Advanced SQL Webinar
Ram Kedem
 
PPTX
Chapter 18
application developer
 
PPTX
Introduction to SQL (for Chicago Booth MBA technology club)
Jennifer Berk
 
PPTX
XML SCHEMAS
SaraswathiRamalingam
 
ODP
BIS05 Introduction to SQL
Prithwis Mukerjee
 
PPT
Introduction to structured query language (sql)
Sabana Maharjan
 
Xml schema
Prabhakaran V M
 
Xsd examples
Bình Trọng Án
 
Xml schema
sana mateen
 
Xml Schema
vikram singh
 
Introduction to SQL
Amin Choroomi
 
XSD Incomplete Overview Draft
Pedro De Almeida
 
XSLT. Basic.
Alexander Kirillov
 
Xslt tutorial
Bijoy Kureekkal
 
Xsd tutorial
Ashoka Vanjare
 
Advanced SQL Webinar
Ram Kedem
 
Introduction to SQL (for Chicago Booth MBA technology club)
Jennifer Berk
 
BIS05 Introduction to SQL
Prithwis Mukerjee
 
Introduction to structured query language (sql)
Sabana Maharjan
 

Viewers also liked (20)

PPTX
Xml part 6
NOHA AW
 
PPT
Web Services
Gaurav Tyagi
 
PPTX
The Mystical Principles of XSLT: Enlightenment through Software Visualization
evanlenz
 
PPTX
Unleashing the Power of XSLT: Catalog Records in Batch
c7002593
 
PPTX
Applying an IBM SOA Approach to Manual Processes Automation
Prolifics
 
PPTX
XML - Displaying Data ith XSLT
Dudy Ali
 
ODP
Interoperable Web Services with JAX-WS
Carol McDonald
 
PDF
SOA Governance and WebSphere Service Registry and Repository
IBM Sverige
 
PPT
Open Id, O Auth And Webservices
Myles Eftos
 
PDF
XSLT for Web Developers
Sanders Kleinfeld
 
PPT
Web Services
Gaurav Tyagi
 
PDF
Web services
Michael Weiss
 
PPT
WebService-Java
halwal
 
PPT
Siebel Web Service
NAVINKUMAR RAI
 
PPTX
CTDA Workshop on XSL
University of Connecticut Libraries
 
PPT
RESTful services
gouthamrv
 
PDF
Java web services using JAX-WS
IndicThreads
 
PPTX
XSLT
rpoplai
 
PDF
OAuth 2.0 with IBM WebSphere DataPower
Shiu-Fun Poon
 
PDF
SOAP-based Web Services
Katrien Verbert
 
Xml part 6
NOHA AW
 
Web Services
Gaurav Tyagi
 
The Mystical Principles of XSLT: Enlightenment through Software Visualization
evanlenz
 
Unleashing the Power of XSLT: Catalog Records in Batch
c7002593
 
Applying an IBM SOA Approach to Manual Processes Automation
Prolifics
 
XML - Displaying Data ith XSLT
Dudy Ali
 
Interoperable Web Services with JAX-WS
Carol McDonald
 
SOA Governance and WebSphere Service Registry and Repository
IBM Sverige
 
Open Id, O Auth And Webservices
Myles Eftos
 
XSLT for Web Developers
Sanders Kleinfeld
 
Web Services
Gaurav Tyagi
 
Web services
Michael Weiss
 
WebService-Java
halwal
 
Siebel Web Service
NAVINKUMAR RAI
 
RESTful services
gouthamrv
 
Java web services using JAX-WS
IndicThreads
 
XSLT
rpoplai
 
OAuth 2.0 with IBM WebSphere DataPower
Shiu-Fun Poon
 
SOAP-based Web Services
Katrien Verbert
 
Ad

Similar to Xml part4 (20)

DOC
Xsd
xavier john
 
PDF
XML Schema.pdf
KGSCSEPSGCT
 
PDF
Xml schema
Dr.Saranya K.G
 
PPT
02 xml schema
Baskarkncet
 
PPTX
XML, DTD & XSD Overview
Pradeep Rapolu
 
PPTX
IPT Chapter 3 Data Mapping and Exchange - Dr. J. VijiPriya
VijiPriya Jeyamani
 
DOCX
Introduction to xml schema
Abhishek Kesharwani
 
PPTX
XML-Schema-Elements_Types_Attributes.pptx
AkshayKumar100378
 
PPT
uptu web technology unit 2 Xml2
Abhishek Kesharwani
 
PPT
XML Schema
yht4ever
 
PPTX
https://www.slideshare.net/slideshow/cyber-security-introductionpptx-25514344...
nivin112224
 
PPTX
XML Schemas
People Strategists
 
PPTX
XML Schema.pptx
JohnsonDcunha1
 
PDF
Xml Schema Essentials First Edition R Allen Wyke Andrew Watt
esgarkavos
 
PPT
Xml schema
Luis Goldster
 
PPT
Xml schema
James Wong
 
PPT
Xml schema
Tony Nguyen
 
PPT
Xml schema
David Hoen
 
PPT
Xml schema
Young Alista
 
XML Schema.pdf
KGSCSEPSGCT
 
Xml schema
Dr.Saranya K.G
 
02 xml schema
Baskarkncet
 
XML, DTD & XSD Overview
Pradeep Rapolu
 
IPT Chapter 3 Data Mapping and Exchange - Dr. J. VijiPriya
VijiPriya Jeyamani
 
Introduction to xml schema
Abhishek Kesharwani
 
XML-Schema-Elements_Types_Attributes.pptx
AkshayKumar100378
 
uptu web technology unit 2 Xml2
Abhishek Kesharwani
 
XML Schema
yht4ever
 
https://www.slideshare.net/slideshow/cyber-security-introductionpptx-25514344...
nivin112224
 
XML Schemas
People Strategists
 
XML Schema.pptx
JohnsonDcunha1
 
Xml Schema Essentials First Edition R Allen Wyke Andrew Watt
esgarkavos
 
Xml schema
Luis Goldster
 
Xml schema
James Wong
 
Xml schema
Tony Nguyen
 
Xml schema
David Hoen
 
Xml schema
Young Alista
 
Ad

Recently uploaded (20)

PDF
Windsurf Meetup Ottawa 2025-07-12 - Planning Mode at Reliza.pdf
Pavel Shukhman
 
PDF
TrustArc Webinar - Data Privacy Trends 2025: Mid-Year Insights & Program Stra...
TrustArc
 
PDF
CIFDAQ Weekly Market Wrap for 11th July 2025
CIFDAQ
 
PDF
SFWelly Summer 25 Release Highlights July 2025
Anna Loughnan Colquhoun
 
PPTX
✨Unleashing Collaboration: Salesforce Channels & Community Power in Patna!✨
SanjeetMishra29
 
PDF
Empower Inclusion Through Accessible Java Applications
Ana-Maria Mihalceanu
 
PDF
Predicting the unpredictable: re-engineering recommendation algorithms for fr...
Speck&Tech
 
PDF
Presentation - Vibe Coding The Future of Tech
yanuarsinggih1
 
PDF
CIFDAQ Token Spotlight for 9th July 2025
CIFDAQ
 
PDF
NewMind AI - Journal 100 Insights After The 100th Issue
NewMind AI
 
PDF
Chris Elwell Woburn, MA - Passionate About IT Innovation
Chris Elwell Woburn, MA
 
PDF
DevBcn - Building 10x Organizations Using Modern Productivity Metrics
Justin Reock
 
PPTX
UiPath Academic Alliance Educator Panels: Session 2 - Business Analyst Content
DianaGray10
 
PPT
Interview paper part 3, It is based on Interview Prep
SoumyadeepGhosh39
 
PDF
Human-centred design in online workplace learning and relationship to engagem...
Tracy Tang
 
PPTX
AUTOMATION AND ROBOTICS IN PHARMA INDUSTRY.pptx
sameeraaabegumm
 
PPTX
Top iOS App Development Company in the USA for Innovative Apps
SynapseIndia
 
PDF
Persuasive AI: risks and opportunities in the age of digital debate
Speck&Tech
 
PPTX
OpenID AuthZEN - Analyst Briefing July 2025
David Brossard
 
PDF
Complete Network Protection with Real-Time Security
L4RGINDIA
 
Windsurf Meetup Ottawa 2025-07-12 - Planning Mode at Reliza.pdf
Pavel Shukhman
 
TrustArc Webinar - Data Privacy Trends 2025: Mid-Year Insights & Program Stra...
TrustArc
 
CIFDAQ Weekly Market Wrap for 11th July 2025
CIFDAQ
 
SFWelly Summer 25 Release Highlights July 2025
Anna Loughnan Colquhoun
 
✨Unleashing Collaboration: Salesforce Channels & Community Power in Patna!✨
SanjeetMishra29
 
Empower Inclusion Through Accessible Java Applications
Ana-Maria Mihalceanu
 
Predicting the unpredictable: re-engineering recommendation algorithms for fr...
Speck&Tech
 
Presentation - Vibe Coding The Future of Tech
yanuarsinggih1
 
CIFDAQ Token Spotlight for 9th July 2025
CIFDAQ
 
NewMind AI - Journal 100 Insights After The 100th Issue
NewMind AI
 
Chris Elwell Woburn, MA - Passionate About IT Innovation
Chris Elwell Woburn, MA
 
DevBcn - Building 10x Organizations Using Modern Productivity Metrics
Justin Reock
 
UiPath Academic Alliance Educator Panels: Session 2 - Business Analyst Content
DianaGray10
 
Interview paper part 3, It is based on Interview Prep
SoumyadeepGhosh39
 
Human-centred design in online workplace learning and relationship to engagem...
Tracy Tang
 
AUTOMATION AND ROBOTICS IN PHARMA INDUSTRY.pptx
sameeraaabegumm
 
Top iOS App Development Company in the USA for Innovative Apps
SynapseIndia
 
Persuasive AI: risks and opportunities in the age of digital debate
Speck&Tech
 
OpenID AuthZEN - Analyst Briefing July 2025
David Brossard
 
Complete Network Protection with Real-Time Security
L4RGINDIA
 

Xml part4

  • 2. LIMITATION OF DTD • DTD has limitation on the number of data type can be used. • Can not recognize the namespace.
  • 3. SCHEMAS ● A schema is an XML document that defines the content and structure of one or more XML documents. ● The XML document that will be validated is called the instance document. ● Why Use an XML Schema? ● With XML Schema, your XML files can carry a description of its own format.
  • 5. XML SCHEMA V.S DTD • XML Schemas are More Powerful than DTD: • XML Schemas are written in XML. • XML Schema supports a collection of built-in data types and allows programmers to define their own data types. • XML Schemas support data types. • XML Schemas support namespaces.
  • 6. STARTING A SCHEMA FILE • A schema is always placed in a separate XML document that is referenced by the instance document. • A file written in XML Schema has the extension .xsd • Since the Schema file is actually an XML file, it must start with an XML declaration. • The root element in any XML Schema document is the schema element , and it must declare a namespace for the XML Schema. <?xml version=“1.0” encoding=“UTF=8”?> <xs:schema xmlns:xs=”http://www.w3.org/2001/XMLSchema”> schema content </xs:schema>
  • 9. SCHEMATYPES XML Schema recognize two categories of element types that are: 1. A complex type element has one or more attributes, or is the parent to one or more child elements. 2. A simple type element contains only character data and has no attributes.
  • 13. SIMPLE TYPE ELEMENTS • Use the following syntax to declare a simple type element in XML Schema: <xs:element name=“name” type =“xs:type”/> ● Here, name is the name of the element in the instance document and type is the datatype of the element. ● XML Schema tags must be qualified with the namespace prefix.
  • 15. DECLARINGANATTRIBUTE • An attribute is always a simple type. The syntax to define an attribute is <xs:attribute name="name" type="xs:type” /> • Where name is the name of the attribute and type is the data type. • You can also define a default value and a fixed value for the attribute: <xs:attribute name="name" type="xs:type” default=“default value” fixed=“fixed value" /> • The default and fixed attributes are optional.
  • 16. * notice that we did not specify which element the attribute belongs to
  • 18. COMPLEX TYPE ELEMENTS The basic structure for defining a complex type element with XML Schema is <xs:element name="name"> <xs:complexType> declarations </xs:complexType> </xs:element> Where name is the name of the element and declarations are the declarations of the child elements or attributes associated with the element.
  • 19. COMPLEX TYPE ELEMENTS Five complex type elements that usually appear in an instance document are the following: 1–The element is an empty element and contains attributes only. 2–The element contains child elements only. 3–The element contains both child elements and attributes. 4–The element contains both child elements and text. 5–The element contains both attributes and text but no child element.
  • 20. 1- ELEMENTS WITHATTRIBUTES ONLY • The code to declare an element with attributes only: <xs:element name="name"> <xs:complexType> attributes </xs:complexType> </xs:element> • Where attributes is the set of declarations that define the attributes associated with the element. For example, the empty element: <subject name="Cynthia Dibbs" age="62" /> • The code for this complex type element has the following structure: <xs:element name="subject"> <xs:complexType> <xs:attribute name="name" type="xs:string" /> <xs:attribute name="age" type="xs:string" /> </xs:complexType> </xs:element>
  • 21. SPECIFYING THE USE OFANATTRIBUTE • An attribute may or may not be required with a particular element. To indicate whether an attribute is required, you add the “use” attribute to the element declaration. The “use” attribute has the following values: • required: The attribute must always appear with the element • optional: The use of the attribute is optional with the element • prohibited: The attribute cannot be used with the element • If you neglect to add the “use” attribute to an element declaration, the parser assumes that the attribute is optional (meaning that “optional” is the default value for the “use” attribute). • Example:
  • 22. 2-ELEMENTS WITH CHILD ELEMENTS ONLY To define child elements, use the code structure: <xs:element name="name"> <xs:complexType> <xs:compositor> elements </xs:compositor> </xs:complexType> </xs:element> Where elements is the list of simple type element declarations for each child element, and compositor defines how the child elements are organized.
  • 23. USING COMPOSITORS XML Schema supports the following compositors: • sequence: defines a specific order for the child elements • choice: allows any one of the child elements to appear in the instance document • all: allows any of the child elements to appear in any order in the instance document; however, they must appear at least once.
  • 24. 2-ELEMENTS WITH CHILD ELEMENTS ONLY(SEQUANCE EXAMPLE) <element name="address"> <xs:complexType> <xs:sequence> <xs:element name="street" type="xs:string"/> <xs:element name="city" type="xs:string"/> <xs:element name="state" type="xs:string"/> <xs:element name="country" type="xs:string"/> </xs:sequence> </xs:complexType> </xs:element>
  • 25. 2-ELEMENTS WITH CHILD ELEMENTS ONLY Example for choice compositor: Example for all compositor:
  • 26. COMBINING/NESTING COMPOSITORS <xs:element name=“employee”> <xs:complexType> <xs:sequence> <xs:element name=“name” type=“xs:string”/> <xs:choice> <xs:element name=“age” type=“xs:integer”/> <xs:element name=“DOB” type=“xs:date”/> </xs:choice> <xs:all> <xs:element name=“phone” type=“xs:string”/> <xs:element name=“mobile” type=“xs:string”/> <xs:element name=“email” type=“xs:string”/> </xs:all> </xs:sequence> </xs:complexType> </xs:element>
  • 27. 3- ELEMENTS WITH CHILD ELEMENTSANDATTRIBUTES The code for a complex type element that contains both attributes and child elements is <xs:element name="name"> <xs:complexType> <xs:compositor> Child Elements </xs:compositor> attributes </xs:complexType> </xs:element>
  • 28. • The patient element contains two attributes (patID and onStudy) and seven child elements (lastName, firstName, dateOfBirth, age, stage, comment, and performance.) <xs:element name="patient"> <xs:complexType> <xs:sequence> <xs:element ref="lastName"/> <xs:element ref="firstName"/> <xs:element ref="dateOfBirth"/> <xs:element ref="age"/> <xs:element ref="stage"/> <xs:element ref="comment" minOccurs="0" maxOccurs="unbounded"/> <xs:element ref="performance"/> </xs:sequence> <xs:attribute ref="patID" use="required"/> <xs:attribute ref="onStudy" use="required"/> </xs:complexType> </xs:element> 3- ELEMENTS WITH CHILD ELEMENTSANDATTRIBUTES
  • 29. 3- ELEMENTS WITH CHILD ELEMENTSANDATTRIBUTES
  • 30. 4- SPECIFYING MIXED CONTENT • To declare an element that contains child elements and text, add the “mixed” attribute and set it to “true”. • Note that XML Schema allows content text to appear before, between, and after any of the child elements.
  • 31. For example, the XML content: <Summary> Patient <Name>Cynthia Davis</Name> was enrolled in the <Study>Tamoxifen Study</Study> on 8/15/2003. </Summary> Can be declared in the schema file using the following complex type: <xs:element name="Summary"> <xs:complexType mixed="true"> <xs:sequence> <xs:element name="Name" type=“xs:string"/> <xs:element name="Study" type=“xs:string"/> </xs:sequence> </xs:complexType> </xs:element> 4- SPECIFYING MIXED CONTENT
  • 32. 5- ELEMENTS WITH TEXTANDATTRIBUTES BUT NO CHILD ELEMENTS If an element contains text and attributes (but no child elements), the structure of the complex type element is slightly different. <xs:element name="name"> <xs:complexType> <xs:simpleContent> <xs:extension base="xs:type"> attributes </xs:extension> </xs:simpleContent> </xs:complexType> </xs:element>
  • 33. For example, the empty element: <performance scale="Karnofsky"> 0.81 </performance> The code to associate the scale attribute with the performance element would therefore be <xs:element name="performance"> <xs:complexType> <xs:simpleContent> <xs:extension base="xs:string"> <xs:attribute name="scale" type="xs:string" /> </xs:extension> </xs:simpleContent> </xs:complexType> 5- ELEMENTS WITH TEXTANDATTRIBUTES BUT NO CHILD ELEMENTS
  • 34. REFERENCINGAN ELEMENT ORATTRIBUTE XML Schema allows for a great deal of flexibility in designing complex types. Rather than nesting the attribute declaration within the element, you can create a reference to it. The code to create a reference to an element or attribute declaration is <xs:element ref="elemName" /> <xs:attribute ref="attName" /> Where elemName is the name used in an element declaration and attName is the name used in an attribute declaration
  • 37. SPECIFYING THE OCCURRENCE OFAN ITEM • You can specify the occurrence of an element using the attributes minOccurs and maxOccurs. • The minOccurs and maxOccurs can be any positive value • The maxOccurs can have a value of “unbounded” for unlimited occurrences of the child element if minOccurs=”0” and maxOccurs is omitted ==> maxOccurs=”1” ==> the element is optional if minOccurs=”x” and maxOccurs is omitted ==> maxOccurs=”x” (where x is any number greater than 0) if minOccurs and maxOccurs are both omitted ==> minOccurs=”1” and maxOccurs=”1” *The default value for minOccurs and maxOccurs is “1” “+” in DTDs is : minOccurs=“1” maxOccurs=“unbound” “*” in DTDs is : minOccurs=“0” maxOccurs=“unbound” “?” in DTDs is : minOccurs=“0” maxOccurs=“1”
  • 39. SPECIFYING THE OCCURRENCE OFASEQUENCE minOccurs and maxOccurs attributes can also be used with compositors to repeat entire sequences of items
  • 40. The Scope of an Element Global Scope: Declarations that are placed as children of the root schema element have a global scope, and can be referenced throughout the schema file. Local Scope: Declarations that are nested within a complex type have a local scope; they cannot be referenced elsewhere. *Note: • The attribute “use” for attributes is set locally. • The attributes minOccurs and maxOccurs for elements are set locally. • We declare the element or attribute globally, then specify the restrictions locally.
  • 42. XML SCHEMA DATA TYPES • XML Schema supports two general categories of data types: • A built-in data type is part of the XML Schema language and is available to all XML Schema authors. • A user-derived data type is created by a schema author for specific data values in an instance document.
  • 43. XML SCHEMA DATA TYPES • XML Schema divides its built-in data types into two classes: • A primitive data type, also called a base type, is one of 19 fundamental data types not defined in terms of other types. • A derived data type is a collection of 25 data types that the XML Schema developers created based on the 19 primitive types. • Unlike DTDs, schemas use the same data types for both elements and attributes.
  • 46. STRING DATA TYPES the string data types that we will use are: string , ID
  • 50. DATES AND TIMES the date and time data types we will use are: date , time
  • 52. PATIENT.XSD WITHOUT USING THE “REF”
  • 53. APPLYINGASCHEMATOAN XMLDOCUMENT ● To attach a schema to the document, you must do the following: –Declare a namespace for XML Schema in the instance document. –Indicate the location of the schema file. ● To declare the XML Schema namespace in the instance document, you add the following attribute to the document’s root element: xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" ● If there is no namespace for the contents of the instance document, add the following attribute to the root element: xsi:noNamespaceSchemaLocation="schemaFile.xsd"
  • 55. SUMMARY: SCHEMA ELEMENTS AND ATTRIBUTES Element Attribute xs:schema xmlns:xs xs:element name, type, minOccurs, maxOccurs, ref xs:attribute name, type, use, default, fixed xs:complexType mixed xs:sequence minOccurs, maxOccurs xs:choice minOccurs, maxOccurs xs:all minOccurs, maxOccurs