36
Working with XML Schemas ©NIIT eXtensible Markup Language/Lesson 3/Slide 1 of 36 Objectives In this lesson, you will learn to: Declare attributes in an XML schema Identify the need for XML namespaces Use the components of one XML schema in another

Working with XML Schemas ©NIITeXtensible Markup Language/Lesson 3/Slide 1 of 36 Objectives In this lesson, you will learn to: * Declare attributes in an

Embed Size (px)

Citation preview

Page 1: Working with XML Schemas ©NIITeXtensible Markup Language/Lesson 3/Slide 1 of 36 Objectives In this lesson, you will learn to: * Declare attributes in an

Working with XML Schemas

©NIIT eXtensible Markup Language/Lesson 3/Slide 1 of 36

Objectives

In this lesson, you will learn to:

Declare attributes in an XML schema

Identify the need for XML namespaces

Use the components of one XML schema in another

Page 2: Working with XML Schemas ©NIITeXtensible Markup Language/Lesson 3/Slide 1 of 36 Objectives In this lesson, you will learn to: * Declare attributes in an

Working with XML Schemas

©NIIT eXtensible Markup Language/Lesson 3/Slide 2 of 36

Problem Statement 3.D.1 The 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.

CyberShoppe sells two categories of products, books and toys. 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. The price of the product must always be greater than zero. In addition to these details, you also need to store the category and product ID.

Page 3: Working with XML Schemas ©NIITeXtensible Markup Language/Lesson 3/Slide 1 of 36 Objectives In this lesson, you will learn to: * Declare attributes in an

Working with XML Schemas

©NIIT eXtensible Markup Language/Lesson 3/Slide 3 of 36

Task List

Identify the elements and attributes required in the XML document.

Identify the method for declaring an attribute in an XML schema.

Identify the mechanism to restrict the values that can be assigned to an attribute.

Declare elements and attributes.

Create an XML document.

Validate the XML document against the schema.

Page 4: Working with XML Schemas ©NIITeXtensible Markup Language/Lesson 3/Slide 1 of 36 Objectives In this lesson, you will learn to: * Declare attributes in an

Working with XML Schemas

©NIIT eXtensible Markup Language/Lesson 3/Slide 4 of 36

Task 1: Identify the elements and attributes required in the XML document.

Result

The required elements are:

PRODUCTDATA

PRODUCT

PRODUCTNAME

DESCRIPTION

PRICE

QUANTITY

Page 5: Working with XML Schemas ©NIITeXtensible Markup Language/Lesson 3/Slide 1 of 36 Objectives In this lesson, you will learn to: * Declare attributes in an

Working with XML Schemas

©NIIT eXtensible Markup Language/Lesson 3/Slide 5 of 36

Task 1: Identify the…XML document. (Contd.)

The required attributes are:

PRODID

CATEGORY

Page 6: Working with XML Schemas ©NIITeXtensible Markup Language/Lesson 3/Slide 1 of 36 Objectives In this lesson, you will learn to: * Declare attributes in an

Working with XML Schemas

©NIIT eXtensible Markup Language/Lesson 3/Slide 6 of 36

Task 2: Identify the method for declaring an attribute in an XML schema.

The 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 given below:

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

Page 7: Working with XML Schemas ©NIITeXtensible Markup Language/Lesson 3/Slide 1 of 36 Objectives In this lesson, you will learn to: * Declare attributes in an

Working with XML Schemas

©NIIT eXtensible Markup Language/Lesson 3/Slide 7 of 36

Task 2: Identify the …XML schema. (Contd.)

The attribute Element (Contd.)

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

The name Attribute

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.

Page 8: Working with XML Schemas ©NIITeXtensible Markup Language/Lesson 3/Slide 1 of 36 Objectives In this lesson, you will learn to: * Declare attributes in an

Working with XML Schemas

©NIIT eXtensible Markup Language/Lesson 3/Slide 8 of 36

Task 2: Identify the …XML schema. (Contd.)

The default Attribute

Is used to specify a default value for an attribute.

The fixed Attribute

Is used to specify a fixed value for an attribute

The ref Attribute

Is used to reference a user-defined attribute declared in the same or any other XSD document.

Page 9: Working with XML Schemas ©NIITeXtensible Markup Language/Lesson 3/Slide 1 of 36 Objectives In this lesson, you will learn to: * Declare attributes in an

Working with XML Schemas

©NIIT eXtensible Markup Language/Lesson 3/Slide 9 of 36

Task 2: Identify the …XML schema. (Contd.)

The type Attribute

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

Attributes can be associated with both built-in and user-defined simple data types.

The use Attribute

The use attribute specifies the way in which an attribute must be used in an XML document.

Page 10: Working with XML Schemas ©NIITeXtensible Markup Language/Lesson 3/Slide 1 of 36 Objectives In this lesson, you will learn to: * Declare attributes in an

Working with XML Schemas

©NIIT eXtensible Markup Language/Lesson 3/Slide 10 of 36

Task 2: Identify the …XML schema. (Contd.)

The values that can be assigned to the use attribute are as follows:

optional: Specifies that the use of the attribute in the XML document is optional.

required: Specifies that the use of the attribute is mandatory.

Page 11: Working with XML Schemas ©NIITeXtensible Markup Language/Lesson 3/Slide 1 of 36 Objectives In this lesson, you will learn to: * Declare attributes in an

Working with XML Schemas

©NIIT eXtensible Markup Language/Lesson 3/Slide 11 of 36

Task 2: Identify the …XML schema. (Contd.)

Global Attributes

Global attributes are attributes that are declared outside all element declarations. For such attributes, the schema element is the parent element.

After declaring a global attribute, you can reuse this attribute from anywhere within the schema.

Global attributes can be associated with both simple and complex data types.

Page 12: Working with XML Schemas ©NIITeXtensible Markup Language/Lesson 3/Slide 1 of 36 Objectives In this lesson, you will learn to: * Declare attributes in an

Working with XML Schemas

©NIIT eXtensible Markup Language/Lesson 3/Slide 12 of 36

Task 2: Identify the…XML schema. (Contd.)

Result

In order to declare the category and ProdID attributes, you should use the attribute element of XSD.

Page 13: Working with XML Schemas ©NIITeXtensible Markup Language/Lesson 3/Slide 1 of 36 Objectives In this lesson, you will learn to: * Declare attributes in an

Working with XML Schemas

©NIIT eXtensible Markup Language/Lesson 3/Slide 13 of 36

Task 3: Identify the mechanism to restrict the values that can be assigned to an attribute. Result

In order to restrict values that can be assigned to an attribute, you need to declare the attribute and associate it with a user-defined simple data type.

Next, you need to create the simple data type by using the simpleType element of XSD.

Finally, you need to use the restriction element of XSD within the simpleType element in order to restrict the values that can be assigned to the elements or attributes that use the simple data type.

Page 14: Working with XML Schemas ©NIITeXtensible Markup Language/Lesson 3/Slide 1 of 36 Objectives In this lesson, you will learn to: * Declare attributes in an

Working with XML Schemas

©NIIT eXtensible Markup Language/Lesson 3/Slide 14 of 36

Task 4: Declare elements and attributes.

Task 5: Create an XML document.

Task 6: Validate the XML document against the schema.

Page 15: Working with XML Schemas ©NIITeXtensible Markup Language/Lesson 3/Slide 1 of 36 Objectives In this lesson, you will learn to: * Declare attributes in an

Working with XML Schemas

©NIIT eXtensible Markup Language/Lesson 3/Slide 15 of 36

Just a Minute…

In addition to storing the name of the book, first and last names of the author, and price of the book, you also need to store the book ID and the type of cover for each book. The type of cover can be either hard or soft. Declare the elements and attributes required to store book details. The book ID can also be used in different XML documents that store products data. Ensure that the book ID attribute is reusable.

Page 16: Working with XML Schemas ©NIITeXtensible Markup Language/Lesson 3/Slide 1 of 36 Objectives In this lesson, you will learn to: * Declare attributes in an

Working with XML Schemas

©NIIT eXtensible Markup Language/Lesson 3/Slide 16 of 36

XML Namespaces

Introduction to XML Namespaces

A namespace is a method of preventing conflicts between elements having same names.

In XML, a namespace is a virtual space that is assigned or recognized by a Uniform Resource Identifier (URI).

A URI is a string that uniquely identifies the elements and attributes from different schemas.

A namespace URI is not the same as a Web URI. It does not actually point to a resource on the Internet. It is just a unique identifier used to resolve conflicts between elements having same names.

Page 17: Working with XML Schemas ©NIITeXtensible Markup Language/Lesson 3/Slide 1 of 36 Objectives In this lesson, you will learn to: * Declare attributes in an

Working with XML Schemas

©NIIT eXtensible Markup Language/Lesson 3/Slide 17 of 36

XML Namespaces (Contd.)

Introduction to XML Namespaces (Contd.)

You can assign any name or string as a URI.

XML allows you to map a prefix to a URI. You can think of a prefix as an alias for a namespace URI.

Page 18: Working with XML Schemas ©NIITeXtensible Markup Language/Lesson 3/Slide 1 of 36 Objectives In this lesson, you will learn to: * Declare attributes in an

Working with XML Schemas

©NIIT eXtensible Markup Language/Lesson 3/Slide 18 of 36

XML Namespaces (Contd.)

Declaring Namespaces

A namespace can be declared in the XSD document by using the xmlns keyword.

This keyword is an attribute of the schema element, which is declared at the beginning of the document.

The general form of the xmlns keyword is as follows:

xmlns:prefix=“URI”

There are two kinds of namespace declarations, default and explicit.

Page 19: Working with XML Schemas ©NIITeXtensible Markup Language/Lesson 3/Slide 1 of 36 Objectives In this lesson, you will learn to: * Declare attributes in an

Working with XML Schemas

©NIIT eXtensible Markup Language/Lesson 3/Slide 19 of 36

XML Namespaces (Contd.)

Default Declaration

The default declaration statement enables you to declare a default namespace for a document.

In case of a default namespace, you need not specify the prefix.

Page 20: Working with XML Schemas ©NIITeXtensible Markup Language/Lesson 3/Slide 1 of 36 Objectives In this lesson, you will learn to: * Declare attributes in an

Working with XML Schemas

©NIIT eXtensible Markup Language/Lesson 3/Slide 20 of 36

XML Namespaces (Contd.)

Explicit Declaration

In an explicit declaration, the xmlns keyword associates a prefix with a namespace URI, as shown in the following example:

<xsd:schema xmlns:xsd=“http://www.w3.org/2001/XMLSchema”>

:

</xsd:schema>

Only those elements and attributes that explicitly use this prefix come under the scope of the specified namespace.

Page 21: Working with XML Schemas ©NIITeXtensible Markup Language/Lesson 3/Slide 1 of 36 Objectives In this lesson, you will learn to: * Declare attributes in an

Working with XML Schemas

©NIIT eXtensible Markup Language/Lesson 3/Slide 21 of 36

Problem Statement 3.D.2

The branches of CyberShoppe purchase various products from their suppliers. The details about all these purchase orders are sent to the head office for reporting purposes. To ensure that the data can be accessed, regardless of the hardware and software used at the head office, the branches send data in an XML format. On receiving this data, the head office needs to verify that all branches have specified the required information. It also needs to verify that the data sent by various branches is in a consistent format.

Page 22: Working with XML Schemas ©NIITeXtensible Markup Language/Lesson 3/Slide 1 of 36 Objectives In this lesson, you will learn to: * Declare attributes in an

Working with XML Schemas

©NIIT eXtensible Markup Language/Lesson 3/Slide 22 of 36

Problem Statement 3.D.2 (Contd.)

The purchase order details sent by various branches include the product ID, order ID, date of the purchase order, name and address of the supplier, quantity ordered, and price per unit.

The product ID and the order ID are used repeatedly in a number of documents. The product ID begins with P followed by 3 digits. Similarly, the order ID begins with O followed by 3 digits. These restrictions must be specified at a centralized location in such a way that they can be applied across multiple documents.

Page 23: Working with XML Schemas ©NIITeXtensible Markup Language/Lesson 3/Slide 1 of 36 Objectives In this lesson, you will learn to: * Declare attributes in an

Working with XML Schemas

©NIIT eXtensible Markup Language/Lesson 3/Slide 23 of 36

Task List

Identify the elements and attributes required to store data.

Identify a mechanism for applying restrictions across multiple documents.

Declare reusable data types.

Declare the elements and attributes to be used for storing data.

Create an XML document.

Validate the XML document against the schema.

Page 24: Working with XML Schemas ©NIITeXtensible Markup Language/Lesson 3/Slide 1 of 36 Objectives In this lesson, you will learn to: * Declare attributes in an

Working with XML Schemas

©NIIT eXtensible Markup Language/Lesson 3/Slide 24 of 36

Task 1: Identify the elements and attributes required to store data.

Result The elements required to store data about purchase

orders are: PURCHASEORDER ORDER DATE PRODUCTNAME SUPPLIERNAME ADDRESS QUANTITY PRICEPERQTY

Page 25: Working with XML Schemas ©NIITeXtensible Markup Language/Lesson 3/Slide 1 of 36 Objectives In this lesson, you will learn to: * Declare attributes in an

Working with XML Schemas

©NIIT eXtensible Markup Language/Lesson 3/Slide 25 of 36

Task 1: Identify the …store data. (Contd.) The attributes required to store data about

purchase orders are: ORDERIDPRODID

Page 26: Working with XML Schemas ©NIITeXtensible Markup Language/Lesson 3/Slide 1 of 36 Objectives In this lesson, you will learn to: * Declare attributes in an

Working with XML Schemas

©NIIT eXtensible Markup Language/Lesson 3/Slide 26 of 36

Task 2: Identify a mechanism for applying restrictions across multiple documents.

The components declared in one schema can be reused by another schema.

This can be done either by using the include or import elements.

The include element

The syntax for using the include element is given below:

<include id="ID" schemaLocation="filename" />

Page 27: Working with XML Schemas ©NIITeXtensible Markup Language/Lesson 3/Slide 1 of 36 Objectives In this lesson, you will learn to: * Declare attributes in an

Working with XML Schemas

©NIIT eXtensible Markup Language/Lesson 3/Slide 27 of 36

Task 2: Identify a mechanism…documents. (Contd.) The include element can have multiple

occurrences in an XSD document.

The schema element is the parent element of the include element.

The include element allows reference to any external schema, which is defined in the context of the same target namespace.

A target namespace has a reference to a URI to which a schema belongs.

The target namespace for a schema is declared using the targetNamespace attribute of the schema element.

Page 28: Working with XML Schemas ©NIITeXtensible Markup Language/Lesson 3/Slide 1 of 36 Objectives In this lesson, you will learn to: * Declare attributes in an

Working with XML Schemas

©NIIT eXtensible Markup Language/Lesson 3/Slide 28 of 36

Task 2: Identify a mechanism …documents. (Contd.)

The import element

The import element performs the same function as the include element. However, the import element allows you to access components from multiple schemas that may belong to different target namespaces.

The syntax for using the import element is given below:

<import id="ID" namespace="namespace" schemaLocation="filename" />

Page 29: Working with XML Schemas ©NIITeXtensible Markup Language/Lesson 3/Slide 1 of 36 Objectives In this lesson, you will learn to: * Declare attributes in an

Working with XML Schemas

©NIIT eXtensible Markup Language/Lesson 3/Slide 29 of 36

Task 2: Identify a mechanism …documents. (Contd.)

The import element (Contd.)

The id attribute takes the ID of the element as its value. This ID must be unique for the XSD document. This attribute is optional.

The namespace attribute is used to specify a URI. It also specifies the prefix, which is used to associate an element or an attribute with a particular namespace.

The schemaLocation attribute is identical to the one used by the include element. The value of this attribute is set to the physical location of the specified schema file.

Page 30: Working with XML Schemas ©NIITeXtensible Markup Language/Lesson 3/Slide 1 of 36 Objectives In this lesson, you will learn to: * Declare attributes in an

Working with XML Schemas

©NIIT eXtensible Markup Language/Lesson 3/Slide 30 of 36

Task 2: Identify a mechanism …documents. (Contd.)

Result:

In the given scenario, you can create reusable data types required for storing product ID and order ID in an XML schema.

You can specify the restrictions on these data types.

Then, you can use the include or import elements to reference these data types in another schema that contains the declarations for elements and attributes required for storing purchase order data.

Page 31: Working with XML Schemas ©NIITeXtensible Markup Language/Lesson 3/Slide 1 of 36 Objectives In this lesson, you will learn to: * Declare attributes in an

Working with XML Schemas

©NIIT eXtensible Markup Language/Lesson 3/Slide 31 of 36

Task 3: Declare reusable data types.

Task 4: Declare the elements and attributes to be used for storing data.

Task 5: Create an XML document.

Task 6: Validate the XML document against the schema.

Page 32: Working with XML Schemas ©NIITeXtensible Markup Language/Lesson 3/Slide 1 of 36 Objectives In this lesson, you will learn to: * Declare attributes in an

Working with XML Schemas

©NIIT eXtensible Markup Language/Lesson 3/Slide 32 of 36

Just a Minute…

The branch offices of CyberShoppe send the details about the sales made by them to the head office. To ensure that the data can be accessed regardless of the hardware and software used at the head office, the branches need to store their data in an XML format. When the head office receives this data, it needs to verify that all branches have specified all required information. It also needs to verify that the data sent by various branches is in a consistent format.

Page 33: Working with XML Schemas ©NIITeXtensible Markup Language/Lesson 3/Slide 1 of 36 Objectives In this lesson, you will learn to: * Declare attributes in an

Working with XML Schemas

©NIIT eXtensible Markup Language/Lesson 3/Slide 33 of 36

Just a Minute…(Contd.)

The sales details sent by various branch offices include the product ID, transaction ID, date of transaction, customer ID, quantity ordered, and price per unit.

The product ID, the transaction ID, and the customer ID are used repeatedly in a number of documents. Create reusable data types for storing these details. Also create an XML schema that uses these data types for storing sales details. The product ID should contain the pattern “P[0-9][0-9][0-9]”.

Page 34: Working with XML Schemas ©NIITeXtensible Markup Language/Lesson 3/Slide 1 of 36 Objectives In this lesson, you will learn to: * Declare attributes in an

Working with XML Schemas

©NIIT eXtensible Markup Language/Lesson 3/Slide 34 of 36

Summary

In this lesson you learned that,

The attribute element is used to declare an attribute in the 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.

default: Specifies a default value for an attribute.

fixed: Specifies a fixed value for an attribute.

Page 35: Working with XML Schemas ©NIITeXtensible Markup Language/Lesson 3/Slide 1 of 36 Objectives In this lesson, you will learn to: * Declare attributes in an

Working with XML Schemas

©NIIT eXtensible Markup Language/Lesson 3/Slide 35 of 36

Summary (Contd.)

use: Specifies whether the use of the user-defined attribute is mandatory or optional. Also allows you to specify the default value for an attribute.

type: Specifies the data type of the attribute.

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

A global attribute is used to declare an attribute that is not associated with any element and that can be reused within a schema.

A namespace is used to avoid naming conflicts between elements having same names.

Page 36: Working with XML Schemas ©NIITeXtensible Markup Language/Lesson 3/Slide 1 of 36 Objectives In this lesson, you will learn to: * Declare attributes in an

Working with XML Schemas

©NIIT eXtensible Markup Language/Lesson 3/Slide 36 of 36

Summary (Contd.)

The namespace is declared using the xmlns keyword.

You can reference a schema from another schema by using the include element or the import element.

The include element is used to reference a schema in which the target namespace is the same as that of the containing document.

The import element is used to reference a schema in which the target namespace is different from that of the containing schema.