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
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.
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 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.
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

Xml part4

  • 1.
  • 2.
    LIMITATION OF DTD •DTD has limitation on the number of data type can be used. • Can not recognize the namespace.
  • 3.
    SCHEMAS ● A schemais 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.
  • 4.
  • 5.
    XML SCHEMA V.SDTD • 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 SCHEMAFILE • 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>
  • 7.
  • 9.
    SCHEMATYPES XML Schema recognizetwo 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.
  • 10.
  • 11.
  • 12.
  • 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 attributeis 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 thatwe did not specify which element the attribute belongs to
  • 17.
  • 18.
    COMPLEX TYPE ELEMENTS Thebasic 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 Fivecomplex 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 WITHATTRIBUTESONLY • 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 USEOFANATTRIBUTE • 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 CHILDELEMENTS 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 Schemasupports 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 CHILDELEMENTS 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 CHILDELEMENTS ONLY Example for choice compositor: Example for all compositor:
  • 26.
    COMBINING/NESTING COMPOSITORS <xs:element name=“employee”> <xs:complexType> <xs:sequence> <xs:elementname=“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 WITHCHILD 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 patientelement 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 WITHCHILD ELEMENTSANDATTRIBUTES
  • 30.
    4- SPECIFYING MIXEDCONTENT • 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, theXML 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 WITHTEXTANDATTRIBUTES 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, theempty 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 XMLSchema 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
  • 35.
  • 36.
  • 37.
    SPECIFYING THE OCCURRENCEOFAN 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”
  • 38.
  • 39.
    SPECIFYING THE OCCURRENCEOFASEQUENCE minOccurs and maxOccurs attributes can also be used with compositors to repeat entire sequences of items
  • 40.
    The Scope ofan 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 DATATYPES • 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 DATATYPES • 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.
  • 45.
  • 46.
    STRING DATA TYPES thestring data types that we will use are: string , ID
  • 47.
  • 48.
  • 49.
  • 50.
    DATES AND TIMES thedate and time data types we will use are: date , time
  • 51.
  • 52.
  • 53.
    APPLYINGASCHEMATOAN XMLDOCUMENT ● Toattach 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"
  • 54.
  • 55.
    SUMMARY: SCHEMA ELEMENTSAND 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