xml - lesson 3

Upload: mathes99994840202

Post on 30-May-2018

215 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/14/2019 XML - Lesson 3

    1/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

  • 8/14/2019 XML - Lesson 3

    2/36

    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 informationabout 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 thatcan be saved in the data store to ensure uniformityand consistency of information.

    CyberShoppe sells two categories of products, booksand toys. The product details comprise the name ofthe product, a brief description about it, the price ofthe product, and the quantity available in stock. Theprice of the product must always be greater than zero.

    In addition to these details, you also need to store thecategory and product ID.

  • 8/14/2019 XML - Lesson 3

    3/36

    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.

  • 8/14/2019 XML - Lesson 3

    4/36

    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

  • 8/14/2019 XML - Lesson 3

    5/36

    Working with XML Schemas

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

    Task 1: Identify theXML document. (Contd.)

    The required attributes are:

    PRODID

    CATEGORY

  • 8/14/2019 XML - Lesson 3

    6/36

    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:

  • 8/14/2019 XML - Lesson 3

    7/36

    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 thatare 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 theparent element of the attribute element.

  • 8/14/2019 XML - Lesson 3

    8/36

    Working with XML Schemas

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

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

    The attribute Element (Contd.)

    The ref Attribute Is used to reference a user-defined attribute

    declared in the same or any other XSD

    document.

    The type Attribute

    Takes a value, which specifies the data type of

    the user-defined attribute.

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

  • 8/14/2019 XML - Lesson 3

    9/36

    Working with XML Schemas

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

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

    The attribute Element (Contd.)

    The use Attribute

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

    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.

    default: Specifies a default value for an

    attribute. You must use the value attribute ofthe attribute element if you set the valueof the use attribute to default.

  • 8/14/2019 XML - Lesson 3

    10/36

  • 8/14/2019 XML - Lesson 3

    11/36

    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 declaredoutside all element declarations. For such

    attributes, the schema element is the parent

    element.

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

    Global attributes can be associated with both

    simple and complex data types.

  • 8/14/2019 XML - Lesson 3

    12/36

    Working with XML Schemas

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

    Task 2: Identify theXML schema. (Contd.)

    Result

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

  • 8/14/2019 XML - Lesson 3

    13/36

    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 anattribute, 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 orattributes that use the simple data type.

  • 8/14/2019 XML - Lesson 3

    14/36

    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.

  • 8/14/2019 XML - Lesson 3

    15/36

    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 coverfor 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.

  • 8/14/2019 XML - Lesson 3

    16/36

    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 isassigned or recognized by a Uniform ResourceIdentifier (URI).

    A URI is a string that uniquely identifies theelements 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 theInternet. It is just a unique identifier used to resolveconflicts between elements having same names.

  • 8/14/2019 XML - Lesson 3

    17/36

    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.

  • 8/14/2019 XML - Lesson 3

    18/36

    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 schemaelement, which is declared at the beginning of thedocument.

    The general form of the xmlns keyword is asfollows:

    xmlns:prefix=URI

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

  • 8/14/2019 XML - Lesson 3

    19/36

  • 8/14/2019 XML - Lesson 3

    20/36

    Working with XML Schemas

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

    XML Namespaces (Contd.)

    Explicit Declaration

    Inan explicit declaration, the xmlns keyword associates a

    prefix with a namespace URI, as shown in the following

    example:

    :

    Only those elements and attributes that explicitly use this

    prefix come under the scope of the specified namespace.

  • 8/14/2019 XML - Lesson 3

    21/36

    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 forreporting 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 officeneeds 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.

  • 8/14/2019 XML - Lesson 3

    22/36

  • 8/14/2019 XML - Lesson 3

    23/36

    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 forstoring data.

    Create an XML document.

    Validate the XML document against the schema.

  • 8/14/2019 XML - Lesson 3

    24/36

  • 8/14/2019 XML - Lesson 3

    25/36

    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 aboutpurchase orders are:

    ORDERIDPRODID

  • 8/14/2019 XML - Lesson 3

    26/36

  • 8/14/2019 XML - Lesson 3

    27/36

    Working with XML Schemas

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

    Task 2: Identify a mechanismdocuments.

    (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 ofthe same target namespace.

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

    The target namespace for a schema is declaredusing the targetNamespace attribute of theschema element.

  • 8/14/2019 XML - Lesson 3

    28/36

    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:

  • 8/14/2019 XML - Lesson 3

    29/36

  • 8/14/2019 XML - Lesson 3

    30/36

    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 datatypes required for storing product ID and order IDin an XML schema.

    You can specify the restrictions on these data

    types. Then, you can use the include orimport

    elements to reference these data types in anotherschema that contains the declarations for elements

    and attributes required for storing purchase orderdata.

  • 8/14/2019 XML - Lesson 3

    31/36

    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 tobe used for storing data.

    Task 5: Create an XML document.

    Task 6: Validate the XML document against the

    schema.

  • 8/14/2019 XML - Lesson 3

    32/36

  • 8/14/2019 XML - Lesson 3

    33/36

    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 priceper 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].

  • 8/14/2019 XML - Lesson 3

    34/36

    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.

    use: Specifies whether the use of the user-defined

    attribute is mandatory or optional. Also allows youto specify the default value for an attribute.

  • 8/14/2019 XML - Lesson 3

    35/36

    Working with XML Schemas

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

    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 isnot 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.

    W ki i h XML S h

  • 8/14/2019 XML - Lesson 3

    36/36

    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 ofthe containing document.

    The import element is used to reference a schema

    in which the target namespace is different from that of

    the containing schema.