Search

Wednesday, March 5, 2014
0 comments

Creating grouped elements and attributes in XML

4:22 AMWednesday, March 5, 2014
The purpose of an XML schema is to define the building blocks of an XML document. An XML schema defines the following in an XML document:

  • Elements
  • Attributes
  • Child elements 
  • Order of the child elements
  • Number of child elements
  • State of the element, whether it is empty or includes text
  • Data types for the elements and attributes 
  • Default and fixed values for the elements and attributes

An XML schema enables you to combine related elements and attributes into groups. This Feature of creating grouped elements and attributes  enables you to perform the following tasks:
  • Create a reusable group of elements and attributes:  A reusable group can be used to specify the content  model for a complex type of element or attribute. This eliminates the task of declaring the elements that have already been declared in some other context. For example, you can create a group of the FIRSTNAME and LASTNAME elements and reuse the group for customers, suppliers, and employees.
  • Select a single element from a group: Assume that you want to use one element or Attribute, such as the phone number of an employee, from a group. To do so, you can create a group of such elements and allow one of them to be used in the XML document.
  • Specify the sequence of elements: You can create a group of  elements and specify the sequence in which each element in the group should appear in the XML document.
  • XSD provides the following  elements to group user-defined elements and attributes:

  1. sequence
  2. group
  3. choice
  4. all
  5. attributeGroup

The sequence Element

The sequence element ensures that the elements declared within the opening and closing tags  of this element appear in a specific order. For example, to ensure that the employee first name, last name, designation, and department appear in a specific order in an XML
document, you can use the following statements in an XML schema:

<xsd:schema  xmlns:xsd=”http://www.w3.org/2001/XMLSchema”>
     <xsd:element name =”EMPLOYEE” type=”emptype”/>
     <xsd:complexType name =”empType name=”emptype”>
             <xsd:sequence>
                    <xsd:element name=”FIRSTNAME” type=”xsd:string”/>
                    <xsd:element name=”LASTNAME” type=”xsd:string”/>
                    <xsd:element name=”DESIGN”  type=”xsd:string”/>
                    <xsd:element name=”DEPARTMENT” type=”xsd:string”/>
             </xsd:sequence>
        </xsd:complexType>
</xsd:schema>

In preceding schema, the EMPLOYEE element is associated with the complex type emptype element. This complex type element contains the declarations for the FIRSTNAME, LASTNAME, DESIG, and DEPARTMENT elements. All these declarations are contained within the sequence element. This ensure that the elements appear in the same sequence in which they are declared.

Therefore, if you validate the following XML document against the preceding schema, it will result in an error because the DEPARTMENT element should come after the DESIG
element according to the schema.

<?xml version=”1.0”?>
<EMPLOYEE>
         <FIRSTNAME> James </FIRSTNAME>
         <LASTNAME > Williams <LASTNAME>
         <DEPARTMENT> MKTG </DEPARTMENT>
         <DESIG> MKTG EXEC </DESIG>
</EMPLOYEE>

The group Element

A set of elements can be grouped together by a common name in an XML schema, and incorporated into a complex data type. Grouping of elements is beneficial when you want a set of related elements to be referred using a common name.
The syntax for declaring a group element is as follows:

<group maxOccurs=”nonNegativeInteger | unbounded”
minOccurs=”nonNegativeInteger” name=”NCName” ref=”QName “> </group>

The following table describes the attributes of the group element.
Attribute
Description
maxOccurs
Used to specify the maximum of times a group can occur in the
XML document. The value of the maxOccurs attribute must be an
integer greater than or equal to zero. If you do not want to set a limit on
the maximum number, you can specify the value of this attribute as
unbounded.
minOccurs
Used to specify the minimum number of times a group can occur in the
XMLdocument. The value of the minOccurs attribute must be an
integer greater than or equal to zero to specify that the group is optional.
name
Used to assign a name for the group element. The name assigned to
the group must not contain a colon.
ref
Used to refer to a group in a complex type element.
Consider the following example:
<xsd:schema xmlns:xsd=”http://www.w3.org/2001/XMLSchema”>
       <xsd:group name=”empname”>
              <xsd:sequence>
                     <xsd:element name=”FIRSTNAME” type=”xsd:string”/>
                     <xsd:element name=”LASTNAME” type=”xsd:string/>
                </xsd:sequence>
          </xsd:group>
<xsd:element name=”EMPLOYEE” type=”emptype”/>
<xsd:ComplexType name=”emptype”>
        <xsd:sequence>
               <xsd:group ref=”empname”/>
               <xsd:element name=”ADDRESS” type=”xsd:string”/>
         </xsd:sequence>
</xsd:complexType>
</xsd:schema>

In the preceding example, the FIRSTNAME and LASTNAME elements are grouped together by a common name called empname by using the group element. The order in which these elements must appear in the XML document is specified using the sequence element. You can refer to the empname group while creating a complex type by using the following Statement:

<xsd:group ref=”empname”/>

After declaring the elements are attributes in the preceding schema, you can create an XML document that conforms to the schema by using the following code snippet:

<?xml version=”1.0?>
<EMPLOYEE>
          <FIRSTNAME> Sam </FIRSTNAME>
<LASTNAME> Peterson </LASTNAME>
<ADDRESS> 10, LIONS STREET, BOSTON </ADDRESS>
</EMPLOYEE>

The choice Element      

XSD enables you to select a a single option from multiple options by using the choice element. The choice element allows only one of the elements contained in the group to be present within the parent element.

The syntax for declaring a choice element is as follows:

<choice id=”ID” maxOccurs=”nonNegativeInteger | unbounded”
minOccurs=”nonNegativeInteger”> </choice>

In the preceding syntax, id, maxOccurs, and minOccurs are attributes of the choice element. These attributes are similar to the attributes of the group element mentioned earlier.

Consider the following example. You may want to store either the office address or the residential  address of a customer in an XML document. You can implement this option by using the choice element, as shown in the following code:

<xsd:schema xmlns:xsd=”http://www.w3.org/2001/XMLSchema”>
        <xsd:element name=”CUSTOMER” type=”custtype”/>
        <xsd:complexType name=”custtype”>
               <xsd:sequence>
                      <xsd:group ref=”custname”/>
                      <xsd:element name=”ADDRESS” type=”addtype”/>
               </xsd:sequence>
                </xsd:complexType>
                <xsd:complexType name=”addtype”>
                       <xsd:choice>
                              <xsd:element name=”RESIDENCE” type=”xsd:string”/>
                              <xsd:element name=”OFFICE” type=”xsd:string”/>
                        </xsd:choice>
                  </xsd:complexType>
                  <xsd:group name=”custname”>
                         <xsd:sequence>
                                <xsd:element name=”FIRSTNAME” type=”xsd:string”/>
                                <xsd:element name=”LASTNAME” type=”xsd:string/>
                          </xsd:sequence>
                     </xsd:group>
               </xsd:schema> 

In the preceding code, the CUSTOMER element is declared with a reference to the complex
Type, custtype. This type further refers to the custname group, which contains the Declarations for the FIRSTNAME and LASTNAME elements. The custtype complex type also contains the declaration for the ADDRESS element, which refers to the complex type, addtype. This complex type contains the declerations for the RESIDENCE and OFFICE elements. These declerations appear within the choice element. Therefore, only one of these elements can be used at a time within the ADDRESS element.

The following XML doument conforms to the preceding schema:

<?xml version=”1.0”?>
<CUSTOMER>
        <FIRSTNAME> Sam </FIRSTNAME>
        <LASTNAME> Peterson </LASTNAME>
         <ADDRESS>
                  <RESIDENCE>10, LIONS STREET, BOSTON </RESIDENCE>
          </ADDRESS>
         </CUSTOMER>

If you include the OFFICE element within the preceding code, it will result in an error During document validation.

The all Element

In contrast to the sequence element, the all element enables you to use the child elements in any order.

The syntax for using the all element is as follows:

        <all maxOccurs=”posiveInteger” minOccurs=”0|1”> </all>
In the preceding syntax, the maxOccurs and minOccurs attributes have the same meaning as the maxOccurs and minOccurs element elements of the group element.

The following code contains the declaration for the EMPLOYEE element and its child elements. The code is the same as the one given for the group element. However, the sequence element has been replaced by the all element here.

<xsd:schema xmlns:xsd=”http://www.w3.org/2001/XMLSchema”>
<xsd:element name=”EMPLOYEE” type=”emptype” />
       <xsd:complexType name=”emptype”>
              <xsd:all>
                      <xsd:element name=”FIRSTNAME” type=”xsd:string”/>
                      <xsd:element name=”LASTNAME” type=”xsd:string”/>
                      <xsd:element name=”DESIG” type=”xsd:string/>
                      <xsd:element name=”DEPARTMENT” type=”xsd:string/>
                </xsd:all>
          </xsd:complexType>
    </xsd:schema>

The attributeGroup Element

XSD enables you to group attributes that can be reused with different elements. You can group attributes by using the attributeGroup element:

The syntax of the attributeGroup element is as follows:

        <attributeGroup>
              attribute1
              attribute2
                  :
          </attributeGroup>
In the syntax, the attribute1 and attribute2 statements declare the various attributes that are to be grouped. The following example illustrates the use of the attributeGroup element. This code is used to declare the EMPLOYEE element, which contains the FIRSTNAME and LASTNAME elements. The EMPLOYEE element also contains the DEPARTMENT and DESIGNATION attributes.
        
        <xsd:schema xmlns:xsd=”http://www.w3.org/2001/XMLSchema”> 
               <xsd:element name=”EMPLOYEE” type=”emptype”/>
                <xsd:complexType name=”emptype”>
                       <xsd:group ref=”empname”/>
                       <xsd:attributeGroup ref=”depdesig/>
                </xsd:ComplexType>
                <xsd:group name=”empname”>
                       <xsd:sequence>
                              <xsd:element name=”FIRSTNAME” type=”xsd:string”/>
                              <xsd:element name=”LASTNAME” type=”xsd:string”/>
                       </xsd:sequence>
               </xsd:group>
               <xsd:attributeGroup name=”depdesig”>
                      <xsd:attribute name=”DEPARTMENT” type=”xsd:string”/>
                      <xsd:attribute name=”DESIGNATION” type=”xsd:string”/>
                 </xsd:attributeGroup>
        </xsd:schema>

In the preceding example, the emptype complex type has a reference to the depdesig attribute group, which contains the declarations for the DEPARTMENT and DESIGNATION attributes.

You can use the elements and attributes declared in the preceding schema as follows:

        <?xml version=”1.0”?>
         <EMPLOYEE DEPARTMENT=”Mktg” DESIGNATION=”Mgr”>
                  <FIRSTNAME> James </FIRSTNAME>
                   <LASTNAME> Wallace </LASTNAME>
         </EMPLOYEE>

An attributeGroup element is typically used in cases where the same set of attributes is Used in multiple elements within the schema.
Protected by Copyscape Online Copyright Protection Software

0 comments :

Post a Comment

 
Toggle Footer
Top