XSD 1.0 will work. Ideally, you should use a combination of the same type of schema and referential integrity.
XSD:
<?xml version="1.0" encoding="utf-8"?> <xsd:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <xsd:element name="sample"> <xsd:complexType> <xsd:sequence> <xsd:element maxOccurs="unbounded" name="car" type="car"/> <xsd:element name="person"> <xsd:complexType> <xsd:sequence> <xsd:element name="drives" type="car"/> </xsd:sequence> </xsd:complexType> </xsd:element> </xsd:sequence> </xsd:complexType> <xsd:key name="PKCars"> <xsd:selector xpath="car"/> <xsd:field xpath="."/> </xsd:key> <xsd:keyref name="FKPersonCar" refer="PKCars"> <xsd:selector xpath="person/drives"/> <xsd:field xpath="."/> </xsd:keyref> </xsd:element> <xsd:simpleType name="car"> <xsd:restriction base="xsd:string"> <xsd:enumeration value="Audi"/> <xsd:enumeration value="Ford"/> <xsd:enumeration value="Honda"/> </xsd:restriction> </xsd:simpleType> </xsd:schema>
Diagram:

Invalid XML:
<sample> <car>Audi</car> <car>Ford</car> <car>Honda</car> <person> <drives>Aud</drives> </person> </sample>
Error:
Error occurred while loading [], line 7 position 16 The 'drives' element is invalid - The value 'Aud' is invalid according to its datatype 'car' - The Enumeration constraint failed. specify-xsd-such-that-an-xml-element-must-have-the-value-of-another-xml-element.xml is invalid.
This error tells you that using an enumerated value makes key / keyref superfluous - you cannot run it.
However, if you cannot have a list of the listed values in your XSD, you should at least provide a minimum length for this type to avoid empty values that create chaos. Of course, although sharing a type is recommended, you don't need to.
Changed XSD:
<?xml version="1.0" encoding="utf-8"?> <xsd:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <xsd:element name="sample"> <xsd:complexType> <xsd:sequence> <xsd:element maxOccurs="unbounded" name="car" type="car"/> <xsd:element name="person"> <xsd:complexType> <xsd:sequence> <xsd:element name="drives" type="car"/> </xsd:sequence> </xsd:complexType> </xsd:element> </xsd:sequence> </xsd:complexType> <xsd:key name="PKCars"> <xsd:selector xpath="car"/> <xsd:field xpath="."/> </xsd:key> <xsd:keyref name="FKPersonCar" refer="PKCars"> <xsd:selector xpath="person/drives"/> <xsd:field xpath="."/> </xsd:keyref> </xsd:element> <xsd:simpleType name="car"> <xsd:restriction base="xsd:normalizedString"> <xsd:minLength value="1"/> </xsd:restriction> </xsd:simpleType> </xsd:schema>
Error message:
Error occurred while loading [], line 9 position 4 The key sequence 'Aud' in Keyref fails to refer to some key. specify-xsd-such-that-an-xml-element-must-have-the-value-of-another-xml-element.xml is invalid.
source share