Friday 7 November 2014

Simple Elements


A Simple element in XML contains only text, no attributes and no child elements.

Example
          <firstname>hari</firstname>
          <middlename>Krishna</middlename>
          <lastname>gurram</lastname>
          <age>25</age>

Above elements are simple elements.

Syntax to define simple elements
<xs:element name=”nameOfTheElement” type=”typeOfTheElement” />

“type” represents the type of the element. XML schema has a lot of built in types like xs:string, xs:decimal, xs:integer, xs:date etc., we will discuss about the data types in later posts.

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


Prevoius                                                 Next                                                 Home

No comments:

Post a Comment