Creating an XML Schema

The first step in creating the solution is to create an XML schema that you can attach and map to your Word document. The XML schema in Listing 5.13 represents a letter that contains six elements: Author, Addressee, Salutation, Re, Body, and Closing. Each of these elements is of type String and has its maxoccurs value set to 1. This means that you can map the XML element to the document only one time.

The Addressee node might be a good candidate for a repeating XML schema element because you can address a single letter to more than one person; however, for this example, you can add the name and address for each recipient within the Addressee elements. You can create schemas using Visual Studio, or you can manually add XML elements to a schema file.

Create a schema named Letter.xsd using the XML elements found in Listing 5.13. To create this file manually, type the XML elements into a file in Notepad, and then save the file with the name Letter.xsd.

Listing 5.13. Letter schema

<?xml version="1.0" encoding="utf-8" ?> <xs:schema targetNamespace=

"" elementFormDefault="qualified"




<xs:element name="Letter" type="LetterType"></xs:element> <xs:complexType name="LetterType"> <xs:all>

<xs:element name="Address" type="AddressType"

minOccurs="0" maxOccurs="1" /> <xs:element name="Content" type="ContentType" minOccurs="0" maxOccurs="1" />

<xs:complexType name="AddressType"> <xs:sequence>

<xs:element name="Author" type="xs:string"

minOccurs="0" maxOccurs="1" /> <xs:element name="Addressee" type="xs:string"

minOccurs="0" maxOccurs="1" /> <xs:element name="Re" type="xs:string" minOccurs="0" maxOccurs="1" /> </xs:sequence> </xs:complexType>

<xs:complexType name="ContentType"> <xs:sequence>

<xs:element name="Salutation" type="xs:string"

minOccurs="0" maxOccurs="1" /> <xs:element name="Body" type="xs:string"

minOccurs="0" maxOccurs="1" /> <xs:element name="Closing" type="xs:string" minOccurs="0" maxOccurs="1" /> </xs:sequence> </xs:complexType> </xs:schema>

The parent element, AddressType, is a complex type that contains three child elements. The XML is structured in this way so that you can easily determine when the user's cursor location is within a specific context. In this way, you can determine when the cursor is in a general area of the document rather than in a specific element, such as the Author element. The parent element ContentType was created for the same reason. As you will see in the next section, when you map the parent element, the three child elements also become available for mapping.

0 0

Post a comment