XSD 1.0會工作。理想情況下,您應該使用相同的模式類型和參照完整性的組合。
的XSD:
<?xml version="1.0" encoding="utf-8"?>
<!--XML Schema generated by QTAssistant/XML Schema Refactoring (XSR) Module (http://www.paschidev.com)-->
<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>
圖:
無效XML:
<sample>
<car>Audi</car>
<car>Ford</car>
<car>Honda</car>
<person>
<drives>Aud</drives>
</person>
</sample>
錯誤:
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.
這個錯誤告訴你使用枚舉值會使key/keyref變得多餘 - 你不會觸發它。
但是,如果在XSD中不能包含枚舉值的列表,則應該至少強制該類型的最小長度,以避免創建空值的空值。當然,雖然建議分享這種類型,但您不必這樣做。
修改XSD:
<?xml version="1.0" encoding="utf-8"?>
<!--XML Schema generated by QTAssistant/XML Schema Refactoring (XSR) Module (http://www.paschidev.com)-->
<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 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.
感謝的Mads指定他們,看起來是我所追求的 –