XML schemas provide a more powerful way to define the structure and content of XML documents compared to DTDs. Schemas support data types, namespaces, and more complex definitions of elements and attributes. The main elements used in schemas are:
1. <xs:schema> which defines the root element and namespace for a schema.
2. <xs:element> and <xs:attribute> which define elements and attributes with attributes like name, type, and occurrence.
3. <xs:complexType> which defines complex element types with child elements, attributes, and mixed content.
Schemas allow precise specification of XML documents' structure through elements, attributes, data types and occurrence to enable validation of
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.
14.
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.
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
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.
41.
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.
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"