Friday, 7 November 2014

XML Attributes


The basic building blocks of XML Schemas are elements and attributes. Elements are used to describe the data, and attributes provide additional information about the element. You can assume attributes are like properties of element.

Example
<address type="permanent">
        <city>Ongole</city>
        <district>Prakasam</district>
        <state>Andhra Pradesh</state>
        <country>India</country>
</address>

Above case, type specifies whether it is permanent or temporary address.

How to define XML schema for element
<xs:attribute name="nameOfTheVariable" type="typeOfTheVariable"/>

For address example schema looks like below.

<xs:element name="address">
 <xs:complexType>
         <xs:sequence>
                 <xs:element name="city" type="xs:string" />
                        <xs:element name="district" type="xs:string" />
                        <xs:element name="state" type="xs:string" />
                        <xs:element name="country" type="xs:string" />
                </xs:sequence>
                <xs:attribute name="type" type="xs:string" />
 </xs:complexType>
</xs:element>

student.xsd
<?xml version="1.0" encoding="UTF-8"?>
<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="middlename" type="xs:string" />
                <xs:element name="lastname" type="xs:string" />
                <xs:element name="age" type="xs:integer" />
                <xs:element name="department" type="xs:string" default="CSE" />
                <xs:element name="dresscode" type="xs:string" fixed="Black and White" />
                <xs:element name="address">
                    <xs:complexType>
                        <xs:sequence>
                            <xs:element name="city" type="xs:string" />
                            <xs:element name="district" type="xs:string" />
                            <xs:element name="state" type="xs:string" />
                            <xs:element name="country" type="xs:string" />
                        </xs:sequence>
                        <xs:attribute name="type" type="xs:string" />
                    </xs:complexType>
                </xs:element>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
</xs:schema> 


student.xml
<?xml version="1.0" encoding="UTF-8"?>

<student xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:noNamespaceSchemaLocation="student.xsd">
    <firstname>hari</firstname>
    <middlename>krishna</middlename>
    <lastname>gurram</lastname>
    <age>25</age>
    <department></department>
    <dresscode>Black and White</dresscode>
    <address type="permanent">
        <city>Ongole</city>
        <district>Prakasam</district>
        <state>Andhra Pradesh</state>
        <country>India</country>
    </address>
</student>

Prevoius                                                 Next                                                 Home

No comments:

Post a Comment