Generating Restrictions in XML Schema Dynamically Using VB.NET 2005: Preliminaries - Extending the previous restriction with minExclusive
(Page 4 of 5 )
I gave an example covering the “Age” element previously. It is not going to be a complete solution yet. We specified only the maximum value (and forgot about the minimum value). So, let us make tiny modifications (especially for the “Age” element) to our schema so that it looks like the following, which contains two facets now:
<xs:element name="age">
<xs:simpleType>
<xs:restriction base="xs:integer">
<xs:minExclusive value="0" />
<xs:maxInclusive value="100" />
</xs:restriction>
</xs:simpleType>
</xs:element>
Within the above XML Schema, I managed to have two facets be implemented. Now, with the above two facets we tightly restrict the “Age” element to have a value only between 1 and 100. Now, let us see how we can achieve the same dynamically using .NET.
You can modify the code fragment presented in the previous sections as follows:
Dim restriction As New XmlSchemaSimpleTypeRestriction()
simpleType.Content = restriction
restriction.BaseTypeName = New XmlQualifiedName
("integer", "http://www.w3.org/2001/XMLSchema")
Dim minExclusive As New XmlSchemaMinExclusiveFacet()
restriction.Facets.Add(minExclusive)
minExclusive.Value = "0"
Dim maxInclusive As New XmlSchemaMaxInclusiveFacet()
restriction.Facets.Add(maxInclusive)
maxInclusive.Value = "100"
And that would achieve what we desired. Once you execute the above code, you should be able to see the following output of the XML Schema:
<?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:minExclusive value="0" />
<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>
Next: Differences between xxxExclusive and xxxInclusive >>
More Visual Basic.NET Articles
More By Jagadish Chaterjee