For more information, see the BizTalk hands-on experiment series directory.
BizTalk Development Series
The role of XML Schema is to define the legal building module of XML documents. In the development process, you sometimes need to make precise constraints on XML documents. To ensure the accuracy of XMl data.
Today, we will take a class Sample to discuss how to make precise constraints on XML data during the development of BizTalk Schema. This Schema defines a class. There are members under the class. The members include the Name attribute, Mobile, City, Result, Comment, and other fields.
Based on the above scenarios, we will analyze the following requirements for different attributes or elements:
Field |
Type |
Function |
Base Class |
Constraints |
Name |
Attribute |
Name |
String |
2-4 characters |
Mobile |
Element |
Mobile phone number |
String |
11 digit characters |
City |
Element |
City |
String |
List: Beijing, Chengdu, Shenzhen |
Result |
Element |
Score |
Int |
Number list, separated by spaces. |
Comment |
Element |
Comment |
Int/string |
Numeric or string type |
Create a Schema file, as shown in. Set the attribute values of corresponding fields based on Requirement Analysis. But before that, let's take a look at the corresponding concepts. The Schema constraint uses a derived class to specify the value types derived from or from the corresponding field. There are four options for the derived type of a field or attribute:
1.DefaultDo not derive from any type.
2.RestrictionDerive a new restricted data type from a simple type
3.ListA single set of simple types (can be understood as an array)
4.UnionA set of multiple simple types
After reading the above concepts, let's take a look at the differences between these settings. Now let's take a look at setting attribute values for the corresponding fields. First, open the Property Window of the Name attribute field. As shown in. There are four options in the Derived By value drop-down box of the Advanced category. The Name attribute field is limited to 2-4 characters. Therefore, set the value to Restriction. In the Restriction category, set the values of Maximum Length and Minimum Length to 4 and 2 respectively.
Because Mobile is a numeric string and has a length limit. If it is set just like the Name attribute, it cannot meet the constraints. The W3C Schema Specification provides pattern constraint ). In the Properties window of the Mobile field, set the Derived By attribute value to the Pattern attribute of Restriction under the Restriction (Restriction) category. You can enter a constraint Statement (Regular Expression supported ). In the open edit window, enter [0-9] {11} Regular Expression statement. The limit string consists of 11 0-9 numbers.
The value of City must be restricted by the list. The Schema specification also provides the enumeration constraint mode. In the Properties window of the City field, set the Derived By attribute value to Restriction. Enter the value shown in the Enumeration attribute editing window under the Restriction (Restriction) category.
Result is actually an array of numerical values. As shown in, set the Derived By attribute value to List. Set Item Type to xs: int.
Comment field is not sure whether the obtained value is of the string or numerical type (it is possible to get a Comment or a score ). Therefore, it is a compound field.
The Schema definition process has been completed through the preceding settings. The following code is obtained in the Schema Soruce window:
<? Xml version = "1.0" encoding = "UTF-16"?>
<Xs: schema xmlns: B = "http://schemas.microsoft.com/BizTalk/2003" xmlns = "http://schemasample.limit"/targetNamespace = "http://schemasample.limit"/xmlns: xs = "http://www.w3.org/2001/XMLSchema">
<Xs: element name = "Class">
<Xs: complexType>
<Xs: sequence>
<Xs: element name = "Member">
<Xs: complexType>
<Xs: sequence>
<Xs: element name = "Mobile">
<Xs: simpleType>
<Xs: restriction base = "xs: string">
<Xs: length value = "11"/>
<Xs: pattern value = "[0-9] {11}"/>
</Xs: restriction>
</Xs: simpleType>
</Xs: element>
<Xs: element name = "City">
<Xs: simpleType>
<Xs: restriction base = "xs: string">
<Xs: enumeration value = "Beijng"/>
<Xs: enumeration value = "Chengdu"/>
<Xs: enumeration value = "Shenzhen"/>
</Xs: restriction>
</Xs: simpleType>
</Xs: element>
<Xs: element name = "Result">
<Xs: simpleType>
<Xs: list itemType = "xs: int"/>
</Xs: simpleType>
</Xs: element>
<Xs: element name = "Comment">
<Xs: simpleType>
<Xs: union memberTypes = "xs: int xs: string"/>
</Xs: simpleType>
</Xs: element>
</Xs: sequence>
<Xs: attribute name = "Name" use = "required">
<Xs: simpleType>
<Xs: restriction base = "xs: string">
<Xs: minLength value = "2"/>
<Xs: maxLength value = "4"/>
</Xs: restriction>
</Xs: simpleType>
</Xs: attribute>
</Xs: complexType>
</Xs: element>
</Xs: sequence>
</Xs: complexType>
</Xs: element>
</Xs: schema>
Test
The test Scheam may directly set the same output and input file path in the Shema file attributes for testing. Note that the XML file generated using the Schema generation instance option after related constraints are set does not necessarily meet the constraints, especially pattern constraint. This has been mentioned in the BizTalk help file.
However, generating instances directly is the easiest way. By directly generating an instance, we get the following XML data:
<Ns0: Class xmlns: ns0 = "http://schemasample.limit"/>
<Member Name = "Nam">
<Mobile> MobileMobil </Mobile>
<City> Beijng </City>
<Result> 10 </Result>
<Comment> 10 </Comment>
</Member>
</Ns0: Class>
The instance cannot be verified directly. The system prompts that Mobile does not comply with the specifications. We modify the XML data as follows:
<Ns0: Class xmlns: ns0 = "http://schemasample.limit"/>
<Member Name = "Nam">
<Mobile> 1, 13800138000 </Mobile>
<City> Beijng </City>
<Result> 90 95 97 99 </Result>
<Comment> Good! </Comment>
</Member>
</Ns0: Class>
The instance can be verified again. Although the test is passed, please compare the above two XML data to understand the differences between different modes. The red bold part is an array of integer score fields. The List mode is used for Schema declaration. In this mode, different values are separated by spaces. Note that there are spaces in the content when using the string type in List mode.
The pink and bold parts are composite. The previous XML data uses the integration type for verification. The next string type is also verified.