I am attempting to debug why validating XML using an XSD is not detecting a uniqueness constraint violation. Given the XML data below, and the XSD schema that follows it, the UniqueParameterNameConstraint uniqueness rule will not detect the violation of two Parameters having the same name of CREAM_AMOUNT.
If I delete the references to "MySchema" from both the XML and the XSD, the constraint works and the uniqueness violation is detected.
I am new to XML schema and at a loss to explain why the namespace declaration would affect the behavior.
<RecipeElement xmlns="urn:MySchema">
<Parameter>
<Name>CREAM_AMOUNT</Name>
</Parameter>
<Parameter>
<Name>CREAM_AMOUNT</Name>
</Parameter>
<Parameter>
<Name>FLAVOR_AMOUNT</Name>
</Parameter>
<Parameter>
<Name>MILK_AMOUNT</Name>
</Parameter>
<Parameter>
<Name>SUGAR_AMOUNT</Name>
</Parameter>
</RecipeElement>
<xs:schema targetNamespace="urn:MySchema" xmlns="urn:MySchema" xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
<xs:complexType name="ParameterType">
<xs:sequence>
<xs:element name="Name" type="xs:string"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="RecipeElementType">
<xs:sequence>
<xs:element name="Parameter" type="ParameterType" minOccurs="0" maxOccurs="unbounded">
</xs:element>
</xs:sequence>
</xs:complexType>
<xs:element name="RecipeElement" type="RecipeElementType">
<xs:unique name="UniqueParameterNameConstraint">
<xs:selector xpath="Parameter"/>
<xs:field xpath="Name"/>
</xs:unique>
</xs:element>
</xs:schema>
I found that I was making a "common mistake" per the following link:
https://support.liquid-technologies.com/kb/a79/creating-a-unique-constraint-with-an-xsd.aspx
Per the link, "If the schema declares a targetnamespace then the XPath expressions must be fully qualified.".
My updated XSD looks like this: