Friday 7 November 2014

Default value for simple element


You can assign default value for simple element by using attribute “default”.

Syntax
<xs:element name=”nameOf TheVariable” type=”typeOfTheVariable” default=”defaultValue” />

Example
<xs:element name=”department” type=”xs:string” default=”CSE” />

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: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>
</student>

Prevoius                                                 Next                                                 Home

No comments:

Post a Comment