Вы находитесь на странице: 1из 36

Extensible Markup Language

Objectives

In this session, you will learn to:


Create an XML schema Declare attributes in an XML schema Identify the need for XML namespaces

Ver. 1.0

Session 2

Slide 1 of 36

Extensible Markup Language


Introducing XML Schema

An XML schema defines the list of elements and attributes that can be used in an XML document. An XML schema specifies the order in which the elements appear in the XML document, and their data types. Microsoft has developed the XML Schema Definition (XSD) language to define the schema of an XML document.

Ver. 1.0

Session 2

Slide 2 of 36

Extensible Markup Language


Advantages of XML Schema Created Using XSDs

Some of the advantages of creating an XML schema by using XSD are:


XSD provides control over the type of data that can be assigned to elements and attributes. XSD enables you to create your own data types. XSD enables you to specify restrictions on data. The syntax for defining an XSD is the same as the syntax used for XML documents. XML schema content models can be used to validate mixed content. XML schema is extensible. XML schema is self documenting.

Ver. 1.0

Session 2

Slide 3 of 36

Extensible Markup Language


Support for XML Schemas in Various Parsers

Parsers that provide support for XML schemas are:


IBM XML4J: Validates XML documents against several types of XML schemas. MSXML 6.0: Enables loading of XML data from anonymous or untrusted sources in a secured manner.

Ver. 1.0

Session 2

Slide 4 of 36

Extensible Markup Language


Data Types in XML Schemas

In an XML schema created using XSD, every element must be associated with a data type. XSD provides the following list of predefined data types:
Primitive Derived Atomic List Union

Ver. 1.0

Session 2

Slide 5 of 36

Extensible Markup Language


Data Types in XML Schemas (Contd.)

In an XML schema created using XSD, every element must be associated with a data type. XSD provides the following list of predefined data types:
Primitive Derived Atomic List Union
Do not contain elements or attributes. Contain only values.

Ver. 1.0

Session 2

Slide 6 of 36

Extensible Markup Language


Data Types in XML Schemas (Contd.)

In an XML schema created using XSD, every element must be associated with a data type. XSD provides the following list of predefined data types:
Primitive Derived Atomic List Union
Are defined by using other data types called base types. Can be built-in or user-defined.

Ver. 1.0

Session 2

Slide 7 of 36

Extensible Markup Language


Data Types in XML Schemas (Contd.)

In an XML schema created using XSD, every element must be associated with a data type. XSD provides the following list of predefined data types:
Primitive Derived Atomic List Union

Cannot be broken down into smaller units. Can be primitive or derived.

Ver. 1.0

Session 2

Slide 8 of 36

Extensible Markup Language


Data Types in XML Schemas (Contd.)

In an XML schema created using XSD, every element must be associated with a data type. XSD provides the following list of predefined data types:
Primitive Derived Atomic List Union

Are derived data types that contain a set of values of atomic data types. Elements referring to a list data type can contain a value only from that defined set.

Ver. 1.0

Session 2

Slide 9 of 36

Extensible Markup Language


Data Types in XML Schemas (Contd.)

In an XML schema created using XSD, every element must be associated with a data type. XSD provides the following list of predefined data types:
Primitive Derived Atomic List Union

Are derived from the atomic and list data types.

Ver. 1.0

Session 2

Slide 10 of 36

Extensible Markup Language


Elements in XML Schemas

There are two types of elements, simple and complex that can be defined in a schema. Simple Element
A simple element does not contain any child elements or attributes. It contains only values, such as numbers, strings, and dates. You can specify restrictions on elements by defining a new simple data type from an existing data type using facet values. You can also associate an element with a simple data type.

Let us look at the syntax for declaring a simple element.

Ver. 1.0

Session 2

Slide 11 of 36

Extensible Markup Language


Elements in XML Schemas (Contd.)
<xsd:element name= element-name" type="data type" min0ccurs="nonNegativeInteger" max0ccurs="nonNegativeInteger| unbounded"/>
The name attribute specifies the name of the element declared. The type attribute specifies the data type of the element declared. minOccurs specifies the minimum number of times the element can occur. maxOccurs specifies the maximum number of times the element can appear.

Ver. 1.0

Session 2

Slide 12 of 36

Extensible Markup Language


Elements in XML Schemas (Contd.)

Complex Element
A complex element contains other elements, attributes, and mixed content. To declare a complex element, you need to first define a complex data type. After defining the complex data type, you can declare a complex element by associating this data type with the element.

Let us look at the syntax for declaring a complex element.

Ver. 1.0

Session 2

Slide 13 of 36

Extensible Markup Language


Elements in XML Schemas (Contd.)
<xsd:complexType name="data type name"> Content model declaration </xsd:complexType>

The complexType element is used to declare a new complex data type. The name attribute specifies the name of the new complex data type. The Content model declaration contains the declaration for the elements and attributes that make up the content of the complex type.

Ver. 1.0

Session 2

Slide 14 of 36

Extensible Markup Language


Demo: Creating an XML Schema

Problem Statement:
CyberShoppe, a toy and book store in the United States, sends its product information from the head office to the branch offices. The product details must be stored in a consistent format. Restrictions must be placed on the type of data that can be saved in the data store, in order to ensure uniformity and consistency of information. The product details include the product name, a brief description, product price, and the available quantity on hand. The price of the product must always be greater than zero.

Ver. 1.0

Session 2

Slide 15 of 36

Extensible Markup Language


Declaring Attributes in a Schema

Attributes in an XML schema are declared in the same way as elements. Declaring attributes in an XML schema facilitates the assimilation of information for an XML document. Attribute declarations can be defined in two ways:
Simple type definitions: Facilitates local validation of the attribute information. Global attribute declarations: Enables reuse of attributes.

Ver. 1.0

Session 2

Slide 16 of 36

Extensible Markup Language


Attribute Element

In XSD, an attribute for a user-defined element is declared using the attribute element.

The syntax for declaring an attribute in XSD is:


<attribute name="attributename" ref="attributename" type="datatypename" use="value" </attribute>

value="value">

The attribute element contains attributes that are used to further qualify and restrict the scope and usage of the user-defined attribute.

Ver. 1.0

Session 2

Slide 17 of 36

Extensible Markup Language


Attribute Element (Contd.) The attribute element consists of the following attributes:
name ref type use

Ver. 1.0

Session 2

Slide 18 of 36

Extensible Markup Language


Attribute Element (Contd.) The attribute element consists of the following attributes:
name ref type use
Is used to specify the name of a user-defined attribute.
Must be used when the schema element is the parent element of the attribute element. Colon (:) should not be included in the value of the name attribute.

Ver. 1.0

Session 2

Slide 19 of 36

Extensible Markup Language


Attribute Element (Contd.) The attribute element consists of the following attributes:
name ref type use
Is used to refer to a user-defined attribute declared either in the same or in any other XSD document.

Ver. 1.0

Session 2

Slide 20 of 36

Extensible Markup Language


Attribute Element (Contd.) The attribute element consists of the following attributes:
name ref type use

Takes a value that specifies the data type of the user-defined attribute.

Ver. 1.0

Session 2

Slide 21 of 36

Extensible Markup Language


Attribute Element (Contd.) The attribute element consists of the following attributes:
name ref type use

Specifies the way in which an attribute can be used in an XML document. Values that can be assigned to the use attribute are optional, default, required, and fixed.

Ver. 1.0

Session 2

Slide 22 of 36

Extensible Markup Language


Global Attributes

Global attributes are declared outside all element declarations. Global attributes facilitate attribute reusability. Global attributes can be associated with simple and complex data types. Global attributes have the schema element as the parent element.

Ver. 1.0

Session 2

Slide 23 of 36

Extensible Markup Language


Restricting Attributes Values

In order to restrict values that can be assigned to an attribute:


Declare the attribute and associate it with a user-defined simple data type. Create a simple data type by using the XSD simpleType element. Use the XSD restriction element within the simpleType element to restrict the values that can be assigned to the elements or attributes that use the simple data type.

Ver. 1.0

Session 2

Slide 24 of 36

Extensible Markup Language


Demo: Declaring Attributes in an XML Schema

Problem Statement:
The Marketing Manager at CyberShoppe sends its product information from its head office to the branch offices. The branch offices update this file and send it back to the head office on a routine basis. The product details must be stored in a consistent format at all branches. Restrictions must be placed on the type of data that can be saved in the data store, to ensure uniformity and consistency of information. CyberShoppe sells two categories of products, books and toys. Product details include the product name, a brief description, product price, and the available quantity on hand. The product price must always be greater than zero. In addition to these details, the data store needs to store the category and product ID.

Ver. 1.0

Session 2

Slide 25 of 36

Extensible Markup Language


Introducing XML Namespaces

In XML, a namespace is a virtual space that is assigned or recognized by a Uniform Resource Identifier (URI). A namespace is a string that uniquely identifies the elements and attributes from different schemas. A namespace is a unique identifier used to resolve conflicts between elements that have the same names. The following guidelines ensure the uniqueness of a URI:
Using a URI that is controlled by the developer. Using a relative URI.

Ver. 1.0

Session 2

Slide 26 of 36

Extensible Markup Language


Declaring Namespaces

A namespace can be declared in an XSD document by using the xmlns keyword. The general form of the xmlns keyword is:
xmlns:prefix="URI"
xmlns is the name of the attribute. prefix is an optional namespace.

There are two types of namespace declarations:


Default Declaration: Declares a default namespace for a document without specifying the prefix for a default namespace. Explicit Declaration: Enables xmlns keyword to associate a prefix with a namespace.

Ver. 1.0

Session 2

Slide 27 of 36

Extensible Markup Language


Practice Questions

Harry is creating an XML schema using XSD. To associate each element, he is using a data type that specifies the type of content that an element can hold. Harry wants to use such data types that are defined using base data types. Base data types can either be primitive or derived data types. Which of the following data types should Harry use to accomplish this task?
a. b. c. d. Primitive Union List Derived

Answer:
d. Derived

Ver. 1.0

Session 2

Slide 28 of 36

Extensible Markup Language


Practice Questions

Which one of the following code snippets can be considered well-formed?


a. <EMPLOYEE empid=e001> <EMPNAME> Alice Peterson</EMPNAME> <BASICPAY> $2000 </BASICPAY> </EMPLOYEE> b. <EMPLOYEE empid=e001> <EMPNAME> Alice Peterson<BASICPAY> $2000 </EMPNAME> </BASICPAY> </EMPLOYEE> c. <EMPLOYEE empid=e001> <EMPNAME> Alice Peterson<BASICPAY> $2000 </BASICPAY></EMPNAME> </EMPLOYEE>

Ver. 1.0

Session 2

Slide 29 of 36

Extensible Markup Language


Practice Questions (Contd.)
d. <EMPLOYEE empid=e001> <EMPNAME> Alice Peterson<BASICPAY> $2000 </BASICPAY></EMPNAME> </employee>

Answer:
c. <EMPLOYEE empid=e001> <EMPNAME> Alice Peterson<BASICPAY> $2000 </BASICPAY></EMPNAME> </EMPLOYEE>
Ver. 1.0

Session 2

Slide 30 of 36

Extensible Markup Language


Practice Questions

You have been assigned the task of developing an XML schema file for a new Web application in an organization. The application aims to deliver financial news to its subscribers. Financial news comprises the date, the name of the organization, and the pertinent financial information. What should you use to represent the financial news in the XML schema?
a. b. c. d. Complex type element Simple type element Element Attribute

Answer:
a. Complex type element

Ver. 1.0

Session 2

Slide 31 of 36

Extensible Markup Language


Practice Questions

Joe wants to specify a user-defined attribute in an XML schema. He wants to use the use attribute in the XML schema, but does not want to set the value for the attribute in his XML document. Which of the following values of the use attribute should Joe use in his XML schema?
a. b. c. d. optional default required fixed

Answer:
a. optional

Ver. 1.0

Session 2

Slide 32 of 36

Extensible Markup Language


Practice Questions

You have created a global attribute named color. Which of the following attributes of the xsd:attribute element will you use to access color?
a. b. c. d. use name ref type

Answer:
c. ref

Ver. 1.0

Session 2

Slide 33 of 36

Extensible Markup Language


Summary

In this session, you learned that:


An XML schema can be used to specify the list of elements and the order in which these elements must appear in the XML document. The XSD language is used to describe the structure of the elements in a schema. The data types supported by an XML schema can be categorized as follows:
Primitive Derived Atomic List

The simpleType XSD element allows you to create user-defined simple data types. The complexType XSD element allows you to create complex data types.
Ver. 1.0

Session 2

Slide 34 of 36

Extensible Markup Language


Summary (Contd.)
The restriction element can be used to specify constraints on the values that can be stored in elements and attributes. The attribute element is used to declare an attribute in an XSD document. The attribute element has the following attributes:
name: Specifies the name of the user-defined attribute. ref: Contains a reference to a global attribute. use: Specifies whether the use of the user-defined attribute is mandatory or optional. In addition, it allows you to specify the default value for an attribute. type: Specifies the data type of the attribute. value: Specifies the default or fixed value for a user-defined attribute.

The use attribute of the attribute element can take optional, default, fixed, or required as its value.

Ver. 1.0

Session 2

Slide 35 of 36

Extensible Markup Language


Summary (Contd.)

A global attribute is used to declare an attribute that is not associated with any element and can be reused within a schema. A namespace is used to avoid naming conflicts between elements having the same name. A namespace is declared using the xmlns keyword.

Ver. 1.0

Session 2

Slide 36 of 36

Вам также может понравиться