Walkthrough: Creating an XML Schema with the XML Designer
来源:互联网 发布:装修工程报价软件 编辑:程序博客网 时间:2024/05/17 02:09
In this walkthrough you will create an XML purchase order schema as part of a Windows Application project. The walkthrough will consist of three main sections:
- Creating a Windows Application project and adding an XML Schema.
- Creating a relational table:
- Adding and defining a new simpleType object.
- Adding and defining a new complexType object.
- Adding and defining a new Element object.
- Editing generated XML using the XML editor.
Creating a Windows Application and Adding an XML Schema
To create a new Windows Application project
- From the File menu, point to New, and then click Project to display the New Project dialog box.
- Depending on what language you want to use, select Visual C# Projects or Visual Basic Projects in the Project Types pane, and then select Windows Application.
- Name the project SampleSchema.
To add an XML Schema to the project
- From the Project menu, choose Add New Item, then double-click the XML Schema icon in the Add New Item dialog box.
The XML Designer appears.
Defining Simple and Complex Types
Before constructing the relational table, you will first build simple and complex type definitions that you will use to format specific elements of the purchase-order schema. The new types are built using existing XML data types, such as string and integer.
First you will define a simple type, which will be named stateCode. This simple type will be used to limit the size of a string to two characters.
To add an XML simpleType to the project
- If not already open, double click the XMLSchema1.xsd file to bring up the XML Designer.
- Click the XML Schema tab of the Toolbox and drag a simpleType onto the design surface.
- Change the name of the simpleType by clicking the first text box in the header and replacing simpleType1 with stateCode.
- Set the base type for the stateCode type by clicking the drop-down list in the header and selecting string.
- Navigate to the first cell in the next row by pressing the TAB key.
- Select facet from the drop-down list.
- Press TAB to go to the next cell and select length from the drop-down list.
- TAB to the third cell of the same row, enter the value 2.
This requires that the value entered into the State field be two characters.
Your stateCode should look like this in Schema view:
- Click the XML tab at the bottom left of the XML Designer to see the XML that has been added:
<xs:simpleType name="stateCode"> <xs:restriction base="xs:string"> <xs:length value="2" /> </xs:restriction></xs:simpleType>
This stateCode
simple type will be used to define the State
element within the complex type you will create in the next section.
The complexType named addressType
defines a set of elements that will appear in any element typed as addressType. For example, a billTo
element will include information on names, addresses, and dates when its type is set to the previously defined addressType. By constructing the complex type and using it within an element, you are generating a nested relationship. For more information, see Creating Complex XML Types.
To add an XML complexType to the project
- Click the Schema tab of the XML Designer.
- Drag a complexType from the XML Schema tab of the Toolbox onto the design surface.
- Change complexType1 to addressType to name the type.
- Add an XML attribute to addressType by clicking the first cell of the first row and selecting element from the drop-down list.
- In the second column, change element1 to Name.
- In the third column, accept the default value of string.
- Add the following XML elements and set their names and types as follows:
Element name Data type Street string City string State stateCode PostalCode integer Your addressType should look like this in Schema view:
- To see the XML that has been added to your .xsd file, click the XML tab at the bottom of the designer. You will see the following XML:
<xs:complexType name="addressType"> <xs:sequence> <xs:element name="Name" type="xs:string"/> <xs:element name="Street" type="xs:string"/> <xs:element name="City" type="xs:string"/> <xs:element name="State" type="stateCode"/> <xs:element name="PostalCode" type="xs:integer"/> </xs:sequence></xs:complexType>
Creating a Relational Table
When you drag the element object from the Toolbox to the design surface, you are really adding an element containing an unnamed complexType. Including the unnamed complex type defines the element to be a relational table. Additional elements can then be added under the complexType to define the relation fields (or columns). If you define one of these new elements to be a new unnamed complexType, you are creating a nested relation inside of the parent relation with its own unique columns. For details see Tables, Columns, Keys, and Constraints in XML Schemas.
Defining new unnamed complex type elements within the PurchaseOrder or Items element creates additional nesting in the schema. Within one purchase order there can be many Items, and within each Item, many additional elements (such as price, size, and so on). In the following procedure, an element Items is added to the purchaseOrder relational table and typed as an unnamed complexType. Because you are defining a new relational table, this causes a new element to appear on the design surface. Within the new items relation, adding the item element and setting its type to unnamed complexType, creates another relational table, which also appears on the design surface.
To add an XML element to the project
- Click the Toolbox and from the XML Schema tab drag an element object onto the design surface.
- Change element1 to PurchaseOrder to name the element. You can leave the data type as (PurchaseOrder).
- Add an element to the purchase order by clicking the first cell of the first row and selecting element from the drop-down list.
- Name the element shipTo and set its data type to addressType.
- Add the following XML elements and set their names and types as follows:
Element name Data type billTo addressType shipDate date Items unnamed complexType When you type the Item element to be anonymous, an additional element is added to the design surface, which is another relational table.
- In the Items element, add an element, name it Item, and set its type to Unnamed ComplexType.
Your purchase order should look like this in Schema view:
The following XML has now been added to your .xsd file:
<xs:element name="PurchaseOrder"> <xs:complexType> <xs:sequence> <xs:element name="shipTo" type="addressType"/> <xs:element name="billTo" type="addressType"/> <xs:element name="shipDate" type="xs:date"/> <xs:element name="Items"> <xs:complexType> <xs:sequence> <xs:element name="Item"> <xs:complexType> <xs:sequence /> </xs:complexType> </xs:element> </xs:sequence> </xs:complexType> </xs:element> </xs:sequence> </xs:complexType></xs:element>
Editing XML
You can use the XML tab of the XML Designer to edit the XML that was generated when you added elements and types to the designer surface. The XML editor features IntelliSense and statement completion. An invalid statement is tagged with a red wavy line. Mousing over the incorrect statement causes an error message to appear.
To edit XML
- Click the XML tab of the XML Designer to view the XML.
- Within the
Item
element, change the self-closing tag (<xs:sequence />
) into separate opening and closing tags (<xs:sequence></xs:sequence>
). - After the
<xs:sequence>
tag in theItem
element, type the following:<xs:element name="Quantity" type="xs:integer"/><xs:element name="Price" type="xs:decimal"/><xs:element name="ProductID" type="xs:integer"/>
You have created three new elements — Quantity, Price, and ProductID — and defined data types for each.
- Next type <invalid/> and note the red wavy line indicating an error. Mouse over the red wavy line to see an error message. Errors will appear in the Task List as well.
- Delete the <invalid/> tag to fix the error.
- Save the schema.
The XML beneath the
Items
element should now look like the following in XML view:<xs:element name="Items"> <xs:complexType> <xs:sequence> <xs:element name="Item"> <xs:complexType> <xs:sequence> <xs:element name="Quantity" type="xs:integer"/> <xs:element name="Price" type="xs:decimal"/> <xs:element name="ProductID" type="xs:integer"/> </xs:sequence> </xs:complexType> </xs:element> </xs:sequence> </xs:complexType></xs:element>
- Walkthrough: Creating an XML Schema with the XML Designer
- XML Document Validation with an XML Schema by Deepak Vohra
- Validate a JAXB Object Model With an XML Schema
- XML Schema Validation with JAXP
- Creating schema using Saiku Schema Designer
- The XML schema complete reference
- Error creating bean with name 'XXX' in the [XXXX.xml] --The import XXX cannot be resolved
- Walkthrough: Word 2007 XML 格式
- Walkthrough: Word 2007 XML 格式
- Validation with Java and XML Schema
- W3C Schema Validation With XML::LibXML
- XML Schema
- XML Schema
- XML Schema
- xml schema
- XML Schema
- XML Schema
- xml schema
- java环境
- 捕捉屏幕的时候包含鼠标
- redhat linux 9.0 VSFTP配置大全
- 关于C++复杂性的零碎思考
- 来吧,移动开发
- Walkthrough: Creating an XML Schema with the XML Designer
- Walkthrough: Creating COM Objects with Visual Basic .NET
- 如何用Delphi编写自己的可视化控件
- Winter Fun Pack 2004 for Windows XP
- DOS 批处理文件
- 在 DOS 下也能访问网上邻居
- DEBUG 命令详解
- 『时尚资讯』 [家居美食]超赞的美食摄影+作法(转载)
- 补面试题