I’m trying to use a XSD document to detect the uniqueness of a combination of attributes from two elements.
I’ve added the following unique statement to my XSD:
<xs:unique name="uniqueEMNameParentID">
<xs:selector xpath=".//EM"/>
<xs:field xpath="@Name"/>
<xs:field xpath=".//Station/@ID"/>
</xs:unique>
And this is an example of my XML:
<?xml version="1.0" encoding="utf-8"?>
<Project>
<PLC Name="PLC">
<Type>
<TypeDef ID="1" Name="G05" Description="" />
</Type>
<SA ID="01">
<Station ID="100">
<EM ID="01" Name="IR01" IsRobot="TRUE">
</EM>
</Station>
</SA>
<SA ID="02">
<Station ID="100">
<EM ID="02" Name="IR01" IsRobot="TRUE">
</EM>
<EM ID="03" Name="IR03" IsRobot="TRUE">
</EM>
</Station>
</SA>
</PLC>
</Project>
When using notepad++ the XSD gives me the correct error: IR01 is a duplicate key for unique identity constraint ‘uniqueEMNameParentID’. When using this same XSD in C# it thinks my document is okay. This is my C# code:
private void ValidateXML()
{
XmlSchemaSet schema = new XmlSchemaSet();
schema.Add("", @"Z:VMshareMaartenWindowsFormsApplication3WindowsFormsApplication3XMLXSDScheme3.xsd");
projectXML.Schemas.Add(schema);
projectXML.Validate((object sender, System.Xml.Schema.ValidationEventArgs args) =>
{
MessageBox.Show(args.Message);
});
}
My XML Document does not contain any namespaces, my XSD does not contain a targetnamespace and both attribute and elementformdefault are set to unqualified. Does anyone have any idea what it could be?
Maarten Schuurmans is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.