Breaking News

XML Schema Attributes

Simple elements cannot have attributes. But the attribute itself is always declared as a simple type. If an element has attributes, it is considered to be of a complex type.
Defining a simple XSD attribute
<xs:attribute name=”attbr_name” type=”data_type”/>
In the above declaration attbr_name is the name of the attribute and data_type specifies the data type of the attribute.
Here is an XML element with an attribute:
<name lang=”EN”>Dharmendra Das</name>
And here is the corresponding attribute definition:
<xs:attribute name=”lang” type=”xs:string”/>
Restrictions on Content
When an XML element or attribute has a data type defined, it puts restrictions on the element’s or attribute’s content.
If an XML element is of type “xs:date” and contains a string like “Hello World”, the element will not validate. 
Basic example:
<?xml version=”1.0″?>
<ebiz xmlns=””
xsi:schemaLocation= “tutorial_attrib.xsd”>
     <title>XML Tutorial</title>
     <author>R Tiwari</author>
     <publisher> Publication</publisher>
     <title>JSP Tutorial</title>
     <author>A Kumar</author>
     <publisher> Publication</publisher>
     <title>SQL Tutorial</title>
     <author>S Srivastava</author>
     <date> 29/04/2007</date>
     <publisher> Publication</publisher>
Click here to view the file.
XML Schema for above xml file: “tutorial_attrib.xsd”
<?xml version=”1.0″ encoding=”UTF-8″?>
<xsd:schema xmlns:xsd=””
<xsd:element name=”ebiz”>
     <xsd:element ref=”book” minOccurs=”1″ maxOccurs=”unbounded”/>
     <xsd:element name=”Book”>
          <xsd:element ref=”title” minOccurs=”1″ maxOccurs=”1″/>
          <xsd:element ref=”author” minOccurs=”1″ maxOccurs=”1″/>
          <xsd:element ref=”date” minOccurs=”1″ maxOccurs=”1″/>
          <xsd:element ref=”ISBN” minOccurs=”1″ maxOccurs=”1″/>
          <xsd:element ref=”publisher” minOccurs=”1″ maxOccurs=”1″/>
     <xsd:element name=”title” type=”xsd:string”/>
     <xsd:element name=”author” type=”xsd:string”/>
     <xsd:element name=”date” type=”xsd:data”/>
     <xsd:element name=”ISBN” type=”xsd:string”/>
     <xsd:element name=”publisher” type=”xsd:string”/>
<xsd:attributeGroup name=”tutorialAttribute”>
<xsd:attribute name=”Category” use=”required”>
     <xsd:restriction base=”xsd:string”>
     <xsd:enumeration value=”computer courses”/>
     <xsd:enumeration value=”language learning”/>
     <xsd:enumeration value=”utility”/>
     <xsd:attribute name=”isLaunched ” type=”xsd:boolean” default=”false”/>
     <xsd:attribute name=”reviewer” type=”xsd:string” default=” “/>


Check Also

XML and Server

Generating XML with JSP   XML can be generated on a server without any installed …