我不确定这是否可以使用XML / XSD,并且没有多少运气搜索它。
您是否可以指定XSD,以便在编写XML时,您可以拥有一个必须引用另一个XML元素中包含的值之一的元素?
例如,如果你有这个XML:
<car>Audi</car>
<car>Ford</car>
<car>Honda</car>
<person>
<drives>Audi</drives>
</person>
如何为驱动器指定XSD,使其必须是为car输入的值之一?
答案 0 :(得分:6)
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.
答案 1 :(得分:1)
这看起来像一个枚举,你的例子碰巧与w3schools例子非常相似。
<xs:element name="car" type="carType"/>
<xs:simpleType name="carType">
<xs:restriction base="xs:string">
<xs:enumeration value="Audi"/>
<xs:enumeration value="Golf"/>
<xs:enumeration value="BMW"/>
</xs:restriction>
</xs:simpleType>
<xs:element name="person">
<xs:complexType>
<xs:sequence>
<xs:element name="drives" type="carType"/>
</xs:sequence>
</xs:complexType>
</xs:element>
如果不是,你可能想要Pangeas回答。