XmlSchemaUnique Class
Identifies a unique constraint among a set of elements. Represents the World Wide Web Consortium (W3C) unique element.
For a list of all members of this type, see XmlSchemaUnique Members.
System.Object
System.Xml.Schema.XmlSchemaObject
System.Xml.Schema.XmlSchemaAnnotated
System.Xml.Schema.XmlSchemaIdentityConstraint
System.Xml.Schema.XmlSchemaUnique
[Visual Basic] Public Class XmlSchemaUnique Inherits XmlSchemaIdentityConstraint [C#] public class XmlSchemaUnique : XmlSchemaIdentityConstraint [C++] public __gc class XmlSchemaUnique : public XmlSchemaIdentityConstraint [JScript] public class XmlSchemaUnique extends XmlSchemaIdentityConstraint
Thread Safety
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Remarks
Specifies that an attribute or element value (or a combination of attribute or element values) must be unique within the specified scope. The constraint name must be unique within a schema.
Example
[Visual Basic, C#, C++] The following example shows the use of the XmlSchemaUnique class.
[Visual Basic] Imports System Imports System.Xml Imports System.Xml.Schema Class XMLSchemaExamples Public Shared Sub Main() Dim schema as New XmlSchema() ' <xs:complexType name="customerOrderType"> Dim customerOrderType as New XmlSchemaComplexType() customerOrderType.Name = "customerOrderType" ' <xs:sequence> Dim sequence1 as New XmlSchemaSequence() ' <xs:element name="item" minOccurs="0" maxOccurs="unbounded"> Dim item as New XmlSchemaElement() item.MinOccurs = 0 item.MaxOccursString = "unbounded" item.Name = "item" ' <xs:complexType> Dim ct1 as New XmlSchemaComplexType() ' <xs:attribute name="itemID" type="xs:string"/> Dim itemID as New XmlSchemaAttribute() itemID.Name = "itemID" itemID.SchemaTypeName = new XmlQualifiedName("string", "http://www.w3.org/2001/XMLSchema") ' </xs:complexType> ct1.Attributes.Add(itemID) ' </xs:element> item.SchemaType = ct1 ' </xs:sequence> sequence1.Items.Add(item) customerOrderType.Particle = sequence1 ' <xs:attribute name="CustomerID" type="xs:string"/> Dim CustomerID as New XmlSchemaAttribute() CustomerID.Name = "CustomerID" CustomerID.SchemaTypeName = new XmlQualifiedName("string", "http://www.w3.org/2001/XMLSchema") customerOrderType.Attributes.Add(CustomerID) ' </xs:complexType> schema.Items.Add(customerOrderType) ' <xs:element name="ordersByCustomer"> Dim ordersByCustomer as New XmlSchemaElement() ordersByCustomer.Name = "ordersByCustomer" ' <xs:complexType> Dim ct2 as New XmlSchemaComplexType() ' <xs:sequence> Dim sequence2 as New XmlSchemaSequence() ' <xs:element name="customerOrders" type="customerOrderType" minOccurs="0" maxOccurs="unbounded" /> Dim customerOrders as New XmlSchemaElement() customerOrders.MinOccurs = 0 customerOrders.MaxOccursString = "unbounded" customerOrders.Name = "customerOrders" customerOrders.SchemaTypeName = new XmlQualifiedName("customerOrderType", "") ' </xs:sequence> sequence2.Items.Add(customerOrders) ' </xs:complexType> ct2.Particle = sequence2 ordersByCustomer.SchemaType = ct2 ' <xs:unique name="oneCustomerOrdersforEachCustomerID"> Dim element_unique as New XmlSchemaUnique() element_unique.Name = "oneCustomerOrdersforEachCustomerID" ' <xs:selector xpath="customerOrders"/> element_unique.Selector = new XmlSchemaXPath() element_unique.Selector.XPath = "customerOrders" ' <xs:field xpath="@customerID"/> Dim field as New XmlSchemaXPath() field.XPath = "@customerID" ' </xs:unique> element_unique.Fields.Add(field) ordersByCustomer.Constraints.Add(element_unique) ' </xs:element> schema.Items.Add(ordersByCustomer) schema.Compile(AddressOf ValidationCallbackOne) Dim nsmgr As New XmlNamespaceManager(New NameTable()) nsmgr.AddNamespace("xs", "http://www.w3.org/2001/XMLSchema") schema.Write(Console.Out, nsmgr) End Sub Public Shared Sub ValidationCallbackOne(sender as object, args as ValidationEventArgs) Console.WriteLine(args.Message) End Sub End Class [C#] using System; using System.Xml; using System.Xml.Schema; class XMLSchemaExamples { public static void Main() { XmlSchema schema = new XmlSchema(); // <xs:complexType name="customerOrderType"> XmlSchemaComplexType customerOrderType = new XmlSchemaComplexType(); customerOrderType.Name = "customerOrderType"; // <xs:sequence> XmlSchemaSequence sequence1 = new XmlSchemaSequence(); // <xs:element name="item" minOccurs="0" maxOccurs="unbounded"> XmlSchemaElement item = new XmlSchemaElement(); item.MinOccurs = 0; item.MaxOccursString = "unbounded"; item.Name = "item"; // <xs:complexType> XmlSchemaComplexType ct1 = new XmlSchemaComplexType(); // <xs:attribute name="itemID" type="xs:string"/> XmlSchemaAttribute itemID = new XmlSchemaAttribute(); itemID.Name = "itemID"; itemID.SchemaTypeName = new XmlQualifiedName("string", "http://www.w3.org/2001/XMLSchema"); // </xs:complexType> ct1.Attributes.Add(itemID); // </xs:element> item.SchemaType = ct1; // </xs:sequence> sequence1.Items.Add(item); customerOrderType.Particle = sequence1; // <xs:attribute name="CustomerID" type="xs:string"/> XmlSchemaAttribute CustomerID = new XmlSchemaAttribute(); CustomerID.Name = "CustomerID"; CustomerID.SchemaTypeName = new XmlQualifiedName("string", "http://www.w3.org/2001/XMLSchema"); customerOrderType.Attributes.Add(CustomerID); // </xs:complexType> schema.Items.Add(customerOrderType); // <xs:element name="ordersByCustomer"> XmlSchemaElement ordersByCustomer = new XmlSchemaElement(); ordersByCustomer.Name = "ordersByCustomer"; // <xs:complexType> XmlSchemaComplexType ct2 = new XmlSchemaComplexType(); // <xs:sequence> XmlSchemaSequence sequence2 = new XmlSchemaSequence(); // <xs:element name="customerOrders" type="customerOrderType" minOccurs="0" maxOccurs="unbounded" /> XmlSchemaElement customerOrders = new XmlSchemaElement(); customerOrders.MinOccurs = 0; customerOrders.MaxOccursString = "unbounded"; customerOrders.Name = "customerOrders"; customerOrders.SchemaTypeName = new XmlQualifiedName("customerOrderType", ""); // </xs:sequence> sequence2.Items.Add(customerOrders); // </xs:complexType> ct2.Particle = sequence2; ordersByCustomer.SchemaType = ct2; // <xs:unique name="oneCustomerOrdersforEachCustomerID"> XmlSchemaUnique element_unique = new XmlSchemaUnique(); element_unique.Name = "oneCustomerOrdersforEachCustomerID"; // <xs:selector xpath="customerOrders"/> element_unique.Selector = new XmlSchemaXPath(); element_unique.Selector.XPath = "customerOrders"; // <xs:field xpath="@customerID"/> XmlSchemaXPath field = new XmlSchemaXPath(); field.XPath = "@customerID"; // </xs:unique> element_unique.Fields.Add(field); ordersByCustomer.Constraints.Add(element_unique); // </xs:element> schema.Items.Add(ordersByCustomer); schema.Compile(new ValidationEventHandler(ValidationCallbackOne)); XmlNamespaceManager nsmgr = new XmlNamespaceManager(new NameTable()); nsmgr.AddNamespace("xs", "http://www.w3.org/2001/XMLSchema"); schema.Write(Console.Out, nsmgr); } public static void ValidationCallbackOne(object sender, ValidationEventArgs args) { Console.WriteLine(args.Message); } } [C++] #using <mscorlib.dll> #using <System.Xml.dll> using namespace System; using namespace System::Xml; using namespace System::Xml::Schema; __gc class XMLSchemaExamples { public: static void ValidationCallbackOne(Object* sender, ValidationEventArgs * args) { Console::WriteLine(args -> Message); } }; int main() { XmlSchema* schema = new XmlSchema(); // <xs:complexType name=S"customerOrderType"> XmlSchemaComplexType* customerOrderType = new XmlSchemaComplexType(); customerOrderType -> Name = S"customerOrderType"; // <xs:sequence> XmlSchemaSequence* sequence1 = new XmlSchemaSequence(); // <xs:element name=S"item" minOccurs=S"0" maxOccurs=S"unbounded"> XmlSchemaElement* item = new XmlSchemaElement(); item -> MinOccurs = 0; item -> MaxOccursString = S"unbounded"; item -> Name = S"item"; // <xs:complexType> XmlSchemaComplexType* ct1 = new XmlSchemaComplexType(); // <xs:attribute name=S"itemID" type=S"xs:string"/> XmlSchemaAttribute* itemID = new XmlSchemaAttribute(); itemID -> Name = S"itemID"; itemID -> SchemaTypeName = new XmlQualifiedName(S"string", S"http://www.w3.org/2001/XMLSchema"); // </xs:complexType> ct1 -> Attributes->Add(itemID); // </xs:element> item -> SchemaType = ct1; // </xs:sequence> sequence1 -> Items->Add(item); customerOrderType -> Particle = sequence1; // <xs:attribute name=S"CustomerID" type=S"xs:string"/> XmlSchemaAttribute* CustomerID = new XmlSchemaAttribute(); CustomerID -> Name = S"CustomerID"; CustomerID -> SchemaTypeName = new XmlQualifiedName(S"string", S"http://www.w3.org/2001/XMLSchema"); customerOrderType -> Attributes->Add(CustomerID); // </xs:complexType> schema -> Items->Add(customerOrderType); // <xs:element name=S"ordersByCustomer"> XmlSchemaElement* ordersByCustomer = new XmlSchemaElement(); ordersByCustomer -> Name = S"ordersByCustomer"; // <xs:complexType> XmlSchemaComplexType* ct2 = new XmlSchemaComplexType(); // <xs:sequence> XmlSchemaSequence* sequence2 = new XmlSchemaSequence(); // <xs:element name=S"customerOrders" type=S"customerOrderType" minOccurs=S"0" maxOccurs=S"unbounded" /> XmlSchemaElement* customerOrders = new XmlSchemaElement(); customerOrders -> MinOccurs = 0; customerOrders -> MaxOccursString = S"unbounded"; customerOrders -> Name = S"customerOrders"; customerOrders -> SchemaTypeName = new XmlQualifiedName(S"customerOrderType", S""); // </xs:sequence> sequence2 -> Items->Add(customerOrders); // </xs:complexType> ct2 -> Particle = sequence2; ordersByCustomer -> SchemaType = ct2; // <xs:unique name=S"oneCustomerOrdersforEachCustomerID"> XmlSchemaUnique* element_unique = new XmlSchemaUnique(); element_unique -> Name = S"oneCustomerOrdersforEachCustomerID"; // <xs:selector xpath=S"customerOrders"/> element_unique -> Selector = new XmlSchemaXPath(); element_unique -> Selector -> XPath = S"customerOrders"; // <xs:field xpath=S"@customerID"/> XmlSchemaXPath* field = new XmlSchemaXPath(); field -> XPath = S"@customerID"; // </xs:unique> element_unique -> Fields->Add(field); ordersByCustomer -> Constraints->Add(element_unique); // </xs:element> schema -> Items->Add(ordersByCustomer); schema -> Compile(new ValidationEventHandler(schema, XMLSchemaExamples::ValidationCallbackOne)); XmlNamespaceManager* nsmgr = new XmlNamespaceManager(new NameTable()); nsmgr -> AddNamespace(S"xs", S"http://www.w3.org/2001/XMLSchema"); schema -> Write(Console::Out, nsmgr); }
[Visual Basic, C#, C++] The following XML file is generated for this code example.
<?xml version="1.0" encoding="IBM437"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:complexType name="customerOrderType">
<xs:sequence>
<xs:element name="item" minOccurs="0" maxOccurs="unbounded">
<xs:complexType>
<xs:attribute name="itemID" type="xs:string"/>
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute name="CustomerID" type="xs:string"/>
</xs:complexType>
<xs:element name="ordersByCustomer">
<xs:complexType>
<xs:sequence>
<xs:element name="customerOrders" type="customerOrderType" minOccurs="0" maxOccurs="unbounded" />
</xs:sequence>
</xs:complexType>
<xs:unique name="oneCustomerOrdersforEachCustomerID">
<xs:selector xpath="customerOrders"/>
<xs:field xpath="@customerID"/>
</xs:unique>
</xs:element>
</xs:schema>
[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button
in the upper-left corner of the page.
Requirements
Namespace: System.Xml.Schema
Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family
Assembly: System.Xml (in System.Xml.dll)