Вы находитесь на странице: 1из 5
Activity: Grouping Elements and Attributes in an XML Schema
Activity: Grouping Elements and Attributes
in an XML Schema

Problem Statement

The customer details of CyberShoppe need to be stored in a central repository. To enable this, the computerized data needs to be collated from the branch offices and maintained at a central location. This data has to be made available to various sections, such as the Accounts and the Sales sections of various branches, irrespective of the hardware and software platforms used. After collating the customer data, the head office needs to verify that the complete information has been made available and is stored in a consistent format.

Customer data includes the customer ID, first name, last name, and contact information, such as the address and phone number. A customer may provide residential or official contact information.

Solution

To solve the preceding problem, perform the following tasks:

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

2. Declare a group of elements and attributes in a schema.

3. Create an XML document to store the data.

4. Validate the XML document against the schema.

Task 1: Identifying the Elements and Attributes Required to Store the Data

The following table lists the elements required to store the customer details in an XML document.

Element

Description

CUSTOMERDATA

Acts as the root element for all other elements that are used in the XML document.

CUSTOMER

Represents the customer details, such as the first name, last name, and contact information for each customer.

FIRSTNAME

Represents the first name of a customer.

LASTNAME

Represents the last name of a customer.

LASTNAME Represents the last name of a customer. NIIT Reusing Schema Components and Creating Grouped Elements

NIIT

Reusing Schema Components and Creating Grouped Elements 3.17

Element

Description

CONTACTINFO

Represents either the residential or the official contact information.

RESIDENCE

Represents the residential contact information for a customer. This element will be used as a child element of CONTACTINFO.

OFFICE

Represents the official contact information for a customer. This element will be used as a child element of CONTACTINFO.

ADDRESS

Represents the address of either the residence or the office of a customer. This element will be used as the child element of the

RESIDENCE and OFFICE elements.

PHONE

Represents the phone number of the residence or the office for a customer. This element will be used as a child element of

CONTACTINFO.

Elements to Store Customer Details

The following table list the attribute required to store the customer details in an XML document.

Attribute

Description

CUSTOMERID

Represents the customer ID.

Attribute to Store Customer Details

3.18 Reusing Schema Components and Creating Grouped Elements

the customer ID. Attribute to Store Customer Details 3.18 Reusing Schema Components and Creating Grouped Elements

NIIT

The elements and the attributes required to store the customer data can be depicted as shown in the following figure.

data can be depicted as shown in the following figure. Structure of the XML Document Used

Structure of the XML Document Used to Store the Customer Data

Task 2: Declaring a Group of Elements and Attributes in a Schema

To create groups of elements and attributes in the given scenario, type the following code in Notepad, and save the file as customer.xsd:

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <xsd:element name="CUSTOMERDATA" type="cdata"/> <!-- Declaration for the "cdata" complex type --> <xsd:complexType name="cdata"> <xsd:sequence>

name="cdata"> <xsd:sequence> NIIT Reusing Schema Components and Creating Grouped Elements

NIIT

Reusing Schema Components and Creating Grouped Elements 3.19

<xsd:element name="CUSTOMER" type="custtype"/> </xsd:sequence> </xsd:complexType> <!-- Declaration for the "custtype" complex type --> <xsd:complexType name="custtype"> <xsd:sequence> <xsd:element name="FIRSTNAME" type="xsd:string"/> <xsd:element name="LASTNAME" type="xsd:string"/> <xsd:element name="CONTACTINFO" type="contacttype" /> </xsd:sequence> <xsd:attribute name="CUSTOMERID" type="xsd:string" use="required"/> </xsd:complexType> <!-- Declaration for the "contacttype" complex type --> <xsd:complexType name="contacttype"> <xsd:choice> <xsd:element name="RESIDENCE" type="addtype" /> <xsd:element name="OFFICE" type="addtype" /> </xsd:choice> </xsd:complexType> <!-- Declaration for the "addtype" complex type --> <xsd:complexType name="addtype"> <xsd:group ref="ADDPHONE" /> </xsd:complexType> <!-- Declaration for the "ADDPHONE" element group --> <xsd:group name="ADDPHONE"> <xsd:sequence> <xsd:element name="ADDRESS" type="xsd:string"/> <xsd:element name="PHONE" type="xsd:string"/> </xsd:sequence> </xsd:group> </xsd:schema>

Task 3: Creating an XML Document to Store the Data

To store the data about CyberShoppe customers, type the following code in Notepad, and save the file as customer.xml:

<?xml version="1.0"?> <CUSTOMERDATA> <CUSTOMER CUSTOMERID="C001"> <FIRSTNAME> Steve </FIRSTNAME> <LASTNAME> Shaw </LASTNAME> <CONTACTINFO> <RESIDENCE> <ADDRESS> 15, LIONS STREET, BOSTON </ADDRESS>

3.20 Reusing Schema Components and Creating Grouped Elements

15, LIONS STREET, BOSTON </ADDRESS> 3.20 Reusing Schema Components and Creating Grouped Elements NIIT

NIIT

<PHONE> 172-693-1146 </PHONE> </RESIDENCE> </CONTACTINFO> </CUSTOMER> </CUSTOMERDATA>

Task 4: Validating the XML Document Against the Schema

To verify that the customer data stored in customer.xml conforms to the schema defined in customer.xsd, perform the following steps:

1. Open Index.htm from the Learning Environment folder.

2. Click the Schema Validator link. This will open the XML

Schema Validator

Module form.

3. In the XML - Schema Validator Module form, type the path for customer.xml in the Enter the XML File Name text box and type the path for customer.xsd in the Enter the XSD File Name text box.

4. Click the Validate button. If there are errors in the XML document, an appropriate error message is displayed. Otherwise, the message, No Error, is displayed.

displayed. Otherwise, the message, No Error , is displayed. NIIT Reusing Schema Components and Creating Grouped

NIIT

Reusing Schema Components and Creating Grouped Elements 3.21