XML Schema
XML Schema
By Rajneesh Goyal
Introduction to Schema
XML Schema is an XML-based alternative to DTD.
An XML schema describes the structure of an XML document.
The XML Schema language is also referred to as XML Schema Definition (XSD).
Referring to a schema
To refer to a DTD in an XML document, the reference goes before the root element: <?xml version="1.0"?> <!DOCTYPE rootElement SYSTEM "url"> <rootElement> ... </rootElement> To refer to an XML Schema in an XML document, the reference goes in the root element: <?xml version="1.0"?> <rootElement xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" (The XML Schema Instance reference is required) xsi:noNamespaceSchemaLocation="url.xsd"> (This is where your XML Schema definition can be found) ... </rootElement>
<schema>
The <schema> element may have attributes: xmlns:xs="http://www.w3.org/2001/XMLSchema" This is necessary to specify where all our XSD tags are defined
elementFormDefault="qualified" This means that all XML elements must be qualified
Complex elements
A complex element is defined as <xs:element name="name"> <xs:complexType> ... information about the complex type... </xs:complexType> </xs:element> Example: <xs:element name="person"> <xs:complexType> <xs:sequence> <xs:element name="firstName" type="xs:string" /> <xs:element name="lastName" type="xs:string" /> </xs:sequence> </xs:complexType> </xs:element> <xs:sequence> says that elements must occur in this order Remember that attributes are always simple types
Indicators
Order indicators: All Choice Sequence Occurrence indicators: maxOccurs minOccurs Group indicators: Group name attributeGroup name
A schema Example
<?xml version="1.0" encoding="ISO-8859-1"?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified"> <xs:element name="persons"> <xs:complexType> <xs:sequence> <xs:element name="person" maxOccurs="unbounded"> <xs:complexType> <xs:sequence> <xs:element name="full_name" type="xs:string"/> <xs:element name="child_name" type="xs:string" minOccurs="0" maxOccurs="5"/> </xs:sequence> </xs:complexType> </xs:element> </xs:sequence> </xs:complexType> </xs:element> </xs:schema>
Group Elements
<xs:group name="persongroup"> <xs:sequence> <xs:element name="firstname" type="xs:string"/> <xs:element name="lastname" type="xs:string"/> <xs:element name="birthday" type="xs:date"/> </xs:sequence> </xs:group> <xs:element name="person" type="personinfo"/> <xs:complexType name="personinfo"> <xs:sequence> <xs:group ref="persongroup"/> <xs:element name="country" type="xs:string"/> </xs:sequence> </xs:complexType>
Defining an attribute
Attributes themselves are always declared as simple types An attribute is defined as <xs:attribute name="name" type="type" /> where: name and type are the same as for xs:element Other attributes of a simple element may have: default="default value" if no other value is specified fixed="value" no other value may be specified use="optional" the attribute is not required (default) use="required" the attribute must be present
Attribute Groups
<xs:attributeGroup name="personattrgroup"> <xs:attribute name="firstname" type="xs:string"/> <xs:attribute name="lastname" type="xs:string"/> <xs:attribute name="birthday" type="xs:date"/> </xs:attributeGroup>
<xs:element name="person"> <xs:complexType> <xs:attributeGroup ref="personattrgroup"/> </xs:complexType> </xs:element>
Restrictions
The general form for putting a restriction on a text value is: <xs:element name="name"> (or xs:attribute) <xs:restriction base="type"> ... the restrictions ... </xs:restriction> </xs:element> For example: <xs:element name="age"> <xs:restriction base="xs:integer"> <xs:minInclusive value="0"> <xs:maxInclusive value="140"> </xs:restriction> </xs:element>
C-DAC,Hyderabad
Restrictions on numbers
minInclusive -- number must be the given value
C-DAC,Hyderabad
Restrictions on strings
length -- the string must contain exactly value characters minLength -- the string must contain at least value characters maxLength -- the string must contain no more than value characters pattern -- the value is a regular expression that the string must
match
C-DAC,Hyderabad
Recall that a simple element is defined as: <xs:element name="name" type="type" /> Here are a few of the possible string types: xs:string -- a string xs:normalizedString -- a string that doesnt contain tabs, newlines, or carriage returns xs:token -- a string that doesnt contain any whitespace other than single spaces Allowable restrictions on strings: enumeration, length, maxLength, minLength, pattern, whiteSpace
C-DAC,Hyderabad
C-DAC,Hyderabad
End of Schema