8/14/2019 XML - Lesson 2
1/49
Ensuring Consistency of Data in XML
Documents
NIIT eXtensible Markup Language/Lesson 2/Slide 1 of 49
Objectives
In this lesson, you will learn to:
Declare elements and attributes in a Document TypeDefinition (DTD)
Create an XML Schema
8/14/2019 XML - Lesson 2
2/49
Ensuring Consistency of Data in XML
Documents
NIIT eXtensible Markup Language/Lesson 2/Slide 2 of 49
Problem Statement 2.D.1
The head office of CyberShoppe sends the information
about its products to the branch offices. The product
details must be stored in a consistent format at allbranches. Restrictions must be placed on the kind of
data that can be saved in the data store to ensure
uniformity and consistency of information.
The products sold by CyberShoppe are organized intotwo categories, toys and books. The product details
comprise the name of the product, a brief description
about it, the price of the product, and the quantity
available in stock. Every product is uniquely identified
by a product ID.
8/14/2019 XML - Lesson 2
3/49
Ensuring Consistency of Data in XML
Documents
NIIT eXtensible Markup Language/Lesson 2/Slide 3 of 49
Task List
Identify the elements required for storing structureddata.
Identify the attributes.
Identify the method for storing consistent data.
Identify the method for declaring elements to be usedfor storing structured data.
Identify the method for declaring attributes.
Identify the method to validate the structure of data.
8/14/2019 XML - Lesson 2
4/49
Ensuring Consistency of Data in XML
Documents
NIIT eXtensible Markup Language/Lesson 2/Slide 4 of 49
Task List (Contd.)
Declare elements and attributes.
Store data.
Validate the structure of data.
8/14/2019 XML - Lesson 2
5/49
Ensuring Consistency of Data in XML
Documents
NIIT eXtensible Markup Language/Lesson 2/Slide 5 of 49
Task 1: Identify the elements required for storing
structured data.
Result:
The elements required to store the details about
products sold at CyberShoppe are as follows:
Represents the description of each product.DESCRIPTION
Represents the quantity of each product.QUANTITY
Represents the price of each product.PRICE
Represents the name of each product.PRODUCTNAME
Represents the details (product name, description, price, and quantity) for each
product.
PRODUCT
Indicates that data specific to various products is being stored in the document.Acts as the root element for all other elements.
PRODUCTDATA
DescriptionElement
8/14/2019 XML - Lesson 2
6/49
Ensuring Consistency of Data in XML
Documents
NIIT eXtensible Markup Language/Lesson 2/Slide 6 of 49
Task 2: Identify the attributes.
Result:
In case of CyberShoppe, you need to store all detailsabout products in an XML document.
Each product needs to have a unique identification
number for easy identification of a particular product.
Therefore, PRODUCTID can be defined as anattribute of the PRODUCT element.
The category classifies a product as Book or Toy.
Therefore, CATEGORY can also be defined as an
attribute of the PRODUCT element.
8/14/2019 XML - Lesson 2
7/49
Ensuring Consistency of Data in XML
Documents
NIIT eXtensible Markup Language/Lesson 2/Slide 7 of 49
Task 2: Identify the attributes. (Contd.)
The following table specifies the attributes to be
used in the XML document that stores product
details:
Represents the category of a product, and specifies whether a
product is a TOY or BOOK.
CATEGORY
Represents a unique identification value for each product. It must
be specified for every product.
PRODUCTID
DescriptionAttribute
8/14/2019 XML - Lesson 2
8/49
Ensuring Consistency of Data in XML
Documents
NIIT eXtensible Markup Language/Lesson 2/Slide 8 of 49
Task 3: Identify the method for storingconsistent data.
Document Type Definition
A DTD defines the structure of the content of an XMLdocument, thereby allowing you to store data in aconsistent format.
XML allows you to create your own DTDs forapplications.
You can check an XML document against a DTD.
This checking process is called validation.
XML documents that conform to a DTD areconsidered valid documents.
8/14/2019 XML - Lesson 2
9/49
Ensuring Consistency of Data in XML
Documents
NIIT eXtensible Markup Language/Lesson 2/Slide 9 of 49
Task 3: Identify the method for (Contd.)
Result:
As a DTD allows you to specify the structure and typeof data elements, a DTD can be created to specify the
structure of the document.
8/14/2019 XML - Lesson 2
10/49
Ensuring Consistency of Data in XML
Documents
NIIT eXtensible Markup Language/Lesson 2/Slide 10 of 49
Task 4: Identify the method for declaring
elements to be used for storing structured data.
In DTD, elements are declared by using the following
syntax:
content-model)>
Elements can be of following types:
Empty
Unrestricted
Container
8/14/2019 XML - Lesson 2
11/49
Ensuring Consistency of Data in XML
Documents
NIIT eXtensible Markup Language/Lesson 2/Slide 11 of 49
Task 4: Identify the method for (Contd.)
While declaring elements in a DTD, different symbols
can be used to specify whether an element is mandatory
or optional, and whether it can occur more than once.
The following table lists the various symbols that can be
used while defining the DTD:
Either PRODUCTNAME or
DESCRIPTION.
PRODUCTNAME|
DESCRIPTION
or|
PRODUCTNAME or
DESCRIPTION must occur in
that order.
PRODUCTNAME,
DESCRIPTION
and in specific
order
,DescriptionExampleMeaningSymbol
8/14/2019 XML - Lesson 2
12/49
Ensuring Consistency of Data in XML
Documents
NIIT eXtensible Markup Language/Lesson 2/Slide 12 of 49
Task 4: Identify the method for (Contd.)
DESCRIPTION can occur
multiple times.
DESCRIPTION+An element must
occur at least once.There can be
multiple
occurrences.
+
Any number of
PRODUCTNAME or
DESCRIPTION elements
can be present in any order.
(PRODUCTNAME|
DESCRIPTION)*
An element can
occur zero or
multiple times.
*
DESCRIPTION need not be
present, but if it is present, itcan occur only once.
DESCRIPTION?optional, can
occur only once.
?
DescriptionExampleMeaningSymbol
8/14/2019 XML - Lesson 2
13/49
Ensuring Consistency of Data in XML
Documents
NIIT eXtensible Markup Language/Lesson 2/Slide 13 of 49
Contains regular text that represents the price of a product.Data contentPRICE
Contains regular text that represents the quantity of a product.Data contentQUANTITY
Contains regular text that represents the description of a product.Data contentDESCRIPTION
Contains regular text that represents the name of a product.Data contentPRODUCTNAME
Contains details of other products, and hence will contain other
elements like PRODUCTNAME, DESCRIPTION, PRICE, and
QUANTITY.
Element contentPRODUCT
Contains one or more PRODUCT elements.Element contentPRODUCTDATA
DescriptionContent TypeElement
Task 4: Identify the method for (Contd.)
As per the given scenario, the type of content for each
element is given in the following table:
8/14/2019 XML - Lesson 2
14/49
Ensuring Consistency of Data in XML
Documents
NIIT eXtensible Markup Language/Lesson 2/Slide 14 of 49
Task 4: Identify the method for (Contd.)
You need to use the statement for
declaring elements in a DTD.
For example, the PRODUCTNAME element used in the
CyberShoppe scenario can be declared as follows:
8/14/2019 XML - Lesson 2
15/49
Ensuring Consistency of Data in XML
Documents
NIIT eXtensible Markup Language/Lesson 2/Slide 15 of 49
Task 5: Identify the method for declaring
attributes.
The syntax for declaring attributes in a DTD is as
follows:
valuetype [attributetype][default]>
The attributename valuetype[attributetype] [default] section is
repeated as often as necessary to create multiple
attributes for any given element.
8/14/2019 XML - Lesson 2
16/49
8/14/2019 XML - Lesson 2
17/49
Ensuring Consistency of Data in XML
Documents
NIIT eXtensible Markup Language/Lesson 2/Slide 17 of 49
Task 5: Identify the attribute types and (Contd.)
Result:
In the case of CyberShoppe, the attribute and their
value types will be as follows:
You need to use the statement for
declaring attributes in a DTD.
Category must be TOYS
or BOOKS.
(enumerated)#REQUIREDCATEGORY
Product ID must have a
unique value and has to
be specified for every
product.
ID#REQUIREDPRODUCTID
DescriptionValue TypeAttribute TypeAttribute
8/14/2019 XML - Lesson 2
18/49
Ensuring Consistency of Data in XML
Documents
NIIT eXtensible Markup Language/Lesson 2/Slide 18 of 49
Task 6: Identify the method to validate the
structure of data.
To validate the structure of data in an XML document
you need to use parsers.
Parsers are software programs that check the syntax
used in an XML file. There are two types of parsers.
They are:
Non-validating parsers: Check whether an XML
document is well-formed.
Validating parsers: Check for well-formedness and
validity of an XML document.
8/14/2019 XML - Lesson 2
19/49
Ensuring Consistency of Data in XML
Documents
NIIT eXtensible Markup Language/Lesson 2/Slide 19 of 49
Task 6: Identify the method to validate (Contd.)
Result:
In order to check whether the data sent by the branchesof CyberShoppe conforms to the structure specified in
the DTD, you need a validating parser.
8/14/2019 XML - Lesson 2
20/49
Ensuring Consistency of Data in XML
Documents
NIIT eXtensible Markup Language/Lesson 2/Slide 20 of 49
Task 7: Declare elements and attributes.
Internal and External DTDs
You can declare elements and attributes in a DTD. A DTD can be classified into two types. They are:
Internal DTD
External DTD
8/14/2019 XML - Lesson 2
21/49
Ensuring Consistency of Data in XML
Documents
NIIT eXtensible Markup Language/Lesson 2/Slide 21 of 49
Task 7: Declare elements and attributes. (Contd.)
Differences between internal and external DTDs are
given in the following table:
This DTD can be used across multiple
documents.
This DTD can be used only by the document
in which it is created and cannot be usedacross multiple documents.
This DTD is maintained as a separate file. A
reference to this file in included in the XML
document.
This DTD is a part of the XML document.
External DTDInternal DTD
8/14/2019 XML - Lesson 2
22/49
Ensuring Consistency of Data in XML
Documents
NIIT eXtensible Markup Language/Lesson 2/Slide 22 of 49
Task 7: Declare elements and attributes. (Contd.)
To ensure that the structure of an XML document
conforms to the DTD, you must associate the DTD
with the XML document.
The declaration is used to define the
internal DTD. It can also be used to reference an
external DTD.
The syntax for defining an internal DTD in an XML
document is as follows:
8/14/2019 XML - Lesson 2
23/49
Ensuring Consistency of Data in XML
Documents
NIIT eXtensible Markup Language/Lesson 2/Slide 23 of 49
Task 7: Declare elements and attributes. (Contd.)
The syntax for referencing an external DTD in the
XML document is as follows:
Action:
Type the code for creating the DTD.
Save the file as products.dtd.
8/14/2019 XML - Lesson 2
24/49
E i C i t f D t i XML
8/14/2019 XML - Lesson 2
25/49
Ensuring Consistency of Data in XML
Documents
NIIT eXtensible Markup Language/Lesson 2/Slide 25 of 49
Task 9: Validate the structure of data.
Action:
Open index.htm in Internet Explorer.
Click the DTD Validator link.
Type the name of the XML document that you want to
parse in the text box.
Click the Validate button.
E i C i t f D t i XML
8/14/2019 XML - Lesson 2
26/49
Ensuring Consistency of Data in XML
Documents
NIIT eXtensible Markup Language/Lesson 2/Slide 26 of 49
Just a Minute
The branches of CyberShoppe send information aboutbooks sold by them to the head office. The book detailsmust be stored in a consistent format. Restrictions must
be placed on kind of data that can be saved in the datastore to ensure uniformity and consistency ofinformation. The details of the books sold byCyberShoppe consist of the name of the book, ISBN ofthe book, first and last names of the author of the book,and the price of the book. The ISBN should be uniquefor each book. In addition, you need to ensure that thebook category contains HISTORY, SCIENCE, orFICTION as its valid values. Create a DTD for declaringthe elements to be used for storing book details in anXML document.
E i C i t f D t i XML
8/14/2019 XML - Lesson 2
27/49
Ensuring Consistency of Data in XML
Documents
NIIT eXtensible Markup Language/Lesson 2/Slide 27 of 49
Introduction to XML Schemas
AnXML schema is used to define the structure of an
XML document.
Microsoft has developed a language that is used to
define the schema of an XML document. This
language is called the XML Schema Definition (XSD)
language.
Ensuring Consistency of Data in XML
8/14/2019 XML - Lesson 2
28/49
Ensuring Consistency of Data in XML
Documents
NIIT eXtensible Markup Language/Lesson 2/Slide 28 of 49
Advantages of XML Schemas over DTDs
Some of the advantages of an XML schema created
by using XSD over DTD are as follows:
XSD provides more control over the type of data
that can be assigned to elements and attributes as
compared to DTD.
DTD does not enable you to define your owncustomized data types. XSD enables you to create
your own data types.
XSD also allows you to specify restrictions on data.
Ensuring Consistency of Data in XML
8/14/2019 XML - Lesson 2
29/49
Ensuring Consistency of Data in XML
Documents
NIIT eXtensible Markup Language/Lesson 2/Slide 29 of 49
Advantages of XML Schemas over DTDs (Contd.)
The syntax for defining a DTD is different from thesyntax used for creating an XML document.However, the syntax for defining an XSD is thesame as the syntax of the XML document.
Ensuring Consistency of Data in XML
8/14/2019 XML - Lesson 2
30/49
Ensuring Consistency of Data in XML
Documents
NIIT eXtensible Markup Language/Lesson 2/Slide 30 of 49
Problem Statement 2.D.2The head office of CyberShoppe sends information about
its products to its branch offices. The product details must
be stored in a consistent format. Restrictions must be
placed on the kind of data that can be saved in the data
store to ensure uniformity and consistency of information.
The product details comprise the name of the product, a
brief description about it, the price of the product, and thequantity available in stock. The price of the product must
always be greater than zero.
Ensuring Consistency of Data in XML
8/14/2019 XML - Lesson 2
31/49
Ensuring Consistency of Data in XML
Documents
NIIT eXtensible Markup Language/Lesson 2/Slide 31 of 49
Task List
Identify the elements required to store data.
Identify the data type of the contents of an element.
Identify the method for declaring a simple type
element.
Identify the method for declaring a complex type
element.
Create the XML schema.
Create an XML document conforming to the schema.
Validate an XML document against the schema.
Ensuring Consistency of Data in XML
8/14/2019 XML - Lesson 2
32/49
Ensuring Consistency of Data in XML
Documents
NIIT eXtensible Markup Language/Lesson 2/Slide 32 of 49
Task 1: Identify the elements required to store
data.
Result:
As per the problem, the elements required in the XML
document are:
Represents the quantity of each product.QUANTITY
Represents the price of each productPRICE
Represents the description of each product.DESCRIPTION
Represents the name of each product.PRODUCTNAME
Represents the details (product name, description, price, and quantity)
for each product.
PRODUCT
This element indicates that data specific to various products is being
stored in the document. Therefore, it contains more elements and acts
as the root element
PRODUCTDATA
DescriptionElement
Ensuring Consistency of Data in XML
8/14/2019 XML - Lesson 2
33/49
Ensuring Consistency of Data in XML
Documents
NIIT eXtensible Markup Language/Lesson 2/Slide 33 of 49
Task 2: Identify the data type of the contents of anelement.
Every element declared in XSD, must be associatedwith a data type.
XSD provides a list of pre-defined data types.
Primitives Data Types: Fundamental data types ofXSD, such as string, decimal, float, and boolean.
Derived Data Types: Defined by using other datatypes.
Atomic Data Types: Data types that cannot bebroken further.
List Data Types: Contain a set of values.
Union Data Types: Derived from list and atomic datatypes.
Ensuring Consistency of Data in XML
8/14/2019 XML - Lesson 2
34/49
Ensuring Consistency of Data in XML
Documents
NIIT eXtensible Markup Language/Lesson 2/Slide 34 of 49
Task 2: Identify the data type of the (Contd.)
XSD also allows definition of custom data types. These
custom data types can be classified as follows:
Simple data type: A data type that contains only
values.
Complex data type: A data type that contains child
elements, attributes, and also the mixed content.
Ensuring Consistency of Data in XML
8/14/2019 XML - Lesson 2
35/49
Ensuring Consistency of Data in XML
Documents
NIIT eXtensible Markup Language/Lesson 2/Slide 35 of 49
Task 2: Identify the data type of the (Contd.)
Result:
The data type for the contents of the elements will be:
A simple type element that contains values of string data type.StringDESCRIPTION
A simple type element that contains values of positiveInteger
data type (product price must be greater than zero.
PositiveintegerPRICE
A simple type element that contains values of integer data type.IntegerQUANTITY
A simple type element that contains values of data string type.StringPRODUCTNAME
A complex type element that can hold other elements, attributes,
and mixed content. This element will hold a complex data type,
which will be defined in the later session.
Complex data typePRODUCT
A complex type element that can hold other elements, attributes,
and mixed content. This element will hold a complex data type,
which will be defined in the later session.
Complex data typePRODUCTDATA
DescriptionData TypeElement
Ensuring Consistency of Data in XML
8/14/2019 XML - Lesson 2
36/49
Ensuring Consistency of Data in XML
Documents
NIIT eXtensible Markup Language/Lesson 2/Slide 36 of 49
Task 3: Identify the method for declaring a
simple type element.
A simple element does not contain any child elements
or attributes. Simple elements contain only valuessuch as numbers, strings, and dates.
The syntax for declaring elements with a simple data
type is as follows:
Ensuring Consistency of Data in XML
8/14/2019 XML - Lesson 2
37/49
g y
Documents
NIIT eXtensible Markup Language/Lesson 2/Slide 37 of 49
Task 3: Identify the method for declaring a
simple type element. (Contd.)
You can associate an element with a user-defined
simple data type. To do so, you must define the newsimple data type.
You can use the simpleType element of XSD to create
a user-defined simple data type.
Ensuring Consistency of Data in XML
8/14/2019 XML - Lesson 2
38/49
g y
Documents
NIIT eXtensible Markup Language/Lesson 2/Slide 38 of 49
Task 3: Identify the method for declaring asimple type element. (Contd.)
Result:
As per the problem, the simple elements can bedeclared in the XSD as follows:
Ensuring Consistency of Data in XML
8/14/2019 XML - Lesson 2
39/49
g y
Documents
NIIT eXtensible Markup Language/Lesson 2/Slide 39 of 49
Task 4: Identify the method for declaring a
complex type element.
A complex type element is one that contains othermarkup elements, attributes, and mixed content.
To declare a complex type element, you need to firstdefine a complex data type. After you define acomplex data type, you can declare a complexelement by associating this data type with theelement.
You can define a complex data type by using thesyntax given below:
Content model declaration
Ensuring Consistency of Data in XML
8/14/2019 XML - Lesson 2
40/49
g y
Documents
NIIT eXtensible Markup Language/Lesson 2/Slide 40 of 49
Task 4: Identify the method for declaring a
complex type element. (Contd.)
To declare an element as a complex type element, the
element must be associated with a complex data type.
For example, to declare the element PRODUCT as a
complex type element you can associate this element
with the prdt data type as shown below:
Result
In the CyberShoppe scenario, you require two complex
type elements, PRODUCTDATA and PRODUCT.
Ensuring Consistency of Data in XML
8/14/2019 XML - Lesson 2
41/49
Documents
NIIT eXtensible Markup Language/Lesson 2/Slide 41 of 49
Task 4: Identify the method for declaring a
complex type element. (Contd.)
You can create complex type elements by associating
them with complex data types.
You can use the element element of XSD to declare a
complex type element.
You can use the complexType element of XSD tocreate the complex data type.
Ensuring Consistency of Data in XML
8/14/2019 XML - Lesson 2
42/49
Documents
NIIT eXtensible Markup Language/Lesson 2/Slide 42 of 49
Task 5: Create the XML Schema.
The Schema element
The integration of the various components of the
XSD is done using the schema element.
The declaration of an XML schema starts with the
element.
The element uses the xmlns attribute tospecify the namespace associated with the
document.
Action:
Type the XML Schema in Notepad.
Save the file as product.xsd.
Ensuring Consistency of Data in XML
8/14/2019 XML - Lesson 2
43/49
Documents
NIIT eXtensible Markup Language/Lesson 2/Slide 43 of 49
Task 6: Create an XML document conforming to
the schema.
To create a data structure that conforms to the XML
schema, you should create an XML document andassociate it with the XML schema.
An XML file cannot be directly associated with the
XML schema file. The XML file can be associated with
the XML schema only through a validator.
Action:
Type the code in Notepad.
Save the file as products.xml
Ensuring Consistency of Data in XML
8/14/2019 XML - Lesson 2
44/49
Documents
NIIT eXtensible Markup Language/Lesson 2/Slide 44 of 49
Task 7: Validate an XML document against the
schema.
Action:
Open index.htm.
Click the Schema Validator link.
Type the name of the XML document and the XSD
file.
Click the Validate button.
Ensuring Consistency of Data in XML
8/14/2019 XML - Lesson 2
45/49
Documents
NIIT eXtensible Markup Language/Lesson 2/Slide 45 of 49
Problem Statement 2.P.2The details of the books sold by CyberShoppe consist
of the name of the book, the ISBN of the book, the first
and last names of the author of the book, and the
price of the book. The ISBN must start with the letter Iand be followed by three digits.
This data must be validated to ensure that it conforms
to the standards specified in order to maintain dataintegrity. Also, the data types used for the data must
be compatible with those used in databases. All data
must be stored in a consistent format.
Ensuring Consistency of Data in XML
D t
8/14/2019 XML - Lesson 2
46/49
Documents
NIIT eXtensible Markup Language/Lesson 2/Slide 46 of 49
Summary
In this lesson, you learned that:
Document type Definition (DTD) is method for defining
the structure of the data in an XML document.
There are two types of DTD:
Internal DTD: It can be included as a part of the
document.
External DTD: it is stored as a separate file having
the declaration of all elements and attributes that
can be used in an XML document.
There are three types of elements: empty,unrestricted, and container.
Ensuring Consistency of Data in XML
D t
8/14/2019 XML - Lesson 2
47/49
Documents
NIIT eXtensible Markup Language/Lesson 2/Slide 47 of 49
Summary (Contd.) The statement is used to declare an
element in a DTD.
The statement is used to declare a list ofattributes for an element in a DTD.
The
8/14/2019 XML - Lesson 2
48/49
Documents
NIIT eXtensible Markup Language/Lesson 2/Slide 48 of 49
Summary (Contd.) Schema can be used to specify the list of elements
and the order in which these elements must appear inthe XML document.
The language that is used to describe the structure ofthe elements in a schema is called the XML SchemaDefinition (XSD) language .
The data types supported by schema are of the
following types: Primitive
Derived
Atomic
List
Ensuring Consistency of Data in XML
Doc ments
8/14/2019 XML - Lesson 2
49/49
Documents
NIIT eXtensible Markup Language/Lesson 2/Slide 49 of 49
Summary (Contd.) The simpleType element of XSD allows you to create
user-defined simple data types.
The complexType element of XSD allows you tocreate complex data types.
The restriction element can be used to specifyconstraints on values that can be stored in elementsand attributes.