XML Tutorial 4: Working with Namespaces and Schemas
Creating a Patient Report Document
Additional Topics
- How to use XML Spy to generate schema from an XML document
- Manuscript schema language
- Importing content from one schema file into another
How to use XML Spy to generate schema from an XML document
A schema is an XML document that defines the content and structure of one or more XML documents. The XML document containing the content is called the instance document because it represents a specific instance of the structure defined in the schema. A schema is always placed in a separate XML document that is referenced by the instance document. Because schemas are XML documents, they follow the same structure as other XML documents.
An XML schema defines the elements that can appear within an XML document and the attributes that can be associated with an element. It also defines the structure of the document, for example, which elements are child elements of others, the sequence in which the child elements can appear, and the number of child elements. The www.xml.com site is an excellent place to learn about the latest issues regarding schemas. (Their schema tutorials are excellent)
Validating documents using schema can be challenging, as the application that validates schema often can only validate XML documents created with those applications. This is the primary difficulty with schema that have prevented many developers from leaving DTDs. Consider the “Russian Doll”, “flat catalog” and “Venetian blind” designs. It is very possible that one editor will create a style for a schema document, validate the XML file against it, and have the schema found to be inaccurate in another editor. Practice and dissection of files from the listed tutorials in this section are invaluable for understanding the nuances of schema construction and validation.
XML Spy includes an advanced graphical XML Schema editor that allows in-place editing of all the major aspects of an XML Schema in the graphical schema representation. When you open an XML Schema document, XML Spy displays all globally defined elements in XML Schema as a list in XML Schema Design View. XML Spy also validates the XML document content, to determine if the XML document is a valid instance of the grammar expressed by the XML schema. For a more complete picture of XML schemas generated by XML Spy, please read the article at http://xmlschema.xmlspy.com/ . The XMLSpy.com website contains a lot of useful information, which will help you gain a deeper understanding of XML Spy.
You can use the XML Spy command to generate a new Schema from an XML document (or from a set of documents in the project window), as shown in Figure 2. Otherwise, you can create a schema from scratch as shown in Figure 3. Figure 3 is the Schema overview; it displays the global components in the top window and the attributes of the currently selected component, in the lower one.
Figure 2
To create a new schema file:
- Start XML Spy by double clicking on the XML Spy icon.
- Select the menu option File | New and select the .xsd W3C Schema entry from the dialog and confirm with OK.
An empty schema file appears in the main window. You are prompted to enter the name of the root element. - Click in the highlighted field and enter “Company”.
Company is now the “root” element of this schema. - Define your own namespace. Select the menu option Schema Design | Schema settings. Click the Target namespace radio button, and enter http://my-company.com/namespace. Confirm by clicking OK.
- Add elements to a schema.
Right click the Company element to open the context menu, and select Add Child | Sequence. This inserts the Sequence compositor, and defines that the following elements must appear in the same sequence (in the XML document).
Right click the Sequence compositor and select Add Child | Element. Enter “Address” as the name of the element.
Right click the Sequence compositor again, select Add Child | Element, and enter “Person” as the name of the element.
Right click the Person element, and select Unbounded from the context menu. The Person element changes at this point, showing the range in which it can occur, in this case 1 to infinity. You can also edit the minOcc and maxOcc fields in the Details entry helper directly. - Add the sub-elements which define the address structure.
Right click the Address element to open the context menu, and select Add Child | Sequence.
Right click the Sequence compositor, and select Add Child | Element. Enter “Name” as the element name. Define element parameters that the Name element is to occur only once, and contain only textual data. All data can be edited directly in the Details window.
Figure 3
Manuscript schema language
XML language has several schema “dialects”. Each dialect has distinct advantages and disadvantages. The two most prominent dialects are XML Schema and XDR. XML Schema was developed by the W3C in March 2001. This dialect is very close to becoming a schema standard. A schema file written in the XML Schema dialect typically ends with the .xsd file extension. You can learn more about XML Schema by visiting the http://www.w3.org/XML/Schema . The W3C site contains specifications for all versions of XML Schema as well as the latest proposals for future modifications.
Microsoft's schema, XDR, was the first schema to be developed. XML-Data Reduced (XDR) language is a subset of ideas described in the XML-Data specification. The schema implementation in Microsoft® XML Parser (MSXML) is based on the XML-Data Reduced language specification. XDR schemas are usually stored in files with the .xdr file extension. For more information about XDR schemas read the article at the Microsoft Web Site .
Importing content from one schema file into another
You can combine several XML schema documents into a single file. With schemas, each part of the combined document can be associated with a different namespaces and different schemas. A namespace is a defined collection of related element and attribute names. Names that belong to the same namespace must be unique, but elements can share the same name as long as they reside in different namespaces. Every schema file is actually a combined document, combining the elements of the XML Schema namespace (schema of schemas) and the elements of the schema namespace. You can read more about namespaces at http://www.w3.org/TR/REC-xml-names/ .
To attach the schemas to the different parts of the combined document, you must do the following:
- add the XML Schema-instance namespace to the document's root element,
- assign namespaces to the different parts of the combined document,
- add the “schemaLocation” attribute to the parent element of each part. You must also declare the imported schema as qualified, and declare each imported namespace with a URL and schema location. This is a powerful method for quickly integrating complex namespace element and attribute definitions. Microsoft provides useful information about the importing schema content at: http://
msdn.microsoft.com/library/default.asp?url=/library/en-us/
cpguide/html/cpconworkingwithxmlnamespaces.asp