您是否可以使用XSD将XML元素的内容限制为与其祖父母相等?这样传递验证:
<pupil>
<pupilid>342424</pupilid>
<name>John Smith</name>
<assessment>
<assessmentid>1</assessmentid>
<pupilid>342424</pupilid>
</assessment>
</pupil>
此失败验证:
<pupil>
<pupilid>342424</pupilid>
<name>John Smith</name>
<assessment>
<assessmentid>1</assessmentid>
<pupilid>666</pupilid>
</assessment>
</pupil>
答案 0 :(得分:2)
如果您将上述内容视为参照完整性,则可以执行此操作。人们可以很容易地说有冗余信息,但我也看到它是由于模型重用。
这就是你的XML:
这就是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="pupil">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="pupilid" type="xsd:unsignedInt"/>
<xsd:element name="name" type="xsd:string"/>
<xsd:element name="assessment">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="assessmentid" type="xsd:unsignedByte"/>
<xsd:element name="pupilid" type="xsd:unsignedInt"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
<xsd:key name="PK">
<xsd:selector xpath="pupilid"/>
<xsd:field xpath="."/>
</xsd:key>
<xsd:keyref name="FK" refer="PK">
<xsd:selector xpath="assessment/pupilid"/>
<xsd:field xpath="."/>
</xsd:keyref>
</xsd:element>
</xsd:schema>
您的第一个XML将通过验证,而第二个XML可能会产生:
Error occurred while loading [], line 8 position 3
The key sequence '666' in Keyref fails to refer to some key.
xsd-restrict-content-to-be-equal-to-grandparent-if-it-is-present.xml is XSD 1.0 invalid.
只要这些约束可以建模类似于参照完整性对数据库人员的意义,那么key / unique和keyref可以提供帮助。
对于更一般的共同约束,除了XSD 1.0之外,您还必须移至XSD 1.1或使用Schematron。