FREE BOOK

Chapter 7: Web Matrix and XML

Posted by Apress Free Book | XML .NET January 13, 2009
In this chapter Use the XML Support ,XML Notepad ,XMLEditGrid Control,XML File Page,XSL Transform Page,XML Schema Page and XML Data Display Application

Using the XML Schema

Databases require a schema to define their content. The tables within the database have columns that contain specific types of data. Each entry has a name, such as LastName, for the last name entry in a contact management database. The entry has a data type such as text, a length, and other characteristics that define that entry. An application relies on these entries to interact with the database. For example, the entry will tell the application whether an edited entry is too long or of the wrong type. All of this information helps create a reliable flow of information between the database and the application.

TIP Most DBMSs will create XSD files for you. However, if you find that you have to create such a file by hand, make sure you use the standards-recognized method.One of the best places to learn about the XSD file format is the XML Schema page on the W3C site at
http://www.w3.org/XML/Schema .You can find a few coding examples on the Got Dot Net site at  http://samples.gotdotnet.com/quickstart/howto/doc/Xml/XmlReadWriteSchema.aspx that will help you understand how XSD works.

In order for XML applications to provide the same level of support that their desktop counterparts do, they need some form of schema to use with the XML data they receive from a remote location. The XSD file contains the information that a desktop application would normally obtain using the database schema. As with everything else in this chapter, the data appears in XML format. In fact, many of the XML editors on the market also provide direct support for XSD files. Figure 7-10 shows an example of an XSD file that I created for the Movie.XML file mentioned elsewhere in this chapter. (You'll probably want to open a copy of this file in XML Notepad as well.)



Figure 7-10. Create an XSD file such as this one to describe the format of any table you produce.

We won't look at the source code for this file because it's relatively large. However, you should notice a few things in the XML Notepad display. For example, you'll find a number of namespace entries at the top of the file. Each <xlmns> tag defines a particular namespace used within the file or by applications. In a few cases, the application will ignore the namespace for now, but might use it in the future.

Immediately below the headers, we begin creating the data set. The first element is the data set name, MovieDS. The file contains some settings for the data set and then names the first (and only) table, ExistingMovies. Beneath the table entry are some more settings and, finally, the field names, each with their settings. In short, this is a complete description of a data set used to access the ExistingMovies table in a database.

Now that you've seen a schema for a data set, let's concentrate on something a little simpler. As previously mentioned, creating an XML Schema page will generate an XSD file with the opening and close tags, along with a few namespace entries. You have to generate all of the other code required to define the schema you want to create. Listing 7-5 shows a simple XSD file that defines a schema for a contact entry database.

Listing 7-5. A Simple XSD File

<?xml version=
"1.0" encoding="utf-8" ?>
<xsd:schema targetNamespace="http://tempuri.org/Simple.xsd"
xmlns="http://tempuri.org/Simple.xsd"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <xsd:annotation>
    <
xsd:documentation xml:lang=
"en">
      A simple XSD file used to demonstrate schemas.
      This is a contact management database.
    </xsd:documentation>
  </
xsd:annotation
>
  <
xsd:element name=
"ADatabase">
    <xsd:complexType>
      <
xsd:element name=
"MyContacts">
        <xsd:complexType>
          <
xsd:sequence
>
            <
xsd:element name=
"name" type="xsd:string"/>
            <xsd:element name="street" type="xsd:string"/>
            <xsd:element name="city" type="xsd:string"/>
            <xsd:element name="state" type="xsd:string"/>
            <xsd:element name="zip" type="xsd:string"/>
            <xsd:element name="telephone" type="xsd:string"/>
          </xsd:sequence>
        </
xsd:complexType
>
      </
xsd:element
>
    </
xsd:complexType
>
  </
xsd:element
>
</
xsd:schema>

This example is simplified, but it demonstrates essential features a schema would require. You need to define the relevant namespaces to begin. Adding some annotation helps anyone who uses the schema you create. Finally, the main section of this schema describes the database. The database name is ADatabase. It contains a single table called MyContacts. The MyContracts table includes a number of text fields that describe the contacts for this person. Figure 7-11 shows how this schema appears in XML Notepad.



Figure 7-11. Defining even a simple schema can become code intensive.

Total Pages : 8 45678

comments