XmlSchemaKeyref Class
Identifies a keyref constraint. Represents the World Wide Web Consortium (W3C) keyref element.
For a list of all members of this type, see XmlSchemaKeyref Members.
System.Object
System.Xml.Schema.XmlSchemaObject
System.Xml.Schema.XmlSchemaAnnotated
System.Xml.Schema.XmlSchemaIdentityConstraint
System.Xml.Schema.XmlSchemaKeyref
[Visual Basic] Public Class XmlSchemaKeyref Inherits XmlSchemaIdentityConstraint [C#] public class XmlSchemaKeyref : XmlSchemaIdentityConstraint [C++] public __gc class XmlSchemaKeyref : public XmlSchemaIdentityConstraint [JScript] public class XmlSchemaKeyref 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 set of values) have a correspondence with those of the specified key or unique element. The constraint name must be unique within a schema.
Example
[Visual Basic, C#, C++] The following example creates a key element.
[Visual Basic] Imports System Imports System.Collections Imports System.IO Imports System.Xml Imports System.Xml.Xsl Imports System.Xml.Schema Public Class XmlSchemaObjectGenerator Private Shared Sub ValidationCallback(sender As Object, args As ValidationEventArgs) If args.Severity = XmlSeverityType.Warning Then Console.Write("WARNING: ") Else If args.Severity = XmlSeverityType.Error Then Console.Write("ERROR: ") End If End If Console.WriteLine(args.Message) End Sub 'ValidationCallback Public Shared Sub Main() Dim tr As New XmlTextReader("empty.xsd") Dim schema As XmlSchema = XmlSchema.Read(tr, New ValidationEventHandler(AddressOf ValidationCallback)) schema.ElementFormDefault = XmlSchemaForm.Qualified schema.TargetNamespace = "http://www.example.com/Report" If (True) Then Dim element As New XmlSchemaElement() element.Name = "purchaseReport" Dim element_complexType As New XmlSchemaComplexType() Dim element_complexType_sequence As New XmlSchemaSequence() If (True) Then Dim element_complexType_sequence_element As New XmlSchemaElement() element_complexType_sequence_element.Name = "region" element_complexType_sequence_element.SchemaTypeName = New XmlQualifiedName("string", "http://www.w3.org/2001/XMLSchema") If (True) Then Dim element_complexType_sequence_element_keyref As New XmlSchemaKeyref() element_complexType_sequence_element_keyref.Name = "dummy2" element_complexType_sequence_element_keyref.Selector = New XmlSchemaXPath() element_complexType_sequence_element_keyref.Selector.XPath = "r:zip/r:part" If (True) Then Dim field As New XmlSchemaXPath() field.XPath = "@number" element_complexType_sequence_element_keyref.Fields.Add(field) End If element_complexType_sequence_element_keyref.Refer = New XmlQualifiedName("pNumKey", "http://www.example.com/Report") element_complexType_sequence_element.Constraints.Add(element_complexType_sequence_element_keyref) End If element_complexType_sequence.Items.Add(element_complexType_sequence_element) End If element_complexType.Particle = element_complexType_sequence If (True) Then Dim element_complexType_attribute As New XmlSchemaAttribute() element_complexType_attribute.Name = "periodEnding" element_complexType_attribute.SchemaTypeName = New XmlQualifiedName("date", "http://www.w3.org/2001/XMLSchema") element_complexType.Attributes.Add(element_complexType_attribute) End If element.SchemaType = element_complexType If (True) Then Dim element_key As New XmlSchemaKey() element_key.Name = "pNumKey" element_key.Selector = New XmlSchemaXPath() element_key.Selector.XPath = "r:parts/r:part" If (True) Then Dim field As New XmlSchemaXPath() field.XPath = "@number" element_key.Fields.Add(field) End If element.Constraints.Add(element_key) End If schema.Items.Add(element) End If schema.Write(Console.Out) End Sub 'Main End Class 'XmlSchemaObjectGenerator ' Main() 'XmlSchemaObjectGenerator [C#] using System; using System.Collections; using System.IO; using System.Xml; using System.Xml.Xsl; using System.Xml.Schema; public class XmlSchemaObjectGenerator { private static void ValidationCallback(object sender, ValidationEventArgs args ){ if(args.Severity == XmlSeverityType.Warning) Console.Write("WARNING: "); else if(args.Severity == XmlSeverityType.Error) Console.Write("ERROR: "); Console.WriteLine(args.Message); } public static void Main() { XmlTextReader tr = new XmlTextReader("empty.xsd"); XmlSchema schema = XmlSchema.Read(tr, new ValidationEventHandler(ValidationCallback)); schema.ElementFormDefault = XmlSchemaForm.Qualified; schema.TargetNamespace = "http://www.example.com/Report"; { XmlSchemaElement element = new XmlSchemaElement(); element.Name = "purchaseReport"; XmlSchemaComplexType element_complexType = new XmlSchemaComplexType(); XmlSchemaSequence element_complexType_sequence = new XmlSchemaSequence(); { XmlSchemaElement element_complexType_sequence_element = new XmlSchemaElement(); element_complexType_sequence_element.Name = "region"; element_complexType_sequence_element.SchemaTypeName = new XmlQualifiedName("string", "http://www.w3.org/2001/XMLSchema") ; { XmlSchemaKeyref element_complexType_sequence_element_keyref = new XmlSchemaKeyref(); element_complexType_sequence_element_keyref.Name = "dummy2"; element_complexType_sequence_element_keyref.Selector = new XmlSchemaXPath(); element_complexType_sequence_element_keyref.Selector.XPath = "r:zip/r:part"; { XmlSchemaXPath field = new XmlSchemaXPath(); field.XPath = "@number"; element_complexType_sequence_element_keyref.Fields.Add(field); } element_complexType_sequence_element_keyref.Refer = new XmlQualifiedName("pNumKey", "http://www.example.com/Report") ; element_complexType_sequence_element.Constraints.Add(element_complexType_sequence_element_keyref); } element_complexType_sequence.Items.Add(element_complexType_sequence_element); } element_complexType.Particle = element_complexType_sequence; { XmlSchemaAttribute element_complexType_attribute = new XmlSchemaAttribute(); element_complexType_attribute.Name = "periodEnding"; element_complexType_attribute.SchemaTypeName = new XmlQualifiedName("date", "http://www.w3.org/2001/XMLSchema") ; element_complexType.Attributes.Add(element_complexType_attribute); } element.SchemaType = element_complexType; { XmlSchemaKey element_key = new XmlSchemaKey(); element_key.Name = "pNumKey"; element_key.Selector = new XmlSchemaXPath(); element_key.Selector.XPath = "r:parts/r:part"; { XmlSchemaXPath field = new XmlSchemaXPath(); field.XPath = "@number"; element_key.Fields.Add(field); } element.Constraints.Add(element_key); } schema.Items.Add(element); } schema.Write(Console.Out); }/* Main() */ } //XmlSchemaObjectGenerator [C++] #using <mscorlib.dll> #using <System.Xml.dll> using namespace System; using namespace System::Collections; using namespace System::IO; using namespace System::Xml; using namespace System::Xml::Xsl; using namespace System::Xml::Schema; __gc public class XmlSchemaObjectGenerator { public: static void ValidationCallback(Object* /*sender*/, ValidationEventArgs * args) { if (args -> Severity == XmlSeverityType::Warning) Console::Write(S"WARNING: "); else if (args -> Severity == XmlSeverityType::Error) Console::Write(S"ERROR: "); Console::WriteLine(args -> Message); } }; // XmlSchemaObjectGenerator int main() { XmlTextReader* tr = new XmlTextReader(S"empty.xsd"); XmlSchema * schema = XmlSchema::Read(tr, new ValidationEventHandler(schema, XmlSchemaObjectGenerator::ValidationCallback)); schema -> ElementFormDefault = XmlSchemaForm::Qualified; schema -> TargetNamespace = S"http://www.example.com/Report"; { XmlSchemaElement* element = new XmlSchemaElement(); element -> Name = S"purchaseReport"; XmlSchemaComplexType* element_complexType = new XmlSchemaComplexType(); XmlSchemaSequence* element_complexType_sequence = new XmlSchemaSequence(); { XmlSchemaElement* element_complexType_sequence_element = new XmlSchemaElement(); element_complexType_sequence_element -> Name = S"region"; element_complexType_sequence_element -> SchemaTypeName = new XmlQualifiedName(S"String*", S"http://www.w3.org/2001/XMLSchema"); { XmlSchemaKeyref* element_complexType_sequence_element_keyref = new XmlSchemaKeyref(); element_complexType_sequence_element_keyref -> Name = S"dummy2"; element_complexType_sequence_element_keyref -> Selector = new XmlSchemaXPath(); element_complexType_sequence_element_keyref -> Selector -> XPath = S"r:zip/r:part"; { XmlSchemaXPath* field = new XmlSchemaXPath(); field -> XPath = S"@number"; element_complexType_sequence_element_keyref -> Fields->Add(field); } element_complexType_sequence_element_keyref -> Refer = new XmlQualifiedName(S"pNumKey", S"http://www.example.com/Report"); element_complexType_sequence_element -> Constraints->Add(element_complexType_sequence_element_keyref); } element_complexType_sequence -> Items->Add(element_complexType_sequence_element); } element_complexType -> Particle = element_complexType_sequence; { XmlSchemaAttribute* element_complexType_attribute = new XmlSchemaAttribute(); element_complexType_attribute -> Name = S"periodEnding"; element_complexType_attribute -> SchemaTypeName = new XmlQualifiedName(S"date", S"http://www.w3.org/2001/XMLSchema"); element_complexType -> Attributes->Add(element_complexType_attribute); } element -> SchemaType = element_complexType; { XmlSchemaKey* element_key = new XmlSchemaKey(); element_key -> Name = S"pNumKey"; element_key -> Selector = new XmlSchemaXPath(); element_key -> Selector -> XPath = S"r:parts/r:part"; { XmlSchemaXPath* field = new XmlSchemaXPath(); field -> XPath = S"@number"; element_key -> Fields->Add(field); } element -> Constraints->Add(element_key); } schema -> Items->Add(element); } schema -> Write(Console::Out); } // main
[Visual Basic, C#, C++] The following XML file is generated for the preceding code example.
<?xml version="1.0" encoding="IBM437"?>
<xs:schema xmlns:r="http://www.example.com/Report" elementFormDefault="qualified" targetNamespace="http://www.example.com/Report" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="purchaseReport">
<xs:complexType>
<xs:sequence>
<xs:element name="region" type="xs:string">
<xs:keyref name="dummy2" refer="r:pNumKey">
<xs:selector xpath="r:zip/r:part" />
<xs:field xpath="@number" />
</xs:keyref>
</xs:element>
</xs:sequence>
<xs:attribute name="periodEnding" type="xs:date" />
</xs:complexType>
<xs:key name="pNumKey">
<xs:selector xpath="r:parts/r:part" />
<xs:field xpath="@number" />
</xs:key>
</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)