0
我在設置XSD/XML文件中的鍵/鍵引用約束時遇到了一些問題。以下是示例:在XSD/XML鍵/ keyref定義中使用元素而不是屬性
<?xml version="1.0" encoding="utf-8"?>
<xs:schema id="PriorityDefinitionFileSchema"
targetNamespace="myNamespace"
elementFormDefault="qualified"
xmlns="myNamespace"
xmlns:this="myNamespace"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
>
<xs:element name="PriorityDefinitionFile" type="this:PriorityDefinitionFile">
<xs:key name="PriorityDefinition_PK">
<xs:selector xpath="this:AvailablePriorities/this:PriorityDefinition"/>
<xs:field xpath="Value"/>
</xs:key>
<xs:keyref name="PriorityDefinition_FK" refer="this:PriorityDefinition_PK">
<xs:selector xpath="this:EventPriorityMappings/EventPriorityMapping"/>
<xs:field xpath="PriorityValue"/>
</xs:keyref>
</xs:element>
<xs:complexType name="PriorityDefinitionFile">
<xs:sequence>
<xs:element name="AvailablePriorities" type="this:AvailablePriorities"/>
<xs:element name="EventPriorityMappings" type="this:EventPriorityMappings"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="AvailablePriorities">
<xs:sequence>
<xs:element name="PriorityDefinition" type="this:PriorityDefinition" minOccurs="0" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="PriorityDefinition">
<xs:sequence>
<xs:element name="Value" type="xs:unsignedByte"/>
<xs:element name="Name" type="xs:string"/>
</xs:sequence>
<xs:attribute name="Id" use="required"/>
</xs:complexType>
<xs:complexType name="EventPriorityMappings">
<xs:sequence>
<xs:element name="EventPriorityMapping" type="this:EventPriorityMapping" minOccurs="0" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="EventPriorityMapping">
<xs:sequence>
<xs:element name="EventNr" type="xs:int"/>
<xs:element name="EventHash" type="xs:string"/>
<xs:element name="PriorityValue" type="xs:unsignedByte"/>
</xs:sequence>
<xs:attribute name="Id" use="required"/>
</xs:complexType>
</xs:schema>
我在Visual Studio中收到錯誤,說我的「PriorityDefinition_PK」無法驗證。密鑰丟失,或密鑰包含空值。
我添加了一些測試屬性,導致大多數示例使用屬性。而令人驚訝的是,如果我在我的字段定義中使用「Id」屬性,如:<xs:field xpath:"@Id">
everythink正常工作。因爲我在DataContractSerializer中使用XML,所以我根本無法使用屬性。
有關http://www.w3schools.com/xml/el_key.asp的XSD文檔清楚地表明可以使用元素和屬性。
那麼,這裏的問題在哪裏?我嘗試了像./Value,.//Value等各種xpaths,但不會接受它。
我用這個例子XML:
<?xml version="1.0" encoding="utf-8"?>
<PriorityDefinitionFile xmlns="myNamespace">
<AvailablePriorities>
<PriorityDefinition Id="0">
<Value>0</Value>
<Name>Subsequent Event</Name>
</PriorityDefinition>
<PriorityDefinition Id="1">
<Value>1</Value>
<Name>Normal Event</Name>
</PriorityDefinition>
<PriorityDefinition Id="2">
<Value>2</Value>
<Name>RootCause Event</Name>
</PriorityDefinition>
</AvailablePriorities>
<EventPriorityMappings>
<EventPriorityMapping Id="0">
<EventNr>31000</EventNr>
<EventHash>sdosdsadp</EventHash>
<PriorityValue>2</PriorityValue>
</EventPriorityMapping>
<EventPriorityMapping Id="9">
<EventNr>432432</EventNr>
<EventHash>sdfdsfd</EventHash>
<PriorityValue>9</PriorityValue> <-- Should error here
</EventPriorityMapping>
</EventPriorityMappings>
</PriorityDefinitionFile>
非常感謝您! 我看到了,我忘了這個標識符在.../this:EventPriorityMapping ... 我很確定我之前試過... 這個問題似乎是Value和PriorityValue中缺少的標識符,但爲什麼呢?我認爲這個標識符爲一些「型標識」的,但價值和PriorityValue是一個複雜類型的元素。有點混亂... – TheAngryCube