我有 XML 和 XML 架构。 XML 包含过滤器,并且可以在另一个过滤器中包含过滤器。现在我尝试在架构中使用 key 和 keyref 但收到错误
The cardinality of the fields in keyref 'FilterKeyRef' must be equal to the cardinality of the identity constraint definition 'FilterIdKey'. The cardinality must be '1'
。我尝试使用多种在线工具、人工智能以及同事,但仍然无法解决该错误。有人知道如何解决这个问题吗?
这是 XML:
<?xml version="1.0" encoding="UTF-8"?>
<Filters xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="example.xsd">
<Filter>
<Id>0</Id>
<DisplayName>Filter1</DisplayName>
</Filter>
<Filter>
<Id>1</Id>
<DisplayName>Filter2</DisplayName>
</Filter>
<Filter>
<Id>2</Id>
<DisplayName>Filter3</DisplayName>
<FiltersToInclude>
<FilterToInclude>
<Id>0</Id>
<DisplayName>Filter1</DisplayName>
</FilterToInclude>
<FilterToInclude>
<Id>1</Id>
<DisplayName>Filter2</DisplayName>
</FilterToInclude>
</FiltersToInclude>
</Filter>
</Filters>
这是 XML 架构:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="DisplayName" type="xs:string"/>
<xs:element name="Id" type="xs:nonNegativeInteger"/>
<xs:element name="Filter">
<xs:complexType>
<xs:sequence>
<xs:element ref="Id" minOccurs="1" maxOccurs="1"/>
<xs:element ref="DisplayName" minOccurs="1" maxOccurs="1"/>
<xs:element ref="FiltersToInclude" minOccurs="0" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="FilterToInclude">
<xs:complexType>
<xs:sequence>
<xs:element ref="Id" minOccurs="1" maxOccurs="1"/>
<xs:element ref="DisplayName" minOccurs="1" maxOccurs="1"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="FiltersToInclude">
<xs:complexType>
<xs:sequence>
<xs:element ref="FilterToInclude" minOccurs="1" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="Filters">
<xs:complexType>
<xs:sequence>
<xs:element ref="Filter" minOccurs="1" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
<xs:key name="FilterIdKey">
<xs:selector xpath="Filter/Id"></xs:selector>
<xs:field xpath="."></xs:field>
</xs:key>
<xs:key name="FilterDisplayNameKey">
<xs:selector xpath="Filter/DisplayName"></xs:selector>
<xs:field xpath="."></xs:field>
</xs:key>
<xs:keyref name="FilterKeyRef" refer="FilterIdKey">
<xs:selector xpath="Filter/FiltersToInclude/FilterToInclude"></xs:selector>
<xs:field xpath="./Id"></xs:field>
<xs:field xpath="./DisplayName"></xs:field>
</xs:keyref>
</xs:element>
</xs:schema>
感谢 Martin Honnen 的评论,我成功做到了。
这是新的 XML 架构:
<?xml version="1.0" encoding="UTF-8"?>
<!-- W3C Schema generated by XMLSpy v2021 rel. 3 (x64) (http://www.altova.com) -->
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<!-- simple elements -->
<xs:element name="DisplayName" type="xs:string"/>
<xs:element name="Id" type="xs:nonNegativeInteger"/>
<xs:element name="Filter">
<xs:complexType>
<xs:sequence>
<xs:element ref="Id" minOccurs="1" maxOccurs="1"/>
<xs:element ref="DisplayName" minOccurs="1" maxOccurs="1"/>
<xs:element ref="FiltersToInclude" minOccurs="0" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="FilterToInclude">
<xs:complexType>
<xs:sequence>
<xs:element ref="Id" minOccurs="1" maxOccurs="1"/>
<xs:element ref="DisplayName" minOccurs="1" maxOccurs="1"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="FiltersToInclude">
<xs:complexType>
<xs:sequence>
<xs:element ref="FilterToInclude" minOccurs="1" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="Filters">
<xs:complexType>
<xs:sequence>
<xs:element ref="Filter" minOccurs="1" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
<xs:key name="FilterKey">
<xs:selector xpath="Filter"></xs:selector>
<xs:field xpath="Id"></xs:field>
<xs:field xpath="DisplayName"></xs:field>
</xs:key>
<xs:keyref name="FilterKeyRef" refer="FilterKey">
<xs:selector xpath="Filter/FiltersToInclude/FilterToInclude"></xs:selector>
<xs:field xpath="./Id"></xs:field>
<xs:field xpath="./DisplayName"></xs:field>
</xs:keyref>
</xs:element>
</xs:schema>