XML - Lesson 3
XML - Lesson 3
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
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.
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.
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.
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.
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.
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]”.
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.
✓ 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.
Summary (Contd.)
✓ 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.
☛ 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.
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.