给出以下模式,是否有一种方法可以使外键为空,并且仍然有keyref
来验证如果使用了ID,则它是有效的?
<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:simpleType name="EmptyType">
<xs:restriction base="xs:string">
<xs:maxLength value="0"/>
</xs:restriction>
</xs:simpleType>
<xs:simpleType name="OptionalIdType">
<xs:union memberTypes="EmptyType xs:int"/>
</xs:simpleType>
<xs:element name="root">
<xs:complexType>
<xs:sequence>
<xs:element name="id" type="xs:int" minOccurs="0" maxOccurs="unbounded"/>
<xs:element name="list">
<xs:complexType>
<xs:sequence minOccurs="0" maxOccurs="unbounded">
<xs:element name="id" type="OptionalIdType"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
<xs:key name="key">
<xs:selector xpath="id"/>
<xs:field xpath="."/>
</xs:key>
<xs:keyref name="keyref" refer="key">
<xs:selector xpath="list/id"/>
<xs:field xpath="."/>
</xs:keyref>
</xs:element>
</xs:schema>
该模式的示例XML文档:
<root>
<id>1</id><id>2</id>
<list>
<id>1</id>
<id>3</id>
<id/>
</list>
</root>
使用xmllint
对此进行验证,按预期会出现以下错误:
test.xml:5: Schemas validity error : Element 'id': No match found for key-sequence ['3'] of keyref 'keyref'.
test.xml:6: Schemas validity error : Element 'id': No match found for key-sequence [''] of keyref 'keyref'.
有没有一种方法可以修改架构以允许第二种情况?