Post on 16-Aug-2015
© People Strategists www.peoplestrategists.com Slide 1 of 64
XML Schemas
© People Strategists www.peoplestrategists.com Slide 2 of 64
Objectives
In this session, you will learn to:
Explain the concept of XML schema
Identify the advantages of XML schema over DTDs
Define XML schema elements
Create simple and complex type elements
Use of compositors with XML schema elements
Apply restrictions on simple type elements
Extend complex type elements
Creating reusable types and schemas
Explain the concept of SAX
Explain the concept of DOM
© People Strategists www.peoplestrategists.com Slide 3 of 64
Introduction to XML Schema
Let us first understand what is XML schema.
How can I get rid of large DTDs that are hard to read and maintain?
© People Strategists www.peoplestrategists.com Slide 4 of 64
Introduction to XML Schema (Contd.)
XML schema, similar to DTD (Document Type Definition), describes and validates the structure of an XML document. It is an XML-based alternative of DTD and referred as XML Schema Definition (XSD).
XML schemas are written in XML itself.
© People Strategists www.peoplestrategists.com Slide 5 of 64
Introduction to XML Schema (Contd.)
XML schema is used to:
Describe elements and attributes that appear in an XML document. Describe the root and child elements.
Describe the order and number of child elements.
Describe the data type for elements and attributes.Describe the default and fixed values for elements and attributes.
© People Strategists www.peoplestrategists.com Slide 6 of 64
Introduction to XML Schema (Contd.)
Let us create an XML schema file, employee.xsd, for an XML file, employee.xml, and add the reference of schema file in the XML file.
<?xml version="1.0" encoding="utf-8"?><employee xmlns="mynamespace"xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance xsi:schemaLocation="mynamespace employee.xsd"> <firstname>John</firstname> <lastname>Smith</lastname></employee>
<?xml version="1.0" encoding="utf-8"?><xs:schema targetNamespace="mynamespace" elementFormDefault="qualified" xmlns="mynamespace employee.xsd" xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xs:element name="employee"> <xs:complexType> <xs:sequence> <xs:element name="firstname" type="xs:string"/> <xs:element name="lastname" type="xs:string"/> </xs:sequence> </xs:complexType> </xs:element></xs:schema>
employee.xsd employee.xml
© People Strategists www.peoplestrategists.com Slide 7 of 64
Introduction to XML Schema (Contd.)
The <schema> element is the root element of every XML Schema and contains some attributes.
The targetNamespace attribute defines the namespace of the XML file that has to be validated.
The elementFormDefault attribute defines whether its local elements must be qualified or unqualified in an XML document.
The xmlns attribute specifies the default namespace.
The attribute xmlns:xs specifies the namespace of the elements and datatypes used in the schema.
<?xml version="1.0" encoding="utf-8"?><xs:schema targetNamespace="<target_namespace>" elementFormDefault="<qualified/unqualified>" xmlns="<default_namespace>" xmlns:xs="http://www.w3.org/2001/XMLSchema"> .................
.................</xs:schema>
© People Strategists www.peoplestrategists.com Slide 8 of 64
Introduction to XML Schema (Contd.)
You can add reference of an XML schema in an XML file by using attributes, as shown in the following code snippet:
The xmlns attribute defines the namespace of the elements declared in the XML file.
The xmlns:xsi attribute specifies the instance of the schema that is used for the XML document.
The xsi:schemaLocation attribute specifies the location of the schema.
<?xml version="1.0"?><emp xmlns="default_namespace"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="<namespace> <schema_file>"............................</emp>
© People Strategists www.peoplestrategists.com Slide 9 of 64
Advantages Over DTD
What are the advantages of using XML schema over
DTD?
© People Strategists www.peoplestrategists.com Slide 10 of 64
Advantages Over DTD (Contd.)
XML schemas follow a universal standard that made data communication over the Internet safe.
For example, a date like: "01-07-2015" can be interpreted as 7 January in some countries, whereas in other countries as 1 July. However, an XML element accepts date in the fixed format "YYYY-MM-DD" to ensure its correct interpretation.
XML schemas support data types that allow programmers to:
Specify the acceptable content in the document
Ensure the validity of data
Work with databases
Apply restrictions on data
Specify data formats
Convert data from one data type to another
© People Strategists www.peoplestrategists.com Slide 11 of 64
Advantages Over DTD (Contd.)
You can define number and order of child elements using XML schemas, but not with DTDs.
XML schemas support namespaces, whereas DTDs do not support namespaces.
XML schemas can be also extended, therefore, you can:
Reuse an XML schema in other schemas.
Drive your own data types from the standard types.
Add the reference of multiple schemas in a single XML document.
© People Strategists www.peoplestrategists.com Slide 12 of 64
Defining Elements
How can I define an element in an XML
schema?
© People Strategists www.peoplestrategists.com Slide 13 of 64
Defining Elements (Contd.)
XML schemas define the elements of XML files.
You can use the XML syntax to define elements in an XML schema file.
To define an element, you can use the following syntax:
The following code snippet depicts some examples of defining elements:
<xs:element name="<element_name>" type="data_name"/>
<xs:element name="firstname" type="xs:string"/>
<xs:element name="lastname" type="xs:string"/>
<xs:element name="dob" type="xs:date"/>
<xs:element name= "age" type="xs:numeric"/>
© People Strategists www.peoplestrategists.com Slide 14 of 64
Simple and Complex Types
You can define two types of elements in XML schemas. These are:
Element Types
Simple Complex
© People Strategists www.peoplestrategists.com Slide 15 of 64
Simple and Complex Types (Contd.)
Simple Type:
Is an XML element that contains only text.
Cannot contain other elements or attributes.
To define a simple type element, you can use the following syntax:
Some common built-in data types that XML schema supports are:
xs:string
xs:decimal
xs:integer
xs:boolean
xs:date
xs:time
<xs:element name="<element_name>" type="data_name"/>
© People Strategists www.peoplestrategists.com Slide 16 of 64
Simple and Complex Types (Contd.)
You can also specify default or fixed value for a simple element.
The default value is implicitly assigned to the element if a specific value is not provided.
The following code snippet shows an example of specifying default value for an element:
If you specify a fixed value for an element, you cannot provide another value for that element in your XML document.
The specified value is automatically assigned to the element.
<xs:element name="city" type="xs:string" default="New York"/>
<xs:element name="city" type="xs:string" fixed="New York"/>
© People Strategists www.peoplestrategists.com Slide 17 of 64
XSD Attributes:
Are used to provide additional information about an element.
Are defined with name and type properties in an XSD document.
The syntax of defining an attribute is shown as follows:
The following code snippet shows an example of defining an attribute:
Simple and Complex Types (Contd.)
<xs:attribute name="<attribute_name>" type="<data_type>" use="<type_of_use>"/>
<xs:attribute name="ID" type="xs:string" use="required"/>
© People Strategists www.peoplestrategists.com Slide 18 of 64
Simple and Complex Types (Contd.)
Complex Type:
Elements include other elements and/or attributes.
For example, to define a complex type, person name, with two other elements, first name and last name, you can use the following code snippet:
<xs:element name="person_name">
<xs:complexType>
<xs:sequence>
<xs:element name="firstname" type="xs:string"/>
<xs:element name="lastname" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
© People Strategists www.peoplestrategists.com Slide 19 of 64
Simple and Complex Types (Contd.)
You can also create a complex type and reuse it in several other complex element, as shown in the following code snippet:
<xs:element name="student" type="person_name"/>
<xs:element name="employee" type="person_name"/>
<xs:complexType name="person_name">
<xs:sequence>
<xs:element name="firstname" type="xs:string"/>
<xs:element name="lastname" type="xs:string"/>
</xs:sequence>
</xs:complexType>
© People Strategists www.peoplestrategists.com Slide 20 of 64
Simple and Complex Types (Contd.)
Complex type elements are of the following types:
Complex Type
Empty Elements
Elements Only
Text Only
Mixed
© People Strategists www.peoplestrategists.com Slide 21 of 64
Simple and Complex Types (Contd.)
Empty Elements:
Can only have attributes, but not text.
For example, you want to define a complex type empty element, employee, with an attribute, emp id as an integer value, you can use the following code snippet:
<xs:element name="employee">
<xs:complexType>
<xs:attribute name="empid" type="xs:positiveInteger"/>
</xs:complexType>
</xs:element>
© People Strategists www.peoplestrategists.com Slide 22 of 64
Simple and Complex Types (Contd.)
Elements Only:
Can contain other elements only.
For example, you want to define a complex type, employee name, with two other elements, first name and last name, you can use the following code snippet:
In the preceding code snippet, the <xs:sequence> tag specifies that the first name and last name elements must appear in the given order inside the employee_name element.
<xs:element name="employee_name">
<xs:complexType><xs:sequence>
<xs:element name="firstname" type="xs:string"/>
<xs:element name="lastname" type="xs:string"/>
</xs:sequence></xs:complexType>
</xs:element>
© People Strategists www.peoplestrategists.com Slide 23 of 64
Simple and Complex Types (Contd.)
Text Only:
Can only contain text and attributes.
The simpleContent element must be defined to specify the content for the element.
An extension or a restriction element must be used within the simpleContent element to expand or to limit the content of the element.
<xs:element name="contact_number">
<xs:complexType><xs:simpleContent>
<xs:extension base="xs:integer">
<xs:attribute name="country" type="xs:string" />
</xs:extension>
</xs:simpleContent></xs:complexType>
</xs:element>
© People Strategists www.peoplestrategists.com Slide 24 of 64
Simple and Complex Types (Contd.)
Mixed:
Can contain attributes, elements, and text.
is defined by setting the mixed attribute to "true" inside the complexType element.
<xs:element name="order">
<xs:complexType mixed="true">
<xs:sequence>
<xs:element name="pname" type="xs:string"/>
<xs:element name="orderid" type="xs:positiveInteger"/>
<xs:element name="orderdate" type="xs:date"/>
</xs:sequence>
</xs:complexType>
</xs:element>
© People Strategists www.peoplestrategists.com Slide 25 of 64
Use of Compositors
What are compositors?
© People Strategists www.peoplestrategists.com Slide 26 of 64
Use of Compositors (Contd.)
Compositors are one of the widely used XSD standards that specify the XML document structure.
You can use compositors to describe the composition of child elements within a parent element in an XML document.
They are generally used with complex type element.
They are used to control the use of elements in an XML document.
When compositors are used appropriately, they offer comprehensive and robust data models.
© People Strategists www.peoplestrategists.com Slide 27 of 64
Use of Compositors (Contd.)
There are seven major compositors in XML, which are divided into three categories. Compositors
Order
All
Choice
Sequence
Occurrence
Min Occurs
Max Occurs
Group
Element
Attribute
© People Strategists www.peoplestrategists.com Slide 28 of 64
Use of Compositors (Contd.)
Order Compositors:
Are used to used to define the order of the elements.
The <all> compositor specifies that the child elements of an element can appear in any order, and each child element must appear only once.
<xs:element name="person_name">
<xs:complexType>
<xs:all>
<xs:element name="firstname“ type="xs:string"/>
<xs:element name="lastname" type="xs:string"/>
</xs:all>
</xs:complexType>
</xs:element>
© People Strategists www.peoplestrategists.com Slide 29 of 64
Use of Compositors (Contd.)
You can use the <choice> compositor to specify that either one child element or another can occur.
<xs:element name="person_name">
<xs:complexType>
<xs:choice>
<xs:element name="firstname" type="xs:string"/>
<xs:element name="lastname" type="xs:string"/>
</xs:choice>
</xs:complexType>
</xs:element>
© People Strategists www.peoplestrategists.com Slide 30 of 64
Use of Compositors (Contd.)
The <sequence> compositor is used to specify that the child elements must appear in the given order.
<xs:element name="person_name">
<xs:complexType>
<xs:sequence>
<xs:element name="firstname" type="xs:string"/>
<xs:element name="lastname" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
© People Strategists www.peoplestrategists.com Slide 31 of 64
Use of Compositors (Contd.)
Occurrence Compositors:
Specify the maximum or minimum number of occurrences of an element in an document.
You can use the using the <maxOccurs> compositor to specify the maximum number of occurrences of an element.
<xs:element name="person">
<xs:complexType>
<xs:sequence>
<xs:element name="full_name" type="xs:string"/>
<xs:element name="contact_number" type="xs:integer" maxOccurs="3"/>
</xs:sequence>
</xs:complexType>
</xs:element>
© People Strategists www.peoplestrategists.com Slide 32 of 64
Use of Compositors (Contd.)
You can specify the minimum number of occurrences of an element in an document using the <minOccurs> compositor.
You can also specify that an element can occur any number of times by specifying maxOccurs="unbounded" .
<xs:element name="person">
<xs:complexType>
<xs:sequence>
<xs:element name="full_name" type="xs:string"/>
<xs:element name="contact_number" type="xs:integer" maxOccurs="3" minOccurs="0"/>
</xs:sequence>
</xs:complexType>
</xs:element>
© People Strategists www.peoplestrategists.com Slide 33 of 64
Use of Compositors (Contd.)
Group Compositors:
Are use to specify the related sets of elements.
You can define an element group and refer it in another XML schema, as shown in the following code snippet.
<xs:group name="empgroup"> <xs:sequence>
<xs:element name="firstname" type="xs:string"/>
<xs:element name="lastname" type="xs:string"/>
<xs:element name="doj" type="xs:date"/>
</xs:sequence></xs:group>
<xs:element name="emp" type="empinfo"/>
<xs:complexTypename="empinfo">
<xs:sequence>
<xs:group ref="empgroup"/>
<xs:element name="city" type="xs:string"/>
</xs:sequence></xs:complexType>
Group Definition Group Reference
© People Strategists www.peoplestrategists.com Slide 34 of 64
Use of Compositors (Contd.)
You can define an attribute group and refer it in another XML schema, as shown in the following code snippet.
<xs:attributeGroup name="empattrgroup">
<xs:attribute name="firstname" type="xs:string"/>
<xs:attribute name="lastname" type="xs:string"/>
<xs:attribute name="doj" type="xs:date"/>
</xs:attributeGroup>
<xs:element name="employee">
<xs:complexType>
<xs:attributeGroup ref="empattrgroup"/>
</xs:complexType>
</xs:element>
Group Definition Group Reference
© People Strategists www.peoplestrategists.com Slide 35 of 64
Restriction on Simple Types
Let us learn about the restrictions that you can apply on simple types.
© People Strategists www.peoplestrategists.com Slide 36 of 64
Restriction on Simple Types (Contd.)
You can apply restrictions on simple element types to specify the acceptable values of XML elements or attributes.
In XML, restrictions on XML elements are known as facets.Types of Restrictions
Restrictions on Values
Restrictions on a Set of Values
Restrictions on a Series of Values
Restrictions on Whitespace Characters
Restrictions on Length
Restrictions for Data types
© People Strategists www.peoplestrategists.com Slide 37 of 64
Restriction on Simple Types (Contd.)
Restrictions on Values:
You can apply restriction on the value of an element. For example, the age of an employee cannot be less than 21 and greater 60.
<xs:element name="emp_age">
<xs:simpleType>
<xs:restriction base="xs:integer">
<xs:minInclusive value="21"/>
<xs:maxInclusive value="60"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
© People Strategists www.peoplestrategists.com Slide 38 of 64
Restriction on Simple Types (Contd.)
Restrictions on a Set of Values:
You can apply restriction on an XML element such that the element can only accept a value from a set of specified values.
For example, you can specify a job role element that can only accept one of the values: Sales, Management, and Operations.
<xs:element name="job_role">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:enumeration value="Sales"/>
<xs:enumeration value="Management"/>
<xs:enumeration value="Operations"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
© People Strategists www.peoplestrategists.com Slide 39 of 64
Restriction on Simple Types (Contd.)
Restrictions on a Series of Values:
You can apply restrictions on a series of numbers or letters that can be used by using the pattern constraint.
For example, you define the contact element that can accept only 10 digit values such that the first digit should not be 0, and other digits must be from 0 to 9.
<xs:element name="contact">
<xs:simpleType>
<xs:restriction base="xs:integer">
<xs:pattern value="[1-9][0-9][0-9][0-9][0-9][0-9][0 9][0-9][0-9][0-9]"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
© People Strategists www.peoplestrategists.com Slide 40 of 64
Restriction on Simple Types (Contd.)
Similarly, you can also apply restrictions on character values.
For example, you can define an element, lcase that can only accept a lowercase letter from a-z.
<xs:element name="lcase">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="[a-z]"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
© People Strategists www.peoplestrategists.com Slide 41 of 64
Restriction on Simple Types (Contd.)
For example, you define an element password that can accept exactly 10 characters in a row and those characters can be lowercase or uppercase letters from a to z or a number from 0 to 9:
<xs:element name="password">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="[a-zA-Z0-9]{8}"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
© People Strategists www.peoplestrategists.com Slide 42 of 64
Restriction on Simple Types (Contd.)
Restrictions on Whitespace Characters:
You can specify how whitespaces are treated for an element by using the whiteSpace constraint.
The whiteSpace constraint may accept one of the following values:
preserve: Leaves all the whitespace characters as they are. replace: Replaces all the whitespace characters with spaces. Collapse: Replaces line feeds, tabs, spaces, carriage returns are with
spaces and multiple spaces with a single space and removes leading and trailing spaces.
© People Strategists www.peoplestrategists.com Slide 43 of 64
Restriction on Simple Types (Contd.)
For example, if you want to define a full_name element such that the whitespaces are not removed by the XML processor, you can use the following code snippet:
<xs:element name="full_name">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:whiteSpace value="preserve"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
© People Strategists www.peoplestrategists.com Slide 44 of 64
Restriction on Simple Types (Contd.)
Restrictions on Length:
You can limit the length of the value of an element by using the maxLength and minLength constraints.
For example, you want to define an element, password, that have minimum 6 letters and maximum 14 letters, you can use the following code snippet:
<xs:element name="password">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:minLength value="6"/>
<xs:maxLength value="14"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
© People Strategists www.peoplestrategists.com Slide 45 of 64
Restriction on Simple Types (Contd.)
Restrictions on Data Types:
Some of the common restrictions that you can apply on data types are listed in the following table.
Constraints Description
enumeration Specifies a set of acceptable values.
fractionDigits Specifies the maximum number of decimal digits, must be equal to or greater than 0.
length Specifies the length of the value of an element.
maxExclusive Specifies the upper bound of a numeric value that must be less than the specified value.
maxInclusive Specifies the upper bound of a numeric value that must be less than or equal to the specified value.
maxLength Specifies the maximum number of allowed characters.
minExclusive Specifies the lower bounds of a numeric value that must be greater than the specified value.
© People Strategists www.peoplestrategists.com Slide 46 of 64
Restriction on Simple Types (Contd.)
Constraints Description
minInclusive Specifies the lower bounds of a numeric value that must be greater than or equal to the specified value.
minLength Specifies the minimum number of allowed characters.
pattern Specifies a sequence of characters that are allowed.
totalDigits Specifies the number of allowed digits.
whiteSpace Specifies how to treat whitespaces.
© People Strategists www.peoplestrategists.com Slide 47 of 64
Extending Complex Types
You can take an existing entity and add more specific information to it as per your requirement.
In programming languages, this concept is known as inheritance or sub classing.
Inheritance or sub classing saves development time and effort.
The same concept is also applied in XSD standard.
You can take an existing complex type element and extend it to add more information.
© People Strategists www.peoplestrategists.com Slide 48 of 64
Extending Complex Types (Contd.)
Consider that you have a complex element personal_info defined as follows:
<xs:element name="personal_info">
<xs:complexType>
<xs:sequence>
<xs:element name="first_name" type="xs:string"/>
<xs:element name="last_name" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
© People Strategists www.peoplestrategists.com Slide 49 of 64
Extending Complex Types (Contd.)
Now, you want to define two more elements, home_address and office_address.
The addresses also contain the first name and last name of the person.
Instead of defining the first name and last name elements inside the home_address and office_address elements, you can extend the personal_info element to reuse the existing information by using the <xs:extension> element.
You can define home_address by extending personal_info, as:
You can also create customized functions in your program to enhance readability and efficiency.
To use functions effectively, you need to know: How to declare a user-defined function. How to pass arguments. How to return a value. How to call a user-defined function from the main program.
For example:
<xs:complexType name="home_address"> <xs:complexContent> <xs:extension base="personal_info">
<xs:sequence> <xs:element name="line1" type="xs:string"/> <xs:element name="line2" type="xs:string"/>
</xs:sequence> </xs:extension> </xs:complexContent> </xs:complexType>
home_address Element
© People Strategists www.peoplestrategists.com Slide 50 of 64
Extending Complex Types (Contd.)
Similarly, you can define the office_address element as:
<xs:complexType name="home_address"> <xs:complexContent> <xs:extension base="personal_info">
<xs:sequence> <xs:element name="line1" type="xs:string"/> <xs:element name="line2" type="xs:string"/>
</xs:sequence> </xs:extension> </xs:complexContent> </xs:complexType>
office_address Element
© People Strategists www.peoplestrategists.com Slide 51 of 64
Extending Complex Types (Contd.)
If you want to use only some specific elements from a complex type element, you can use the <xs:restriction> element.
For example, you want to create a new element first_name, you can restrict the personal_info type by using the <xs:restriction> element, as:
<xs:complexType name="first_name"> <xs:complexContent> <xs:restriction base="personal_info"> <xs:sequence> <xs:element name="first_name" type="xs:string" /> </xs:sequence> </xs:restriction> </xs:complexContent></xs:complexType>
first_name Element
© People Strategists www.peoplestrategists.com Slide 52 of 64
Creating Reusable Types
Can I create an XML schema and reuse
it in other XML schemas?
© People Strategists www.peoplestrategists.com Slide 53 of 64
Creating Reusable Types (Contd.)
If you want to create some elements that can be reused on multiple files in your project, you should define these elements in a separate XML schema, and refer it in other schemas whenever required.
You can create an XML schema and reuse it in other schemas with the help of the <xs:import> element.
For example, suppose, you have an XML schema, info.xsd that defines the first name and last name of a person.
Now, you want to create a new schema, empinfo.xsd that also contains the first name and last name of the person.
In this case, you can provide the reference of the info.xsd schema in the empinfo.xsd and reuse the elements defined in the info.xsd schema.
© People Strategists www.peoplestrategists.com Slide 54 of 64
Creating Reusable Types (Contd.)
The following code snippet shows the XML markup of info.xsd schema:
You can reuse this schema in another schema with the help of <xs:import> element.
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://example.org/person" targetNamespace="http://example.org/person"> <xsd:complexType name="personal_info"> <xsd:sequence> <xsd:element name="first_name" type="xsd:string"/> <xsd:element name="last_name" type="xsd:string"/> </xsd:sequence> </xsd:complexType></xsd:schema>
info.xsd
© People Strategists www.peoplestrategists.com Slide 55 of 64
Creating Reusable Types (Contd.)
The following code snippet shows the XML markup of empinfo.xsd schema:
<xs:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:basetns="http://example.org/person" xmlns:tns="http://example.org/person/derived" targetNamespace="http://example.org/person/derived"> <xs:import namespace="http://example.org/person" schemaLocation="info.xsd"/> <xs:complexType name="empInfo"> <xs:complexContent> <xs:extension base="basetns:personal_info"> <xs:sequence>
<xs:element name="contact_num" type="xs:numeric"/> <xs:element name="doj" type="xs:date"/>
<xs:sequence> </xs:extension> </xs:complexContent> </xs:complexType></xs:schema>
empinfo.xsd
© People Strategists www.peoplestrategists.com Slide 56 of 64
Identifying SAX
SAX stands for Simple API for XML.
It is an Application Programming Interface (API) to interpret XML-based Web files or the Web files that use XML.
The API interprets the XML markup and describe the collection of data.
SAX provides an alternative way to DOM for interpreting the XML file.
It is a simpler interface in comparison to DOM and does not require in-memory resource for processing.
© People Strategists www.peoplestrategists.com Slide 57 of 64
SAX (Contd.)
It is used where large number of files need to be processed.
You can use SAX to search small pieces of information in a large document and abort processing when the information is found.
SAX is also capable to build DOM trees.
Developers can also traverse DOM trees to create SAX streams as well.
© People Strategists www.peoplestrategists.com Slide 58 of 64
DOM
The XML DOM is used to represent an XML document as a tree like structure.
The DOM tree is called as the node-tree.
The DOM tree represents a hierarchical relationship between the nodes of the tree.
The tree starts with the root node branches out to the text node to the lowest level of the tree.
Each node of the tree can be accessed from the tree.
The content of the tree can be modified or deleted.
A new element can be also inserted in a DOM tree.
© People Strategists www.peoplestrategists.com Slide 59 of 64
DOM (Contd.)
The parent, children, and sibling terms are used to specify relationships between nodes in a DOM tree.
A parent node have children.
Nodes that are at the same level are known as siblings.
The top node is known as the root node.
Each DOM tree has only one root node.
Each node except root node has a parent node.
A parent node can have any number of children.
A node that has no children is known as leaf node.
Nodes that have the same parent are known as siblings.
© People Strategists www.peoplestrategists.com Slide 60 of 64
Text: ASP.NET
Text:Benson
Text:Kelly Bond
Text: XML
Root Node
Sibling Nodes
Data
Parent-Child Relationship
DOM (Contd.)
A sample XML DOM tree is shown as follows. Book
Book 1
Author
© People Strategists www.peoplestrategists.com Slide 61 of 64
Summary
XML schema, similar to DTDs, describes and validates the structure of an XML document.
XML schemas are written in XML itself.
The targetNamespace attribute defines the namespace of the XML file that has to be validated.
The attribute xmlns:xs specifies the namespace of the elements and datatypes used in the schema.
XML schemas support data types that allow users to:
Specify the acceptable content in the document
Ensure the validity of data
Work with databases
Apply restrictions on data
Specify data formats
Convert data from one data type to another
© People Strategists www.peoplestrategists.com Slide 62 of 64
Summary (Contd.)
XML schemas can be also extended, therefore, you can:
Reuse an XML schema in other schemas.
Drive your own data types from the standard types.
Add the reference of multiple schemas in a single XML document.
You can define two types of elements in XML schemas. These are:
Simple Type
Complex Type
You can use XSDs to specify the structure of markup in an XML document at different levels.
Compositors are one of the widely used XSD standards that specify the XML document structure.
When compositors are used appropriately, they offer comprehensive and robust data models.
© People Strategists www.peoplestrategists.com Slide 63 of 64
Summary (Contd.)
In XML, restrictions on XML elements are known as facets. Types of restrictions are:
Restrictions on Values
Restrictions on a Set of Values
Restrictions on a Series of Values
Restrictions on Whitespace Characters
Restrictions on Length
Restrictions for Datatypes
You can create an XML schema and reuse it in other schemas with the help of the <xs:import> element.
SAX stands for Simple API for XML.
SAX provides an alternative way to DOM for interpreting the XML file.
© People Strategists www.peoplestrategists.com Slide 64 of 64
Summary (Contd.)
The XML DOM is used to represent an XML document as a tree like structure.
The DOM tree represents a hierarchical relationship between the nodes of the tree.