0% found this document useful (0 votes)
14 views3 pages

XML Schema

Uploaded by

vishnuk448509
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views3 pages

XML Schema

Uploaded by

vishnuk448509
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

XML XSD (XML Schema Definition)

XML schema is an alternative to DTD. An XML document is considered “well formed” and
“valid” if it is successfully validated against XML Schema. The extension of Schema file
is .xsd.

An example of XML Schema


XML file: bb.xml

<?xml version="1.0"?>
<beginnersbook xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="bb.xsd">
<to>My Readers</to>
<from>Chaitanya</from>
<subject>A Message to my readers</subject>
<message>Welcome to beginnersbook.com</message>
</beginnersbook>
XML Schema file: bb.xsd

<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns="https://www.beginnersbook.com">

<xs:element name="beginnersbook">
<xs:complexType>
<xs:sequence>
<xs:element name="to" type="xs:string"/>
<xs:element name="from" type="xs:string"/>
<xs:element name="subject" type="xs:string"/>
<xs:element name="message" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>

</xs:schema>
Explanation:

 <xs:element name=”beginnersbook”> defines that beginnersbook is the name of an


element.
 <xs:complexType> This is the next line after the element “beginnersbook”. It defines
the type of element “beginnersbook”, it says that the type of this element is
“complexType” ,<xs:sequence> It defines that the complex type element
“beginnersbook” is a sequence of elements
 <xs:element name=”to” type=”xs:string”> It defines that the element “to” is of type
string
 <xs:element name=”from” type=”xs:string”> It defines that the element “from” is of type
string
 <xs:element name=”subject” type=”xs:string”> It defines that the element “subject” is
of type string
 <xs:element name=”message” type=”xs:string”> It defines that the element “message”
is of type string

XML Schema Data types


In the above example, we have seen that the root element “beginnersbook” is
complexType. In XML schema an element belongs to either of the following two types.

1. simpleType – A singleType element can contain text, they do not contain other elements.
In the above example, the elements to, from, subject and message are simpleType
element.

2. complexType – A complexType element can contain attributes, other elements, and text.
In the above example, the element beginnersbook is of type complexType because it
contains other elements.

Specifying Element Cardinality


It is possible to constrain the number of instances (cardinality) of an XML element that appear in
an XML document. The cardinality is specified using the minOccurs and maxOccurs attributes,
and allows an element to be specified as mandatory, optional, or can appear up to a set number
of times. The default values for minOccurs and maxOccurs is 1. Therefore, if both the
minOccurs and maxOccurs attributes are absent, as in all the previous examples, the element
must appear once and once only.

minOccurs can be assigned any non-negative integer value (e.g. 0, 1, 2, 3... etc.), and
maxOccurs can be assigned any non-negative integer value or the special string constant
"unbounded" meaning there is no maximum so the element can occur an unlimited number of
times.
Sample XSD Description

<xs:element name="Customer_dob" If we do not specify minOccurs or maxOccurs,


then the default values of 1 are used.
type="xs:date" /> This means there has to be one and only one
occurrence of Customer_dob, i.e. it is mandatory.

<xs:element name="Customer_order" If we set minOccurs to 0, then the element is optional.


Here, a customer can have from 0 to an unlimited
type="xs:integer" number of Customer_orders.
minOccurs ="0"

maxOccurs="unbounded" />

<xs:element name="Customer_hobbies" Setting both minOccurs and maxOccurs means


the element Customer_hobbies must appear at
type="xs:string" least twice, but no more than 10 times.
minOccurs="2"
maxOccurs="10" />

Advantages of using XML Schema over DTD


1. Schema uses XML as language so you don’t have to learn new syntax.
2. XML schema supports data types and namespaces.
3. You can use XML parser to parse the XML schema as well.
4. Just like XML, the XML schema is extensible which means you can reuse the schema in
other schema, as well as you can reference more than one schemas in a single XML
document.

You might also like