XML Schema Syntax
The example XML Schema below contains the following items:
- The first statement,
<?xml version="1.0" encoding="utf-8"?>, specifies the version of XML in use.
- The second statement consists of several parts:
xs:schemadeclaration indicates that this is a schema and that the xs: prefix will be used before schema items.
xmlns:xs="http://www.w3.org/2001/XMLSchema"declaration indicates that all tags in this schema should be interpreted according to the default namespace of the World Wide Web Consortium (W3C), which was created in 2001.
targetnamespacedeclaration names this schema as XMLSchema1.xsd and indicates its default location, which will be in a default URI (universal resource identifier) on your development server called tempuri.org.
For more information about xmlns and targetnamespace, see schema Element.Note When you create a schema with the XML Designer, the version and namespace statements are automatically added for you. You might modify the contents of these two declarations in some cases. For example, if you want to use a tag prefix other than "XS," you would need to define the default namespace for that tag in addition to the XS tag prefix.
- An unnamed complex type called "addressType" is defined that will contain five elements of various data types. Note that each of the elements it contains is a simple, named type.
- A complex element named "purchaseOrder" is defined. It contains elements that make use of the previously defined addressType. This creates a nested relationship between the purchaseOrder element and these elements.
- The final element in the purchaseOrder, Item, contains a facet called maxOccurs. Facets set limits on the type of content a simple type can contain. maxOccurs is used to constrain how many times an element can occur in the documents created from this schema. By default, maxOccurs is equal to one. In this case, setting it to unbounded indicates that the Item element can reoccur as many times as needed.
The following example shows a sample XML Schema:
<?xml version="1.0" encoding="utf-8"?> <xs:schema targetNamespace="http://tempuri.org/XMLSchema1.xsd" xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xs:complexType name="addressType"> <xs:sequence> <xs:element name="street1" type="xs:string"/> <xs:element name="street2" type="xs:string"/> <xs:element name="city" type="xs:string"/> <xs:element name="state" type="xs:string"/> <xs:element name="zip" type="xs:integer"/> </xs:sequence> </xs:complexType> <xs:element name="purchaseOrder"> <xs:complexType> <xs:sequence> <xs:element name="shipTo" type="addressType" /> <xs:element name="billTo" type="addressType" /> <xs:element name="shipDate" type="xs:date" /> <xs:element name="item" maxOccurs="unbounded"/> </xs:sequence> </xs:complexType> </xs:element> </xs:schema>