2012-08-09 51 views
2

我有一個xsd命名的Event,其中包含AccountStructureItem和EventItem類型的兩個列表(「AccountStructures」和「Events」)。有一個屬性:AccountStructureItem中的SourceId被定義爲唯一和密鑰。 EventItem類型還有一個SourceId屬性,它是對AccountStructureItem的SourceId的引用。XSD架構參考完整性xs:keyref

我宣佈AccountStructureItem's 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's指同一AccountStructureItem's的SourceID。因此EventItem的關鍵是另一個屬性。

我的問題:嘗試使用下面的xs:keyref爲accountstructure的accounts_key創建密鑰引用約束時出錯。

<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> 

回答

3

我自己拿到了解決方案。問題在於我在元素列表中聲明瞭鍵和引用。我現在將這個關鍵字,唯一關鍵字和ref參數的聲明移到了兩個列表之外,並使用「list/item」xpath來訪問它們。就像這樣:

<xs:selector xpath="AccountStructures/AccountStructureItem"/> 
    <xs:field xpath="SourceId"/> 
</xs:unique> 

<xs:key name="accounts_key" id="accounts_key_id"> 
    <xs:selector xpath="AccountStructures/AccountStructureItem"/> 
    <xs:field xpath="SourceId"/> 
</xs:key> 

<xs:unique name="events_unique"> 
    <xs:selector xpath="Events/EventItem"/> 
    <xs:field xpath="SourceEventId"/> 
</xs:unique> 

<xs:key name="events_key"> 
    <xs:selector xpath="Events/EventItem"/> 
    <xs:field xpath="SourceEventId"/> 
</xs:key> 

<xs:keyref name="events_constraint" refer="accounts_key"> 
    <xs:selector xpath="Events/EventItem"/> 
    <xs:field xpath="SourceId"/> 
</xs:keyref>