Why isn't XML schema validation detecting this uniqueness constraint violation?

44 Views Asked by At

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>
1

There are 1 best solutions below

1
Calethon On

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:

<xs:schema targetNamespace="urn:MySchema" xmlns:ms="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="ms:ParameterType" minOccurs="0" maxOccurs="unbounded">
            </xs:element>
        </xs:sequence>
    </xs:complexType>

    <xs:element name="RecipeElement" type="ms:RecipeElementType">
        <xs:unique name="UniqueParameterNameConstraint">
            <xs:selector xpath="ms:Parameter"/>
            <xs:field xpath="ms:Name"/>
        </xs:unique>        
    </xs:element>

</xs:schema>