复杂类型的xsd唯一约束失败

时间:2015-08-13 17:52:46

标签: xml xsd xsd-validation

Xsd架构

    <?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
            attributeFormDefault="unqualified" elementFormDefault="qualified">
<xsd:element name="message">
<xsd:complexType>
    <xsd:choice maxOccurs="unbounded">
        <xsd:element ref="controlId"/>
        <xsd:element ref="date"/>
        <xsd:element ref="type"/>
        <xsd:element ref="event"/>
        <xsd:element ref="sendingFacility"/>
    </xsd:choice>
</xsd:complexType>
<xsd:unique name="AnswerIdUnique">
        <xsd:selector xpath="./controlId" />
      <xsd:field xpath="."/>
    </xsd:unique>
<xsd:unique name="AnswerIdUnique1">
        <xsd:selector xpath="./date" />
      <xsd:field xpath="."/>
    </xsd:unique>
<xsd:unique name="AnswerIdUnique2">
        <xsd:selector xpath="./type" />
      <xsd:field xpath="."/>
    </xsd:unique>
<xsd:unique name="AnswerIdUnique3">
        <xsd:selector xpath="./event" />
     <xsd:field xpath="."/>
    </xsd:unique>
</xsd:element>

<xsd:element name="event">
<xsd:complexType mixed="true">
    <xsd:attribute name="class" type="xsd:string" use="optional"/>
    <xsd:attribute name="name" type="xsd:string" use="optional"/>
</xsd:complexType>
</xsd:element>

<xsd:element name="controlId" type="xsd:string"/>
<xsd:element name="date" type="xsd:string"/>
<xsd:element name="type" type="xsd:string"/>
<xsd:element name="sendingFacility" type="xsd:string"/>
</xsd:schema>

XML

    <message>
<date></date>
<sendingFacility></sendingFacility>
<controlId></controlId>
<event>fdsf</event>
<event>fdsf</event>
</message>

我不希望元素事件被复制。如果我复制日期,它会抛出适当的错误

 "Duplicate Unique Value [] Declared For Identity Constraint "AnswerIdUnique1" Of Element "message".." 

但是,虽然我对事件元素强制执行了唯一约束,但它仍然允许元素重复。请建议

1 个答案:

答案 0 :(得分:0)

您的XML没有名为eventId的属性。我想你想要

<xsd:unique name="AnswerIdUnique3">
     <xsd:selector xpath="event" />
     <xsd:field xpath="."/>
</xsd:unique>