我有一个名为 Event 的 xsd,其中包含两个类型为 AccountStructureItem 和 EventItem 的列表(“AccountStructures”和“Events”)。AccountStructureItem 中有一个属性:SourceId 定义为 unique 和 key。EventItem 类型还有一个 SourceId 属性,它是对 AccountStructureItem 的 SourceId 的引用。
我将 AccountStructureItem 的 ID 声明如下:
<xs:unique name="accounts_unique">
<xs:selector xpath="AccountStructureItem"/>
<xs:field xpath="SourceId"/>
</xs:unique>
<xs:key name="accounts_key">
<xs:selector xpath="AccountStructureItem"/>
<xs:field xpath="SourceId"/>
</xs:key>
这种关系是一对多的,可以有很多 EventItem 引用同一个 AccountStructureItem 的 SourceId。因此 EventItem 的键是另一个属性。
我的问题:尝试使用下面的 xs:keyref 创建对 accountstructure 的 accounts_key 的 keyreference 约束时出现错误。
<xs:keyref name="events_constraint" refer="accounts_key">
<xs:selector xpath="AccountStructureItem"/>
<xs:field xpath="SourceId"/>
</xs:keyref>
验证中的错误消息如下:“keyref 事件找不到引用的键或范围内唯一”
请参阅下面提供的完整 XSD 和测试 xml 文件:
<!-- XSD -->
<xs:element name="Event">
<xs:complexType>
<xs:sequence>
<!--Information Start-->
<xs:element minOccurs="1" maxOccurs="1" nillable="false" name="Information" type="ct:Information" />
<!--Information End-->
<!--AccountStructure Start-->
<xs:element name="AccountStructures">
<xs:complexType>
<xs:sequence>
<xs:element minOccurs="0" maxOccurs="unbounded" name="AccountStructureItem">
<xs:complexType>
<xs:complexContent mixed="false">
<xs:extension base="ct:AccountStructureItem" />
</xs:complexContent>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
<xs:unique name="accounts_unique">
<xs:selector xpath="AccountStructureItem"/>
<xs:field xpath="SourceId"/>
</xs:unique>
<xs:key name="accounts_key" id="accounts_key_id">
<xs:selector xpath="AccountStructureItem"/>
<xs:field xpath="SourceId"/>
</xs:key>
</xs:element>
<!--AccountStructure End-->
<!--Events Start-->
<xs:element name="Events" minOccurs="1" maxOccurs="1" nillable="false">
<xs:complexType>
<xs:sequence>
<xs:element name="EventItem" minOccurs="1" maxOccurs="10000">
<xs:complexType>
<xs:complexContent mixed="false">
<xs:extension base="ct:EventItem" />
</xs:complexContent>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
<xs:unique name="events_unique">
<xs:selector xpath="EventItem"/>
<xs:field xpath="SourceEventId"/> <!-- events has a different key -->
</xs:unique>
<xs:key name="events_key">
<xs:selector xpath="EventItem"/>
<xs:field xpath="SourceEventId"/> <!-- events has a different key -->
</xs:key>
<xs:keyref name="events_constraint" refer="accounts_key">
<xs:selector xpath="AccountStructureItem"/>
<xs:field xpath="SourceId"/>
</xs:keyref>
</xs:element>
<!--Events End-->
</xs:sequence>
</xs:complexType>
</xs:element>
<!-- XML -->
<AccountStructures>
<AccountStructureItem>
<SourceId>AC1</SourceId>
<AccountStructureType>Site</AccountStructureType>
<ParentSourceId>000017252</ParentSourceId>
<Name>Oras Isometsän tehdas</Name>
<Street>Isometsäntie 2</Street>
<City>RAUMA</City>
</AccountStructureItem>
<AccountStructureItem>
<SourceId>AC2</SourceId>
<AccountStructureType>Site</AccountStructureType>
<ParentSourceId>000017252</ParentSourceId>
<Name>Oras Oy lomamökki</Name>
<Street>Lomapolku 15</Street>
<City>RAUMA</City>
</AccountStructureItem>
</AccountStructures>
<Events>
<EventItem>
<SourceId>AC1</SourceId>
<AccountStructureType>SiteLocation</AccountStructureType>
<SourceEventId>123</SourceEventId>
<EventDate>2012-06-20T17:57:26</EventDate>
<EventText>
<EnglishValue> Oras/Isometsän tehdas Oras Isometsä </EnglishValue>
<LocalValue> Oras/Isometsän tehdas Oras Isometsä </LocalValue>
</EventText>
<Objects>
<Object>
<EnglishValue>Location</EnglishValue>
<LocalValue>Location</LocalValue>
</Object>
</Objects>
<Type>
<EnglishValue>Round</EnglishValue>
<LocalValue>Round</LocalValue>
</Type>
<Solutions>
<Solution>
<EnglishValue>On-site</EnglishValue>
<LocalValue>On-site</LocalValue>
</Solution>
</Solutions>
<EventValue>
<UnitType>Integer</UnitType>
<Value>1</Value>
</EventValue>
<Notes></Notes>
</EventItem>
<EventItem>
<SourceId>AC1</SourceId>
<AccountStructureType>SiteLocation</AccountStructureType>
<SourceEventId>1234</SourceEventId>
<EventDate>2012-06-20T17:57:26</EventDate>
<EventText>
<EnglishValue> Oras/Isometsän tehdas Oras Isometsä </EnglishValue>
<LocalValue> Oras/Isometsän tehdas Oras Isometsä </LocalValue>
</EventText>
<Objects>
<Object>
<EnglishValue>Location</EnglishValue>
<LocalValue>Location</LocalValue>
</Object>
</Objects>
<Type>
<EnglishValue>Round</EnglishValue>
<LocalValue>Round</LocalValue>
</Type>
<Solutions>
<Solution>
<EnglishValue>On-site</EnglishValue>
<LocalValue>On-site</LocalValue>
</Solution>
</Solutions>
<EventValue>
<UnitType>Integer</UnitType>
<Value>1</Value>
</EventValue>
<Notes></Notes>
</EventItem>
</Events>