在XSD / XML key / keyref定义中使用element而不是attribute

时间:2016-11-22 13:26:01

标签: xml visual-studio xsd xsd-validation

我在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,所以根本不能使用属性。

{{}}上的XSD文档明确指出可以使用元素和属性。

那么,这里的问题在哪里?我尝试了各种xpath,如./Value,。// Value等,但不接受它。

我正在使用这个示例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>

1 个答案:

答案 0 :(得分:0)

您尚未使用命名空间别名&#39;此&#39;一贯

Diagram from Liquid XML

<xs:element name="PriorityDefinitionFile"
            type="this:PriorityDefinitionFile">
    <xs:key name="PriorityDefinition_PK">
        <xs:selector xpath="this:AvailablePriorities/this:PriorityDefinition"/>
        <xs:field xpath="this:Value"/>
    </xs:key>
    <xs:keyref name="PriorityDefinition_FK"
               refer="this:PriorityDefinition_PK">
        <xs:selector xpath="this:EventPriorityMappings/this:EventPriorityMapping"/>
        <xs:field xpath="this:PriorityValue"/>
    </xs:keyref>
</xs:element>

此外,您的样本数据没有正确的FK(我用**和##推销了PK / FK)。

<?xml version="1.0" encoding="utf-8"?>
<!-- Created with Liquid XML 2017 Developer Bundle Edition 15.0.0.0 (https://www.liquid-technologies.com) -->
<PriorityDefinitionFile xmlns="myNamespace"
                        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                        xsi:schemaLocation="myNamespace file:///D:/Development2017/Liquid/Applications/XmlStudio/Tests/UserSamples/40743074_using-element-instead-of-attribute-in-xsd-xml-key-keyref-definition/MySchema.xsd">
    <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>1</PriorityValue>**
        </EventPriorityMapping>
        <EventPriorityMapping Id="10">
            <EventNr>432432</EventNr>
            <EventHash>sdfdsfd</EventHash>
            **<PriorityValue>0</PriorityValue>**
        </EventPriorityMapping>
    </EventPriorityMappings>
</PriorityDefinitionFile>