次の方法で共有


XmlSchemaUnique クラス

要素のセット間の一意の制約を識別します。W3C (World Wide Web Consortium) unique 要素を表します。

この型のすべてのメンバの一覧については、XmlSchemaUnique メンバ を参照してください。

System.Object
   System.Xml.Schema.XmlSchemaObject
      System.Xml.Schema.XmlSchemaAnnotated
         System.Xml.Schema.XmlSchemaIdentityConstraint
            System.Xml.Schema.XmlSchemaUnique

Public Class XmlSchemaUnique
   Inherits XmlSchemaIdentityConstraint
[C#]
public class XmlSchemaUnique : XmlSchemaIdentityConstraint
[C++]
public __gc class XmlSchemaUnique : public
   XmlSchemaIdentityConstraint
[JScript]
public class XmlSchemaUnique extends XmlSchemaIdentityConstraint

スレッドセーフ

この型の public static (Visual Basicでは Shared) のすべてのメンバは、マルチスレッド操作で安全に使用できます。インスタンスのメンバの場合は、スレッドセーフであるとは限りません。

解説

属性値または要素値 (あるいは属性値または要素値の組み合わせ) は、指定したスコープ内で一意である必要があることを指定します。制約名はスキーマ内で一意である必要があります。

使用例

[Visual Basic, C#, C++] XmlSchemaUnique クラスを使用する例を次に示します。

 
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++] このコード例に対して生成される XML ファイルを次に示します。

<?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] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン 言語のフィルタ をクリックします。

必要条件

名前空間: System.Xml.Schema

プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ

アセンブリ: System.Xml (System.Xml.dll 内)

参照

XmlSchemaUnique メンバ | System.Xml.Schema 名前空間