I'm struggling with the problem of the correct use of xsd:key Element in my xml schema to preserve uniqueness of records. I've seen many working examples, yet when I try to utilize them to fit my needs I can't achieve desired results.
Here's a simple example, written by me, which doesn't work as expected:
KeyTest.xml
<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<KeyTest xmlns="keytest"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="keytest keytest.xsd">
<ManyThings>
<OneThing>
<Id>a</Id>
</OneThing>
<OneThing>
<Id>b</Id>
</OneThing>
<OneThing>
<Id>b</Id>
</OneThing>
</ManyThings>
</KeyTest>
KeyTest.xsd
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="keytest"
xmlns="keytest"
elementFormDefault="qualified">
<xs:element name="KeyTest">
<xs:complexType>
<xs:all>
<xs:element name="ManyThings" type="ManyThingsType">
<xs:key name="PK_ManyThings">
<xs:selector xpath="OneThing"/>
<xs:field xpath="Id"/>
</xs:key>
</xs:element>
</xs:all>
</xs:complexType>
</xs:element>
<xs:complexType name="ManyThingsType">
<xs:sequence maxOccurs="unbounded">
<xs:element name="OneThing" type="OneThingType"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="OneThingType">
<xs:all>
<xs:element name="Id" type="xs:string"/>
</xs:all>
</xs:complexType>
</xs:schema>
I expect validator to refuse KeyTest.xml because there are multiple records with Id "b". However, KeyTest.xml is considered valid. Could you point me where's a mistake in my code, please?
I've been using Utilties Online Validator to validate my files. I'm xml/xsd beginner, so please take into consideration the fact that I might have made some basic mistakes.