1. We have moved from vBulletin to XenForo and you are viewing the site in the middle of the move. Though the functional aspect of everything is working fine, we are still working on other changes including the new design on Xenforo.
    Dismiss Notice

Working with XML Schemas and Namespaces

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

  1. MinalS

    MinalS New Member

    Joined:
    Jul 8, 2014
    Messages:
    138
    Likes Received:
    29
    Trophy Points:
    0
    An XML document consists of several elements. The XML schema defines the list of elements and attributes that can be used in the XML document. The orders in which the elements appear are defined in the XML schema. The XML Schema Definition (XSD) defines the schema of the XML document.

    XML Schema Elements



    The schema consists of several elements. Based on the types, there are two types as simple and complex element defined in the schema. The details about the elements are explained below:

    1. Simple Element

    The simple element contains values as members, strings and dates. It does not contain any attribute or child element.

    Any element in an XSD must be associated with a predefined or custom data type. The syntax for the declaration of the simple element is as follows:

    Code:
    <xsd: element name="element-name" type="data type" minOccurs="non negative Integer" maxOccurs="non negative Integer|unbounded" />
    
    Where, element is used for declaring the new element. The following list shows the attributes of this element.
    1. name: It specifies the name of the element declared.
    2. type: It defines the data type of the element
    3. minOccurs: It specifies the minimum times the element can occur. The parameter value zero indicates that the element is optional whereas the values greater than zero indicates the element is mandatory.
    4. maxOccurs: It specifies the maximum times the element can occur. If the parameter value is unbounded, the element can appear any number of times in the document.
    Consider the example of the STUDENT. The tag structure is as shown below:

    Code:
    <? xml version="1.0" encoding="utf-8" ? >
    	<Department>
    		<DeptID> D111 </DeptID>
    		<DeptName>Production </DeptName>
    	</Department>
    
    In the above code, the STUDENTNAME and COURSE are the simple elements. The simple elements can be declared in the XSD as follows:

    Code:
    <xsd:element name="Department"></xsd:element>
    	<xsd:sequence>
    <xsd:element name="DeptID" type="xsd:integer"></xsd:element>
    <xsd:element name="Deptname" type="xsd:string"></xsd:element>
    	</xsd:sequence>
    </xsd:element>
    
    The output for the simple element is as shown below:

    [​IMG]

    2. Complex Element

    The complex element can contain other elements, attributes and mixed content. The complex type needs to be defined while using the complex element. The complex elements associated with the data type are declared.

    The syntax for defining the complex element is as follows:

    Code:
    <xsd: complexType name="data type name" >
    	Content model declaration
    </xsd: complexType>
    
    Where, the name attribute specifies the name of the new complex data type. The declaration contains the declaration for the elements and attributes.

    Consider the example of the STUDENT. The tag structure is as shown below:

    Code:
    <? xml version="1.0" encoding="utf-8" ? >
    <STUDENTINFO>
    	<STUDENT STUDENTID="101"></STUDENT>
    	<STUDENTNAME>John</STUDENTNAME>
    	<COURSE>MBA</COURSE>
    </STUDENTINFO>
    
    In the above code, the STUDENTNAME and COURSE are the simple elements. The simple elements can be declared in the XSD as follows:

    Code:
    <xsd:element name="STUDENTINFO">
    	<xsd:complexType>
    	<xsd:sequence>
    <xsd:element name="STUDENTNAME" type="xsd:string"></xsd:element>
    <xsd:element name="COURSE" type="xsd:string"></xsd:element>
    	</xsd:sequence>
             <xsd:attribute name="ID" type="xs:unsignedByte" />
            </xsd:complexType>
    </xsd:element>
    
    The output for the simple element is as shown below:

    [​IMG]

    Attributes in a Schema



    The declaration of attributes is similar to elements in the schema. The declaration of the attributes can be categorized into two types as simple type definitions and global type definitions. The simple type can be used for local attribute validation. The global type can be used for reuse of the attributes.

    Attribute Element

    In an XML definition, an attribute for a user defined element is declared using the attribute element. The syntax for the attribute declaration is as shown below:

    Code:
    <attribute name="attributename" ref="attributename" type="datatypename" use="value" value="value" > </attribute>
    
    The attribute element consists of various elements. Some of them are as follows:
    1. name attribute
    2. ref attribute
    3. type attribute
    4. use attribute
    1. name attribute

    It is used to define the name of the user defined attribute. It must be used when the schema element is the parent element.

    2. ref attribute

    It is used to refer to the user defined attribute. It can be in the same or another XSD document. The attribute cannot be defined or referred to an attribute in the same statement.

    3. type attribute

    It takes a value specifying the data type of the user defined attribute. Consider the following example.

    Code:
    <xsd: attribute name="STUDENTID" type="xsd:integer" 
    In the above example, the STUDENTID attribute is declared. The type attribute is set to integer, a simple data type.

    The attribute can be used with both built in data type and user defined data type. The example to demonstrate the user defined data type is as shown below:

    Code:
    <xsd: attribute name="DeptID" type="empID" >
    
    In the above example, the empID is the user defined simple data type.

    4. use attribute

    The use attribute states the way in which the attribute can make the XML document. The use attribute contains the values assigned to it.

    The lists of values that can be assigned are:

    1. optional: It specifies the use of the user defined attribute in the XML document. It is not a mandatory element.

    The example of optional is as shown below:

    Code:
    <xsd: attribute name="price" type="xsd: integer" use="optional" />
    
    2. default: It specifies the default value for the user defined attribute. The value is not specified in the document.

    The example for the default value is as shown below:

    Code:
    <xsd: attribute name="price" type="xsd: integer" use="default" value="30" />
    3. required: It specifies the attribute that must appear in the XML document. The error is generated if the value is not specified.

    The example for the required attribute is as shown below:

    Code:
    <xsd: attribute name="price" type="xsd:integer" use="required" />
    4. fixed: It specifies the user defined attribute that has a fixed value.

    The example for the fixed attribute is as shown below:

    Code:
    <xsd: attribute name="price" type="xsd:integer" use="fixed" value="500" />
    The code snippet demonstrates the use of the attribute element.
    The XML file consists of the following code.

    Code:
    <?xml version="1.0" encoding="utf-8" ?>
      <productOrder xmlns="http://www.w3.org/2001/XMLSchema" orderDate="2010-01-01">
    	<shipto country="UK">
    	<productname>Toys</productname>
    	<quantity>100</quantity>
    	<Price>50</Price>
    	</shipto>
      </productOrder> 
    
    The XSLT file consists of the following code.

    Code:
    
    <xsd: element name="productOrder" type="PurchaseOrderType" >
        <xsd:complexType id="prod">
    	<xsd:sequence>
    		<xsd: element name="productname"></xsd: element>
    		<xsd: element name="quantity"></xsd: element>
    		<xsd: element name="Price"></xsd:element>
    	</xsd:sequence>
       </xsd: ComplexType>
    </xsd: element>
    
    
    The output for the code is as shown below:

    [​IMG]

    Global Attributes



    There are requirements when user needs to use the same attribute for some elements. At such times, global attributes are used. They are declared outside the element declaration. They provide attribute reusability.

    Global attributes are declared directly under the schema. It can be reused from anywhere under the schema elements.

    Consider the example of the global attributes in the schema.

    Code:
    <xsd: schema xmlns:xsd=http://www.w3.org/2001/XMLSchema">
    	<xsd:attribute name="amit" type="xsd:float" />
    	<xsd:element name="root">
    	<xsd:complexType>
    		<xsd:attribute ref="amit" />
    	</xsd:complexType>
    	</xsd:element>
    </xsd:schema>
    
    The following declaration is accepted by the schema which is globally applied to the XML documents.

    Code:
    <root xmlns:"http://abc.com" amit="11.1" />
    
    Restricting attribute values

    User needs to apply the pattern to the specified attributes that match a certain pattern. The value of the use attribute is set to the required to ensure that the value assigned to the attribute matches the pattern.

    Consider the following example of an element with the pattern restriction.

    Code:
    <xsd: schema xmlns: xsd="http://www.w3.org/2001/XMLSchema" >
    	<xsd:attribute name="StudID" type="sID" use="required">
    		<xsd:simpleType name="sID">
    		<xsd:restriction base="xsd:string" >
    		<xsd:pattern value="[P][1]\d{4}" />
    		</ xsd:restriction>
    		</ xsd:simpleType>
    </xsd: schema>
    
    In the above example, the StudID attribute is associated with the simple data type called sID. The pattern is specified in the restriction element. The error message is generated if the value does not match the pattern.

    Namespaces in XML



    The xmlns keyword can be used to declare the namespace in the XSD document. It is the keyword of the attribute in the schema element. The general syntax of the schema is as shown below:

    Code:
    xmlns: prefix="URI"
    
    The xmlns is the name of the attribute. It is followed by the optional parameter namespace prefix. The value of the attribute is the namespace URI associated with the XSD document. There are two types of namespaces in XML as default and explicit.

    Default Declaration

    The default namespace can be declared for the document using the default statement. There is no prefix for the default namespace. The declaration for the default namespace is as shown below:

    Code:
    <schema xmlns:"http://www.w3.org/2001/XMLSchema" >
    
    	Code to added
    
    </schema>
    
    Explicit Declaration

    The xmlns keyword is associated with the namespace URI. The following declaration shows the syntax of the explicit declaration.

    Code:
    <xsd: schema xmlns:"http://www.w3.org/2001/XMLSchema" >
    
    </xsd:schema>
    
    The keyword is used to associate with the URI.
     
    Safari and shabbir like this.
  2. Safari

    Safari New Member

    Joined:
    Oct 16, 2007
    Messages:
    183
    Likes Received:
    17
    Trophy Points:
    0
    Really nice contribution. Keep it up.
     

Share This Page