Creating an XML Schema with XML Designer

only for RuBoard

Chapter 2, "XML Schemas in .NET," gives you a detailed overview of schemas. In this section, you look into using Visual Studio .NET to create these schemas.

If you already have an XML file and you want to quickly create an XML Schema to represent the same data structure as this XML file, you can use the Create Schema option from the XML menu. This option adds an XML Schema ( .xsd file) to the current project with the same name as the original XML file. When you create a schema with this approach, all the data types are initially set to string so you must edit the data types according to the content requirements of your XML data. To make changes to this generated schema, you can either edit the schema file or load the XML Schema into XML Designer and perform the changes visually.

Although the previous approach might appear easy and quick for simple XML files, large and complex structures can be difficult to figure out the structure generated by the IDE. Another approach is to create the XML Schema yourself from scratch. This gives you more control over the schema design. This approach is discussed next .

To add an XML Schema to the project, choose Add New Item from the Project menu, select the XML Schema icon in the Add New Item dialog box (see Figure 4.10), and rename the file (if necessary).

Figure 4.10. Adding a new XML Schema file.
graphics/04fig10.gif

Adding a new schema file adds the .xsd file to the project and the XML Designer appears. By switching to the XML view, you'll notice the following XML with an empty schema generated:

 <?xml version="1.0" encoding="utf-8" ?>  <xs:schema id=" Customers" targetNamespace="http://tempuri.org/Customers.xsd"  elementFormDefault="qualified" xmlns="http://tempuri.org/Customers.xsd"  xmlns:xs="http://www.w3.org/2001/XMLSchema"></xs:schema> 

This XML doesn't include the actual declaration part of the schema, nor does it include the actual schema tags, which are called the root or document level tags. Notice that the default namespace http://tempuri.org/ is added by Visual Studio .NET IDE whenever it generates an XML Schema. You can enter the code and replace it with your custom namespace if required.

The XML Schema Tab in the Toolbox

The Schema tab in the Toolbox provides all the elements you can add to the XML Schema and the ADO.NET DataSets . These elements appear in a form similar to visual controls that are more common to Windows GUI developers. These elements can be dragged and dropped onto the XML Designer surface. This tab is available in the Toolbox when the XML Designer is in the Schema view. The Toolbox appears by default; if it's closed but you need it, you can display the Toolbox by selecting Toolbox from the View menu. To make the Toolbox close automatically, select Autohide from the Window menu. To make it stay open , you can clear the Autohide from the Window menu or click the small pushpin that appears on the status bar on top of the ToolBox. (See Figure 4.11.)

Figure 4.11. The XML Schema tab in the ToolBox.
graphics/04fig11.gif

Take a brief look at each of the elements that appear in the Toolbox Schema tab:

  • element Creates an element that can be global, added to other elements, added to groups, or used to construct complexTypes .

  • attribute Creates an attribute that can be global, added to elements, or added to groups.

  • attributeGroup Creates an attributeGroup that can be global, added to elements, or used in the construction of complexTypes .

  • complexType Creates a complexType to which you can add elements, attributes, attributeGroups , anys , and anyAttributes .

  • simpleType Creates a simpleType to which you can add facets.

  • group Creates groups that can be global, added to other groups, elements, or complexTypes .

  • any Creates an any element that can be added to elements, complexTypes , or groups.

  • anyAttribute Creates an anyAttribute element that can be added to elements, attribute groups, or complex types.

  • Facet Creates a facet that can be added to a simpleType .A facet can further restrict the definition of a simple type.

  • Key When dragged and dropped on an existing element, launches the Edit Key dialog box that can create keys when added to an element. Keys are the primary fields that tie relations together.

  • Relation Launches the Edit Relation dialog box that defines relationships between elements.

Alternatively, you can also add these elements to the designer by right-clicking the design surface and selecting Add from the pop-up menu.

Adding Simple Type Definitions

You will create a SimpleType element that's derived from the built-in derived type positiveInteger and represents a five-digit postal code. You'll use this element later to create a complex type named usAddress . To create a SimpleType element, follow these steps:

  1. From the XML Schema tab of the Toolbox, drag a simpleType onto the design surface. The element appears on the Design surface like a datagrid control.

  2. Select the default name simpleType1 and rename this type postalCode .

  3. Navigate to the adjacent cell on the right and click the drop-down list. You can see all the simple types built in to XML Schema. From this list, select positiveInteger .

  4. Navigate to the next row and click the drop-down box. You can see that the only choice is facet. Simple types cannot include elements or attributes as part of their content models; therefore, only facets can be used to build simple types.

  5. The drop-down list in the adjacent cell on the right lists all the constraining facets for the built-in simple type positiveInteger that was chosen in step 3. From this list, select Pattern.

  6. In the adjacent cell to the right, type \d{5} . The Pattern facet allows you to enter regular expressions. The regular expression \d{5} restricts the contents of the postalCode type.

Figure 4.12 shows the postalCode simple type at the end of the preceding steps.

Figure 4.12. The simple type postalCode .
graphics/04fig12.gif

If you switch to XML view, you can see the following simple-type definition that's added to the schema:

 <xs:simpleType name="postalCode">       <xs:restriction base="xs:positiveInteger">            <xs:pattern value="\d{5}" />       </xs:restriction>  </xs:simpleType> 

Now you can similarly create another simple type named stateCode , as shown in Figure 4.13. You'll use this during the complex type creation, which is discussed next.

Figure 4.13. The simple type stateCode .
graphics/04fig13.gif

Adding Complex Type Definitions

In this section, you'll create a ComplexType element named usAddress that defines the structure of an address located in U.S.

To create a complex type, follow these steps:

  1. From the XML Schema tab in the Toolbox, drag a complexType onto the design surface.

  2. Select the default name complexType1 and rename this type to usAddress . Do not select a data type for this element because you do not want to derive this from any other complex or simple type. Note: Selecting the data type as another simple type restricts the complex type to include only the element of type anyAttribute , attribute , attributeGroup, and facet .

  3. Navigate to the next cell and click the drop-down box. You can see the many choices of elements that you can add to a complex type, as shown in Figure 4.14. You can select element. (You can even Tab over this cell because element is the default.)

    Figure 4.14. Selecting the element type.
    graphics/04fig14.gif
  4. In the adjacent cell on the right, type the name as Street and leave the type selection in the next cell as string , which is the default.

  5. Repeat steps 3 and 4 to create the other elements, City , State , and Zip , as shown in Figure 4.15. For the State and Zip elements, you can select the type as the simple types that were just defined.

    Figure 4.15. The complex type usAddress .
    graphics/04fig15.gif

By switching to XML view, you can see the following complex type definition added to the schema:

 <xs:complexType name="usAddress">       <xs:sequence>            <xs:element name="Street" type="xs:string" />            <xs:element name="City" type="xs:string" />            <xs:element name="State" type="stateCode" />            <xs:element name="Zip" type="postalCode" />       </xs:sequence>  </xs:complexType> 

You might see the prefix xsd: rather than xs: in the rest of this chapter. This is due to a previous build of Visual Studio .NET that used xsd per the convention. But it is only essential that, irrespective of the name used, the namespace should be associated to the URI of http://www.w3.org/2001/XMLSchema.

You can create the two complex types, contactType and customerType ,as shown in Figure 4.16 and Figure 4.17.

Figure 4.16. The complex type contactType .
graphics/04fig16.gif
Figure 4.17. The complex type customerType .
graphics/04fig17.gif

In the Schema Designer view, you can only set the name and type attributes of the elements in the schema definition. To set the other attributes, you can use the Properties window. In the Schema view, if you place the cursor on a specific element, the Properties window shows all the attributes conforming to the W3C Schema definition standard. This greatly eases your job because you do not have to remember these standard attributes. If you are in XML view, you must keep the cursor within the element's angular brackets to see the attributes that correspond to a specific element. Figure 4.18 shows the Properties window for the <xsd:attribute> element.

Figure 4.18. The Properties window for an attribute element.
graphics/04fig18.gif

Now that you have seen the creation of some simple and complex type definitions, one final step completes your schema creation. You must create the elements that will define the data. To do that, follow these steps:

  1. From the XML Schema tab of the Toolbox, drag an element to the design surface.

  2. Select the default name element1 and rename this type to Customers .

  3. Tab over the adjacent cell, leaving the default type as Customers , and navigate to the next row.

  4. Type the name of the element in this row as Customer . Select the name Customer and, in the Properties window, set the maxOccurs property to unbounded.

  5. In the adjacent cell on the right, select the type as customerType .

This finishes the schema creation. Figure 4.19 shows the relational structure in the XML Designer. You can get a clean appearance in the Schema view by clicking the Auto-Arrange option under the Schema menu. Clicking the Move symbol in the lower-right corner of the designer opens a bird's-eye view of the schema for a glance of the designer surface.

Figure 4.19. The XML Designer showing the relational structure.
graphics/04fig19.gif

The XML Designer enables you to cut or copy an element and paste it in the same designer surface, or paste it in a different schema's designer surface.

No Undo Command

The XML Designer provides the Delete option, but doesn't provide the Undo command in Schema view. Therefore, it is essential that you plan your steps and, if you are doing a more complex schema design, maintain backups at regular intervals. Doing so helps you in case you want to revert to the changes.

Editing the Generated XML

By switching to XML view, you can see the following complete code that has been added to the Customers.xsd file:

 <?xml version="1.0" encoding="utf-8" ?>  <xsd:schema id="Customers" targetNamespace="http://tempuri.org/Customers.xsd"  xmlns="http://tempuri.org/Customers.xsd"  xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas microsoft-com:xml-msdata" attributeFormDefault="qualified"  elementFormDefault="qualified">       <xsd:element name="Customers" >            <xsd:complexType>                 <xsd:sequence>                      <xsd:element name="Customer" type="customerType" graphics/ccc.gif maxOccurs="unbounded"/>                 </xsd:sequence>            </xsd:complexType>       </xsd:element>       <xsd:complexType name="customerType">            <xsd:sequence>                 <xsd:element name="CompanyName" type="xsd:string" minOccurs="0" />                 <xsd:element name="Contact" type="contactType" minOccurs="0" />                 <xsd:element name="Email" type="xsd:string" minOccurs="0" />                 <xsd:element name="Phone" type="xsd:string" minOccurs="0" />                 <xsd:element name="ShippingAddress" type="usAddress" minOccurs="0" />                 <xsd:element name="BillingAddress" type="usAddress" minOccurs="0" />            </xsd:sequence>            <xsd:attribute name="id" type="xsd:string" form="unqualified" />       </xsd:complexType>       <xsd:simpleType name="stateCode">            <xsd:restriction base="xsd:string">                 <xsd:length value="2" />            </xsd:restriction>       </xsd:simpleType>       <xsd:simpleType name="postalCode">            <xsd:restriction base="xsd:positiveInteger">                 <xsd:pattern value="\d{5}" />            </xsd:restriction>       </xsd:simpleType>       <xsd:complexType name="usAddress">            <xsd:sequence>            <xsd:element name="Street" type="xsd:string" minOccurs="0" />            <xsd:element name="City" type="xsd:string" minOccurs="0" />            <xsd:element name="State" type="stateCode" minOccurs="0" />            <xsd:element name="Zip" type="postalCode" minOccurs="0" />            </xsd:sequence>       </xsd:complexType>       <xsd:complexType name="contactType">            <xsd:sequence>                 <xsd:element name="FirstName" type="xsd:string" minOccurs="0" />                 <xsd:element name="MiddleName" type="xsd:string" minOccurs="0" />                 <xsd:element name="LastName" type="xsd:string" minOccurs="0" />                 <xsd:element name="Title" type="xsd:string" minOccurs="0" />            </xsd:sequence>       </xsd:complexType>  </xsd:schema> 

You can edit this XML-generated code to add or remove any elements (if required). You can finally validate the schema by selecting Validate Schema from the Schema menu.

only for RuBoard


XML and ASP. NET
XML and ASP.NET
ISBN: B000H2MXOM
EAN: N/A
Year: 2005
Pages: 184

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