您是否可以使用XML模式断言特定元素是否符合另一个元素的值? 在下面的示例中,我想确保具有父元素的每个节点名称都具有有效的父级?
<doc>
<entry>
<Name>A</Name>
</entry>
<entry>
<Name>A1</Name>
<Parent>A </Parent> <!-- valid parent defined above -->
</entry>
<entry>
<Name>C1</Name>
<Parent>C</Parent> <!-- this should fail; no entry node that is C -->
</entry>
</doc>
答案 0 :(得分:1)
您所描述的问题可以使用此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="root">
<xsd:complexType>
<xsd:sequence>
<xsd:element maxOccurs="unbounded" name="entry">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="Name">
<xsd:simpleType>
<xsd:restriction base="xsd:string">
<xsd:whiteSpace value="collapse"/>
</xsd:restriction>
</xsd:simpleType>
</xsd:element>
<xsd:element minOccurs="0" name="Parent">
<xsd:simpleType>
<xsd:restriction base="xsd:string">
<xsd:whiteSpace value="collapse"/>
</xsd:restriction>
</xsd:simpleType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
<xsd:key name="pk">
<xsd:selector xpath="entry"/>
<xsd:field xpath="Name"/>
</xsd:key>
<xsd:keyref name="fk" refer="pk">
<xsd:selector xpath="entry"/>
<xsd:field xpath="Parent"/>
</xsd:keyref>
</xsd:element>
</xsd:schema>
看起来像这样:
它向您展示了如何实现参照完整性要求。