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=”http://www.ebizel.com”
xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance”
xsi:schemaLocation= “tutorial_attrib.xsd”>
<book>
     <title>XML Tutorial</title>
     <author>R Tiwari</author>
     <date>29/04/2007</date>
     <ISBN>1-56592-235-2</ISBN>
     <publisher>eBIZ.com Publication</publisher>
</book>
<book>
     <title>JSP Tutorial</title>
     <author>A Kumar</author>
     <date>29/04/2007</date>
    <ISBN>0-440-34319-4</ISBN>
     <publisher>eBIZ.com Publication</publisher>
</book>
<book>
     <title>SQL Tutorial</title>
     <author>S Srivastava</author>
     <date> 29/04/2007</date>
     <ISBN>0-06-064831-7</ISBN>
     <publisher>eBIZ.com Publication</publisher>
</book>
</ebiz>
 
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=”http://www.w3.org/2001/XMLSchema”
targetNamespace=”http://www.ebizel.com”
xmlns=”http://www.ebizel.com”
elementFormDefault=”qualified”>
<xsd:element name=”ebiz”>
     <xsd:complexType>
     <xsd:sequence>
     <xsd:element ref=”book” minOccurs=”1″ maxOccurs=”unbounded”/>
     </xsd:sequence>
     </xsd:complexType>
     </xsd:element>
     <xsd:element name=”Book”>
<xsd:complexType>
     <xsd:sequence>
          <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:sequence>
</xsd:complexType>
</xsd:element>
     <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:simpleType>
     <xsd:restriction base=”xsd:string”>
     <xsd:enumeration value=”computer courses”/>
     <xsd:enumeration value=”language learning”/>
     <xsd:enumeration value=”utility”/>
</xsd:restriction>
</xsd:simpleType>
</xsd:attribute>
     <xsd:attribute name=”isLaunched ” type=”xsd:boolean” default=”false”/>
     <xsd:attribute name=”reviewer” type=”xsd:string” default=” “/>
</xsd:attributeGroup>

</xsd:schema>

Check Also

XML and Server

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