Introduction to Web Services Part 3: Understanding XML, Page 2
XML Schema is a more advanced version of DTD. DTD has lots of disadvantages over schema, such as it does not support strong data typing, has syntax other than XML, and it is not expandable. Schema was introduced to overcome those drawbacks. The most common features of XML Schema are:
- Syntax is very similar to XML. This means you can edit your schema by using any XML editor.
- You not only specify basic data types like string, integer, long, float, and so forth but also can define your own custom data types. Example:
<xs:element name="name" type="xs:string"/>The new types you can define are simple and complex types definition. Complex types may contain other elements and/or attributes, whereas Simple types do not contain other elements or attributes. Instead, they contain only simple text data.
- XML Schema provides Content-Based Validation (the order in which the child elements are nested) and also provides Data Type validations. You have lots of functionality and validation checks provided for simple and complex types.
For example, you can define a simple type with 'year' range between 2000 and 2100 as follows:
<xsd:simpleType name="year"> <xsd:restriction base="xsd:integer"> <xsd:minInclusive value="2000"/> <xsd:maxInclusive value="2100"/> </xsd:restriction></xsd:simpleType>
Similarly, Complex types can describe the restrictions on the sequence in which the child elements should appear. Example:
<xsd:complexType name="Employee"> <xsd:sequence> <xsd:element name="Name" type="xsd:string"/> <xsd:element name="Address" type="xsd:string"/> <xsd:element name="Phone" type=" xsd:string "/> </xsd:sequence></xsd:complexType>
- XML Schema provides you with the ability to extend other documents, which is nothing but inheritance in Object-Oriented terms. This means you can reuse and refine other schema definitions.
- Support for Namespace (using URI) is also provided by XML Schema. It provides each element a unique identifier, which avoids element name conflicts that may occur due to many reasons say, when two documents are merged, and both document have "name" fields but have different meanings to them. For example, "name" can be a person's name in one document, but it can be a spouse's name in another document. In short, it helps in distinguishing duplicate elements and attributes.
Here, you see 'id' is a prefix and namespace is 'http://somesite.com/schema'.
After you define a namespace, you can use the prefix on all the elements to uniquely identify it.
<myElement xmlns:id='http://somesite.com/schema'> <id:name>myName</id:name></ myElement >
- Last but not least, XML Schema is easily extendible to incorporate more features in the future.
Finally, let us take a quick look at a comparison between DTD and XML Schema.
DTD and XML Schema and DTD—a Comparsion
- XML Schema is an extension of DTD.
- XML Schema supports Namespace; DTD does not.
- XML Schema uses XML syntax that is easy to understand; DTD uses a specialized syntax.
- XML Schema supports Standard data types as well as user-defined; DTD provides for only textual types.
- XML Schema supports inheritance; DTD does not provide any Object-Oriented Features.
In this article, we covered the basics of XML to get you up to speed with XML concepts. This background of XML will help you when we study each of the technologies of Web Services in the coming weeks. Next, we will cover the advanced XML technologies such as ebXML and take a detailed look at how Web Services and XML go hand in hand.