1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Creating Group Elements and Reusing Schema Components

Discussion in 'JavaScript and AJAX' started by MinalS, Sep 5, 2014.

  1. MinalS

    MinalS New Member

    Joined:
    Jul 8, 2014
    Messages:
    138
    Likes Received:
    30
    Trophy Points:
    0

    The include element



    The element is used to refer to an external schema located in a definite address. The syntax for using the element is as shown below:

    Code:
    <include id="ID" schemaLocation="filename" />
    
    The element contains two attributes as id and schemaLocation. The id specifies the element ID. It is the optional parameter. The schemaLocation is used to specify the location of the schema file.

    The include element is useful only when both the containing and contained schema files are present in the same target namespace.

    The reference to the URI is declared in the target namespace. User can use different namespace for different schemas. The element allows the user to refer to any external schema that is defined in the same target namespace. The targetNamespace attribute is used to declare it.

    Consider the example for demonstrating the use of the include element.

    Code:
    <schema xmlns="http://www.w3.org/2001/XMLSchema" targetNamespace="www.shoppingvilla.com/shop">
        <simpleType name="pstring">
        <restriction base="string">
        <pattern value="[P]{1}\d{3}" />
        </restriction>
        </simpleType>
    </schema>
    
    In the above example, the psrting element of simple data type is created. The pattern should be matched with the element or attribute present in the pstring. The use of the pstring element in other schemas is as shown below:

    Code:
    <schema xmlns="http://www.w3.org/2001/XMLSchema" targetNamespace="www.shoppingvilla.com/shop"
     xmlns: prd="www.shoppingvilla/shop" >
        <include schemaLocation="shop.xsd" />
            ...
            ...
        <element name="custID" type="prd:string" />
    </schema>
    
    In the above code, the default namespace is defined. The target namespace is defined in the attribute. Both the namespaces are in the same schemas. The prefix prd is used as an alias to refer to the namespace URI.

    The import element



    The element allows user to access the components from multiple schemas that belong to different namespaces. The functionality is same as the include element.

    The syntax for the import element is as shown below:

    Code:
    <import id="ID" namespace="namespace" schemaLocation="filename" />
    
    The import element contains three attributes as follows:
    1. The id attribute takes the ID of the element as its value. The value must be unique. It is optional attribute in the declaration.
    2. The namespace attribute specifies the namespace URI to which the schema belongs.
    3. The schemaLocation attribute is used to set the physical location of the schema file.

    Creating Grouped Elements and Attributes



    The schema is the basic building block for the XML document. The XML document derived from the XML schema contains the elements, attributes, child elements, order of the child elements, number of child elements, data types for the elements, state of the element.

    The similar elements and attributes are combined into a group. Some of the functions that can be performed using the group elements are as follows:
    1. User can create the elements group and specify the sequence in which the element should appear in the XML document.
    2. Access to the single element from the created group is possible.
    3. The reusable group can be used to the model for the complex type of element or attribute.
    The schema provides the elements to group user defined elements and attributes.
    1. sequence element
    2. group element
    3. choice element
    4. all element
    5. attributegroup element
    1. sequence element

    The element is used to check the specific order of the elements. The elements must be present in the opening and closing tags.
    Consider the following example to demonstrate the use of the element.

    The Schema file contains the following code.

    Code:
    <xs: schema xmlns=:xs="http://www.w3.org/2001/XMLSchema" >
        <xs: element name="Student" >
            <xs: complexType>
            <xs: sequence>
                <xs: element name="FirstName" type="xs: string" />
                <xs: element name="LastName" type=:"xs:string" />
                <xs: element name="Class" type="xs:string" />
            </xs: sequence>
            </xs: complexType>
        </xs: element>
    </xs: schema>
    
    The XML File contains the following code.

    Code:
    <Student>
        <FirstName>Ajay</FirstName>
        <LastName>Patil</LastName>
    </Student>
    
    The output for the code is as shown below:

    [​IMG]

    2. group element

    Some elements can be grouped together by using a common name in an XML Schema. The syntax for declaring the group element is as shown below:

    Code:
    <group maxOccurs="nonnegativeInteger | unbounded" minOccurs="nonnegativeInteger"
    Name="Name" ref="rname" >
    </group>
    
    The attributes of the element are as listed below:
    1. maxOccurs: It is used to specify the maximum number of times a group can occur in the document.
    2. minOccurs: It is used to specify the minimum number of times a group can occur in the document.
    3. name: It is used to assign the name for the group element.
    4. ref: It is used to refer to the group in the complex type element.
    Consider the example to demonstrate the group element.

    The Schema file consists of the following code.

    Code:
    <xs: schema xmlns:xs="http://www.w3.org/2001/XMLSchema" >
        <xs: element name="Employee">
        <xs: group name="empdetails" >
            <xs: sequence>
            <xs: element name="Empid" type="xs: string" />
            <xs: element name="EmpAddress" type="xs: string" />
            </xs: sequence>
        </xs: group>
        
        <xs: complexType name="emptype">
            <xs: sequence>
            <xs: group ref="empdetails"></xs: group>
            <xs: element name="Emprole" type=" xs: string" >
            </xs: element>
        </xs: complexType>
        </xs: element>
    </xs: schema>
    
    In the above code, the Empid and EmpAddress elements are grouped together as empdetails. The sequence element is used to show the order of the elements. User can refer to the group name through the ref keyword.

    The XML File consists of the following code.

    Code:
    <Employee>
        <Empid>E101</Empid>
        <EmpAddress>Mumbai</EmpAddress>
        <EmpRole>Manager</EmpRole>
    </Employee>
    
    The output for the code is as shown below:

    [​IMG]

    3. choice element

    The element allows user to select the option from the list of options through the choice attribute.

    The syntax for the choice element is as shown below:

    Code:
    <choice id="ID’ maxOccurs="nonnegativeInteger | unbounded" minOccurs="nonnegativeInteger" ></choice>
    
    In the above code, Id, maxOccurs and minOccurs are attributes of the element. These attributes are similar to the group element.

    The example of the choice element is as shown below:
    The Schema file contains the following code.

    Code:
    <xs: schema xmlns:xs="http://www.w3.org/2001/XMLSchema" >
        <xs: element name="OrderDetials" type="prodtype">
        <xs: complexType name="prodtype">
            <xs: sequence>
            <xs: group ref="prodname"></xs: group>
            </xs: sequence>
        </xs: complexType>
        <xs: complexType name="addname" >
            <xs: choice>
            <xs: element name="Location" type="xs:string">
            <xs: element name="address" type="xs: string" >
            </xs: choice>
        </xs: complexType>
        <xs: group name="proddata">
            <xs: sequence>
            <xs: element name="Product" type="xs: string"></xs: element>
            <xs: element name="Material" type="xs: string"></xs: element>
            </xs: sequence>
        </xs: group>
        </xs: element>
    </xs: schema>
    
    The XML File consists of the following code.

    Code:
    <OrderDetails>
        <Product>Toys<Product>
        <Material>Plastics</Material>
        <Location>
        <address>Mumbai</address>
        <Location>
    </OrderDetails>
    
    The output for the code is as shown below:

    [​IMG]

    4. all element

    The all element enables you to use the child elements in any order. The syntax for the all element is as follows:

    Code:
    <all maxOccurs="positiveInteger" minOccurs="0|1" ></all>
    
    In the above syntax, maxOccurs and minOccurs are the attributes having the maximum and minimum occurrences of the element in the group.

    The following example demonstrates the use of the attribute.
    The Schema file contains the following code.

    Code:
    <xs: schema xmlns:xs="http:www.w3.org/2001/XMLSchema" >
        <xs: element name="Employee" type="empdetails" >
        <xs: complexType>
        <xs: all>
            <xs: element name="Empid" type="xs: unsignedByte" />
            <xs: element name="Empname" type="xs: string" />
            <xs: element name="Role" type="xs: string" />
            <xs: element name="Address" type="xs: string" />
        </xs: all>
        </xs: complexType>
        </xs: element>
    </xs: schema>
    
    The XML File consists of the following code.

    Code:
    <Employee>
        <Empid>101</Empid>
        <Empname>Nick</Empname>
        <Role>Manager</Role>
        <Address>Calcutta</Address>
    </Employee>
    
    The output for the code is as shown below:

    [​IMG]

    5. attributeGroup element

    The group attributes can be reused with different elements. The attributeGroup element is used to group attributes in the schema.

    The syntax for the attributeGroup element is as shown below:

    Code:
    <attributeGroup>
        attribute 1
        attribute 2
             .
                     .
    </attributeGroup>
    
    The example to demonstrate the attributeGroup element is as shown below:
    The Schema file is as shown below:

    Code:
    <xs: schema xmlns:xs="http:www.w3.org/2001/XMLSchema" >
        <xs: element name="GamePlayers" type="players">
        <xs: complexType name="players" >
    <xs: group ref="playersdata"></xs: group>
                <xs: attributeGroup ref="playersinfo"></xs: attributeGroup>
        </xs: complexType>
        <xs: group name="playersdata">
             <xs:sequence>
            <xs: element name="PlayerName" type="xs: string" />
            <xs: element name="PlayerAge" type="xs: unsignedByte" />
            <xs: element name="Experience" type="xs: string" />
            </xs: sequence>
        </xs: group>
        <xs: attributeGroup name="playerinfo">
            <xs: attribute name="Team" type="xs: string" >
            </xs: attribute>
            <xs: attribute name="Size" type="xs: unsignedByte">
            </xs: attribute>
        </xs: attributeGroup>
        </xs: element>
    </xs: schema>
    
    The XML file consists of the following code.

    Code:
    <GamePlayers Team="UK" Size="12">
        <PlayerName>John</PlayerName>
        <PlayerAge>25</PlayerAge>
        <Experience>4 years</Experience>
    </GamePlayers>
    
    The output for the code is as shown below:

    [​IMG]
     
    Last edited by a moderator: Jan 21, 2017

Share This Page