gpt4 book ai didi

xml - 指定 XSD,使一个 XML 元素必须具有另一个 XML 元素的值

转载 作者:数据小太阳 更新时间:2023-10-29 02:00:48 26 4
gpt4 key购买 nike

我不确定这是否可以通过 XML/XSD 实现,并且没有找到它。

您能否指定一个 XSD,以便在您编写 XML 时,您可以拥有一个必须引用另一个 XML 元素中包含的值之一的元素?

例如,如果您有这个 XML:

<car>Audi</car>
<car>Ford</car>
<car>Honda</car>

<person>
<drives>Audi</drives>
</person>

如何为驱动器指定 XSD,使其必须是为汽车输入的值之一?

最佳答案

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>

图表:

enter image description here

无效的 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.

关于xml - 指定 XSD,使一个 XML 元素必须具有另一个 XML 元素的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13300291/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com