YouTip LogoYouTip

Schema Example

## XSD Example * * * This section will demonstrate how to write an XML Schema. You will also learn different ways to write a schema. * * * Let's look at this XML document named "shiporder.xml": ```xml John Smith Ola Nordmann
Langgt 23
4000 Stavanger Norway Empire Burlesque Special Edition 1 10.90 Hide your heart 1 9.90 The above XML document includes the root element "shiporder", which contains an attribute that must be named "orderid". The "shiporder" element contains three different child elements: "orderperson", "shipto", and "item". The "item" element appears twice and contains a "title", an optional "note" element, a "quantity", and a "price" element. The line `xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"` tells the XML parser to validate this document against a schema. The line `xsi:noNamespaceSchemaLocation="shiporder.xsd"` specifies the location of the schema (in this case, it is in the same folder as "shiporder.xml"). * * * ## Creating an XML Schema Now, we need to create a schema for the above XML document. We can start by opening a new file and naming it "shiporder.xsd". To create the schema, we simply need to follow the structure in the XML document and define each element we find. We start by defining a standard XML declaration: ```xml ... In the schema above, we use the standard namespace (xs), and the URI associated with this namespace is the Schema language definition, whose standard value is `http://www.w3.org/2001/XMLSchema`. Next, we need to define the "shiporder" element. This element has an attribute and contains other elements, so we define it as a complex type. The child elements of the "shiporder" element are enclosed within an `xs:sequence` element, which defines the order of the child elements: ```xml ... Then we need to define the "orderperson" element as a simple type (because it does not contain any attributes or other elements). The type prefix (`xs:string`) is specified by the namespace prefix associated with the XML schema that indicates predefined schema data types: ```xml Next, we need to define two elements as complex types: "shipto" and "item". We start by defining the "shipto" element: ```xml With a schema, we can use the `maxOccurs` and `minOccurs` attributes to define how many times an element may appear. `maxOccurs` defines the maximum number of times an element can appear, and `minOccurs` defines the minimum number of times. The default values for both `maxOccurs` and `minOccurs` are 1! Now, we can define the "item" element. This element can appear multiple times inside the "shiporder" element. This is achieved by setting the `maxOccurs` attribute of the "item" element to "unbounded", allowing the "item" element to appear as many times as the creator wishes. Note that the "note" element is optional. We have set its `minOccurs` attribute to 0: ```xml Now, we can declare the attribute for the "shiporder" element. Since it is a required attribute, we specify `use="required"`. **Note:** The attribute declaration must be placed last: ```xml Here is the complete schema file named "shiporder.xsd": ```xml * * * ## Splitting the Schema The design method above is very simple, but it can be difficult to read and maintain when documents are complex. The next design method introduced is based on first defining all elements and attributes, and then using the `ref` attribute to reference them. Here is the schema file ("shiporder.xsd") designed with the new method: ```xml * * * ## Using Named Types The third design method defines classes or types, which allows us to reuse element definitions. The specific approach is: first, name the simple and complex elements, and then point to them via the element's `type` attribute. Here is the schema file ("shiporder.xsd") designed with the third method: ```xml The `restriction` element indicates that the data type is derived from a data type in the W3C XML Schema namespace. Therefore, the following snippet means that the value of the element or attribute must be a string: ```xml The `restriction` element is often used to impose constraints on elements. Look at these snippets from the schema above: ```xml This code indicates that the value of the element or attribute must be a string and must be exactly six consecutive characters, which must be digits from 0-9.
← Schema Dtypes StringXquery Reference β†’