XML Schema Design

This section introduces you to XML schemas from two perspectives. First, the section delivers an overview of selected schema design issues, particularly those pertaining to the specification of DataTable objects for holding the contents of tables and queries from an Access database. As you learned in the Schemas for Typed and Untyped Datasets section in Chapter 9, schemas can define datasets. Next , this section shows you how to create, edit, and use a schema with an XML document, such as one created in the previous section.

Schema Design Issues

XML schemas are to XML documents as database schemas are to databases. A schema is data about data. Programmers typically refer to this kind of data as metadata. The metadata defining any given XML schema defines the structure of an XML document. For example, a schema specifies the case for the name of elements and attributes. You can also use a schema to define primary and foreign keys for an XML document representing the contents of a collection of DataTable objects in a dataset.

An XML schema is an XML document. Therefore, the syntax conventions that you learned earlier in the discussion of XML documents apply to XML schemas. An XML document that is an XML schema must reference the http://www.w3.org/2001/XMLSchema namespace. This namespace defines the XML Schema Definition (XSD) elements, attributes, and types that you can use in an XML schema. The XML schema, in turn , can define the elements, attributes, and types for an XML document. Element definitions within an XML schema can be simple or complex. Within a schema, you can specify simple and complex elements with the simpleType or complexType tags. A simpleType element has neither a child element nor an attribute. In addition, a simpleType element always relies on one of the built-in XSD types. Simple types allow you to set attribute values. For example, a simple type can be a variation based on a built-in string type. You can use your custom simple type to make a value setting for the string s maximum length by using its maxlength setting. A complexType element has at least one child element or at least one attribute. A complex type defines a custom (not built-in) type specification for an XML schema. A complex type can contain one or more simple type elements along with one or more attributes.

Note  

An element in an XML document can have child, sibling, or parent relations to one or more elements in an XML document.

Visual Studio .NET includes tools for automating the creation of an XML schema. Two Visual Studio .NET resources can help you start a schema graphically: you can use a form or the Dataset Designer. The Jump Start: A DataGrid Sample for the Northwind Shippers Table section in Chapter 2 illustrates the creation of an XML schema (known as a dataset ) from a form, and the Graphically Creating a Dataset with Two DataTable s section in Chapter 9 introduces the Dataset Designer. One of the most common uses for the Dataset Designer is to construct a new schema or edit an existing one for a dataset derived from one or more tables and queries in an Access database.

Note  

Because a typed dataset is an XML schema, the Dataset Designer also facilitates the design of XML schemas. For this reason, you can think of the Dataset Designer as an XML schema designer.

Learning about XML schemas for XML documents that you create in the XML Designer gives you hands-on experience with schema design issues. This experience can help you understand how to read and edit an automatically generated schema based on Access database objects. When using an XML schema with an XML document that you entered yourself, the schema is useful for validating the data in the XML document. In order to validate an XML document against an XML schema, the XML document must reference the XML schema. You can launch validation by choosing the XML, Validate XML Data command within the XML Designer.

Creating, Editing, and Using an XML Schema

To create an XML schema for the XMLFile1.xml document, double-click the document in Solution Explorer. This opens the XML document in the XML Designer. From the XML Designer menu, choose XML, Create Schema. This adds a new file, XMLFile1.xsd, to Solution Explorer. Double-click the .xsd file to open the automatically generated XML schema in the Dataset Designer window. Two tabs in the window s lower left corner enable you to toggle between the Dataset view and the XML view of the XML schema document named XMLFile1.xsd. The Dataset view is graphical, and the XML view is text based.

Choosing View, Toolbox opens a collection of controls that you can use to edit your schema design. Because the XMLFile1.xml document is just a text file, the wizard for creating the schema has no information about special data types or keys that you might want for an XML document. Because XMLFile1.xml is based on the Shippers table, we know that ShipperID should be an integer type that is a primary key. However, the ShipperID element initially appears with a string type, and the element does not serve as a primary key for the document. (See Figure A-2.) Drag the Key tool from the Toolbox to the ShipperID field in the Dataset view of the XML document. This opens the Edit Key dialog box with a suggested name for the key ( RootKey1 ) and with ShipperID as the selected field for building the primary key index. Select the Dataset Primary Key check box to designate ShipperID as the element on which the document builds a primary key. Leave Nullable unselected because primary keys must be unique. Then, click OK. Once Visual Studio .NET returns control to the Dataset Designer, successively click the right side of the cell with the string to the right of the ShipperID element name until a list box opens. Use the list box to select another type. Choose integer as the type from the box. This completes two settings that you can make in the Dataset view.

click to expand
Figure A-2: Dataset view of the XMLFile1.xsd schema before any editing

Click the XML tab to continue viewing and editing the schema. The initial XML schema appears next with wrapping to make it easier to view. This view shows the XML schema for defining the XMLFile1.xml document. Notice that the target namespace for the schema is http://tempuri.org/XMLFile1.xsd . XML documents using this schema must refer to that namespace. Also, notice the references to the xmlns:xs and xmlns: msdata namespaces. These namespaces are the building blocks for the structure of the XMLFile1.xsd schema, much as the XMLFile1.xsd schema is the language for specifying the structure of the XMLFile1.xml document.

 <?xml version="1.0" ?> <xs:schema id="Root" targetNamespace="http://tempuri.org/XMLFile1.xsd" xmlns:mstns="http://tempuri.org/XMLFile1.xsd" xmlns="http://tempuri.org/XMLFile1.xsd" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" attributeFormDefault="qualified" elementFormDefault="qualified"> <xs:element name="Root" msdata:IsDataSet="true" msdata:EnforceConstraints="False"> <xs:complexType> <xs:choice maxOccurs="unbounded"> <xs:element name="Shippers"> <xs:complexType> <xs:sequence> <xs:element name="ShipperID" type="xs:integer" minOccurs="0" /> <xs:element name="CompanyName" type="xs:string" minOccurs="0" /> <xs:element name="Phone" type="xs:string" minOccurs="0" /> </xs:sequence> </xs:complexType> </xs:element> </xs:choice> </xs:complexType> <xs:key name="RootKey1" msdata:PrimaryKey="true"> <xs:selector xpath=".//mstns:Shippers" /> <xs:field xpath="mstns:ShipperID" /> </xs:key> </xs:element> </xs:schema> 

In the Shippers table, the CompanyName field is required. In addition, the column serving as the index column for the primary key cannot include nulls. Therefore, the elements corresponding to both of these fields can have their minOccurs setting changed from 0 to 1. Only the Phone column value is optional in the original Northwind database version of the Shippers table. The Phone element consequently can retain its minOccurs setting of 0. However, the maximum length settings (in number of characters ) for the CompanyName and Phone fields are 40 and 24, respectively. XML schema syntax permits you to designate this kind of maximum length constraint for a string type with a restriction element nested within a simpleType element nested within the original built-in element. Within the restriction element, you can specify a maxLength constraint or facet for the number of characters. The following excerpt shows the revised formatting for the ShipperID , CompanyName , and Phone elements. The Dataset Designer offers IntelliSense to help with syntax construction, and you can choose Schema, Validate Schema to validate your schema syntax after redesigning it.

 <xs:element name="ShipperID" type="xs:integer" minOccurs="1" /> <xs:element name="CompanyName" minOccurs="1"> <xs:simpleType> <xs:restriction base="xs:string"> <xs:maxLength value="40" /> </xs:restriction> </xs:simpleType> </xs:element> <xs:element name="Phone" minOccurs="0"> <xs:simpleType> <xs:restriction base="xs:string"> <xs:maxLength value="24" /> </xs:restriction> </xs:simpleType> </xs:element> 

After verifying your redesigned XML schema, you can close it. Then, open the document that the schema describes. Double-click XMLFile1.xml in Solution Explorer to open the XML document in the XML Designer. Notice that a new attribute appears in the Root tag. This attribute designates the namespace for the XMLFile1.xsd schema. The namespace reference indicates that all elements and values in the XML document must conform to the definitions in the schema. Choose XML, Validate XML Data to validate the XML structure relative to the specifications in the schema. If the data you initially entered is valid, you will see a message in the status bar saying that no validation errors were found. Next, change the opening and closing Phone tags for the last shipper from Phone to phone . Because elements are case sensitive, doing so is an error in the specification of the document. If the Task List window is open, an error message (shown in Figure A-3) will appear in it immediately after you change the first tag. The default location for the Task List window is below the XML Designer window. By choosing XML, Validate XML Data, you can generate additional diagnostic information in the window. Without the reference to the XMLFile1.xsd schema, this faulty modification would not raise any error signals. Fix the Phone tags for the last shipper and close the XMLFile1.xml document.

click to expand
Figure A-3: XML view of the XMLFile1.xml document, with a reference to the XMLFile1.xsd schema, after making an edit to generate an error
 


Programming Microsoft Visual Basic. NET for Microsoft Access Databases
Programming Microsoft Visual Basic .NET for Microsoft Access Databases (Pro Developer)
ISBN: 0735618194
EAN: 2147483647
Year: 2006
Pages: 111
Authors: Rick Dobson

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net