谁能指出为什么我的 XSD 中的唯一元素不强制唯一性?这应该会引发错误,因为最后一个 ScreenResult
元素不包含 Type
属性的唯一值。我还应该注意,我确实是在 ScreenResults
中强制每个 Type
之一(ScreenResult
需要存在 3 次,有 3类型的屏幕,我需要独特性)所以如果有更好的方法来实现这一点,我也完全赞成。
谢谢。
这是我的 XML 片段:
<ScreenResults>
<ScreenResult Type="Screen Type A">1</ScreenResult>
<ScreenResult Type="Screen Type B">1</ScreenResult>
<ScreenResult Type="Screen Type B">2</ScreenResult>
</ScreenResults>
这是我的 XSD 片段(另请注意,我的原始 XSD 片段跨越多个文件,但我已验证我的所有命名空间都是正确的):
<xs:element name="ScreenResults" type="import:ScreenResults" minOccurs="0" maxOccurs="1">
<xs:unique name="UniqueScreenResults">
<xs:selector xpath="ScreenResult" />
<xs:field xpath="@Type" />
</xs:unique>
</xs:element>
<!--============ ScreenResults =============-->
<xs:complexType name="ScreenResults">
<xs:sequence minOccurs="1" maxOccurs="1">
<xs:element name="ScreenResult" minOccurs="3" maxOccurs="3">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="enum:ScreenResult">
<xs:attribute name="Type" type="enum:ScreenType" use="required" />
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
<!--============= ScreenType =============-->
<xs:simpleType name="ScreenType">
<xs:restriction base='xs:token'>
<xs:enumeration value='Screen Type A' >
<xs:annotation>
<xs:documentation>1</xs:documentation>
</xs:annotation>
</xs:enumeration>
<xs:enumeration value='Screen Type B' >
<xs:annotation>
<xs:documentation>2</xs:documentation>
</xs:annotation>
</xs:enumeration>
<xs:enumeration value='Screen Type C' >
<xs:annotation>
<xs:documentation>3</xs:documentation>
</xs:annotation>
</xs:enumeration>
</xs:restriction>
</xs:simpleType>
<!--============ ScreenResult ============-->
<xs:simpleType name="ScreenResult">
<xs:restriction base='xs:token'>
<xs:enumeration value='1' >
<xs:annotation>
<xs:documentation>Positive</xs:documentation>
</xs:annotation>
</xs:enumeration>
<xs:enumeration value='2' >
<xs:annotation>
<xs:documentation>Negative</xs:documentation>
</xs:annotation>
</xs:enumeration>
<xs:enumeration value='3' >
<xs:annotation>
<xs:documentation>Not administered</xs:documentation>
</xs:annotation>
</xs:enumeration>
</xs:restriction>
</xs:simpleType>
最佳答案
我正在为遇到此问题的其他人发布我的解决方案。
尽管我断言我的所有命名空间都是正确的,但这当然是问题所在。所有命名空间在唯一元素本身上都是正确的除了。
我错误地认为唯一元素不需要像在上下文中那样为命名空间添加前缀。但事实并非如此。因为我确实为文件声明了一个默认命名空间,所以我仍然需要前缀。
所以我唯一的改变和解决方案如下:
<xs:element name="ScreenResults" type="import:ScreenResults" minOccurs="0" maxOccurs="1">
<xs:unique name="UniqueScreenResults">
<xs:selector xpath="import:ScreenResult" />
<xs:field xpath="@Type" />
</xs:unique>
</xs:element>
关于xsd - 如何通过 xs :unique 使我的枚举具有唯一性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1428886/