0% found this document useful (0 votes)
65 views6 pages

XML XSD

The document discusses XML schema which is an alternative to DTDs for validating XML documents. It provides an example of an XML schema definition and explains some of its key components and advantages over DTDs.
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)
65 views6 pages

XML XSD

The document discusses XML schema which is an alternative to DTDs for validating XML documents. It provides an example of an XML schema definition and explains some of its key components and advantages over DTDs.
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/ 6

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>
<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"
targetNamespace="https://www.beginnersbook.com"
xmlns="https://www.beginnersbook.com"
elementFormDefault="qualified">

<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” (we will discuss this type
later in this same tutorial)
 <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.

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.

employee.xml

1. <?xml version="1.0"?>
2. <employee
3. xmlns="http://www.javatpoint.com"
4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5. xsi:schemaLocation="http://www.javatpoint.com employee.xsd">
6.
7. <firstname>vimal</firstname>
8. <lastname>jaiswal</lastname>
9. <email>[email protected]</email>
10. </employee>
employee.xsd

1. <?xml version="1.0"?>
2. <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
3. targetNamespace="http://www.javatpoint.com"
4. xmlns="http://www.javatpoint.com"
5. elementFormDefault="qualified">
6.
7. <xs:element name="employee">
8. <xs:complexType>
9. <xs:sequence>
10. <xs:element name="firstname" type="xs:string"/>
11. <xs:element name="lastname" type="xs:string"/>
12. <xs:element name="email" type="xs:string"/>
13. </xs:sequence>
14. </xs:complexType>
15. </xs:element>
16.
17. </xs:schema>

<?xml version="1.0"?>
<Staff>
<Employee>
<Name>
<Surname>Herman</Surname>
<Firstname>Ivan</Firstname>
</Name>
<Title>Dr</Title>
<Dept>INS</Dept>
<Started>01.10.88</Started>
<Email>[email protected]</Email>
<Tel>4163</Tel>
<Fax>4199</Fax>
<Building>Secondary</Building>
<Room>C112</Room>
</Employee>
</Staff>

In traditional XML, possible tags are defined by a DTD

(Document Type Definition)


DTD Type Definition
<!ELEMENT Staff (Employee)*>
<!ELEMENT Employee (Name,Title,Dept,
Started,Email?,Tel,Fax?,Building,Room)>
<!ELEMENT Name (Surname, FirstName+)>
<!ELEMENT Surname (#PCDATA)>
<!ELEMENT FirstName (#PCDATA)>
<!ELEMENT Title (#PCDATA)>
<!ELEMENT Dept (#PCDATA)>
<!ELEMENT Started (#PCDATA)>
<!ELEMENT Email (#PCDATA)>
<!ELEMENT Tel (#PCDATA)>
<!ELEMENT Fax (#PCDATA)>
<!ELEMENT Building (#PCDATA)>
<!ELEMENT Room (#PCDATA)>
<!ATTLIST Tel CDATA #REQUIRED>
<!ATTLIST Fax CDATA #REQUIRED>
<!ATTLIST Building CDATA #REQUIRED>

What is wrong with DTD-s?


 DTD uses different notation; ie, it requires:
 Special parser
 Special editor
 Special processor
 Special ad hoc notation
 No real datatypes
 Complex to extend
 DTD cannot refer to namespaces

So XML made them optional (well-formedness may suffice)!

Schemas
Like DTDs, but better:

 Functionality of DTDs
 XML syntax
 Include:
 Datatypes (more than 30 instead of 10!) that can also define the
lexical representation (must be of form dd.ddd, say)
 Inheritance (can create your own datatypes)
 Schema combination rules
 Proper namespace support
 Richer content model
 Better hooks for documentation and semantics

Schema Version for our DTD


<element name="Staff" type="Staff_type">
<complexType name="Staff_type">
<element name="Employee" type="Employee_type"
minOccurs="1" maxOccurs="unbounded"/>
</complexType>
<complexType name="Employee_type">
<sequence>
<element name="Name" type="Name_type" />
<element name="Title" type="string"/>
...
<element name="Email" type="string"
minOccurs="0" maxOccurs="1"/>
</sequence>
<complexType>
<complexType name="Name_type">
<sequence>
<element name="Surname" type="string"/>
<element name="FirstName" type="string"
minOccurs="1" maxOccurs="unbounded"/>
</sequence>
</complexType>

Schema Terminology
 Element and attribute types:
 with text content only: simple type
 with subelements and/or attributes: complex type
 All attributes are of simple type
 Schemas define which elements and attributes can appear
 It may define new types (simple and complex)

You might also like