Generating Restrictions in XML Schema Dynamically Using VB.NET 2005: Preliminaries (Page 1 of 5 )
This is the first article in a series concentrating on generating restrictions in XML Schema dynamically using Visual Basic 2005. The series is mainly targeted at those who are familiar with XML, XML Schema and the .NET framework.
A downloadable file for this article is available
here.
If you are new to XML, there exist several articles on XML alone on this web site (try “igrep”). If you are new to XML Schema, I strongly suggest you go through my series “Designing your own XML Schema.” If you are new to generating XML Schema using .NET, I suggest you go through my series “Generating XML schema dynamically using VB.NET 2005.”
The entire solution for this article was developed using Visual Studio 2005 Professional Edition on Windows Server 2003 standard edition. Some examples in this series may not compile successfully using Visual Studio 2003, as some of the features in .NET 1.1 turned out to be obsolete in .NET 2.0. For complete details you can refer to this link: http://go.microsoft.com/fwlink/?linkid=14202. Please note that some of the examples in this series may not be practical. Those were designed only for explaining the particular concept.
Restrictions (or constraints) in XML Schema: A sample schema
Since I already covered “restrictions” in my series “designing your own XML schema,” I will not repeat the information. Here we shall work directly with a sample scenario.
I would like to have an element “AGE” present in my XML Schema. I can assign a data type “integer” to it. But this is not sufficient because I would like to have a value between 0 and 100 (whereas an integer would accept more than that). This means a “restriction” is necessary. Let us consider the following XML Schema, which can do something better:
<?xml version="1.0" encoding="utf-16"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="Organization">
<xs:complexType>
<xs:sequence>
<xs:element maxOccurs="unbounded" name="Employee">
<xs:complexType>
<xs:sequence>
<xs:element name="Name" type="xs:string" />
<xs:element name="Age">
<xs:simpleType>
<xs:restriction base="xs:integer">
<xs:maxInclusive value="100" />
</xs:restriction>
</xs:simpleType>
</xs:element>
</xs:sequence>
<xs:attribute name="ID" type="xs:int"
use="required" />
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
Let us concentrate on the “age” element. It stays under the “Employee” element. Its “simpleType” (or data type) is defined as an integer type. And finally, we applied the “restriction” by specifying the maximum value as follows:
<xs:element name="age">
<xs:simpleType>
<xs:restriction base="xs:integer">
<xs:maxInclusive value="100" />
</xs:restriction>
</xs:simpleType>
</xs:element>
You should also observe that “Name” and “Age” are defined with “simple types” rather than with “complex types.” The only complex elements are “Organization” and “Employee.” You can use “Exclusive” or “Inclusive” according to your requirements (they can even be interchanged). But care should be taken while defining the same (especially when using tools).
Now let us go through an example with .NET!
Next: Restrictions (or constraints) in XML Schema: VB.NET sample >>
More Visual Basic.NET Articles
More By Jagadish Chaterjee