本文介绍了如何通过xs:unique使我的枚举明确的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

谁能指出我为什么XSD中的唯一元素没有强迫唯一性?这应该引发错误,因为最后一个 ScreenResult 元素不包含 Type 属性的唯一值.我还应注意,我确实是在强制 ScreenResults 中的每个 Type 之一之后(必须将 ScreenResult 存在3次,其中3次屏幕类型,而且我需要唯一性),因此,如果有更好的方法可以做到这一点,那么我也全力以赴.

Can anyone point me as to why the unique element in my XSD is not forcing unique-ness? This should throw an error because the last ScreenResult element does not contain a unique value for the Type attribute. I should also note that I'm truly after forcing one of each Type within ScreenResults (ScreenResult is required to exist 3 times, there are 3 types of screens and I am requiring unique-ness) so if there is a better way to accomplish that, I'm all for that as well.

谢谢.

这是我的XML代码段:

Here is my XML snippet:

<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代码段跨越了多个文件,但我已经验证了我所有的命名空间都是正确的):

Here is my XSD snippet (also note that my original XSD snippets span multiple files but I have verified all of my namespaces are correct):

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

推荐答案

我将我的解决方案发布给遇到此问题的其他任何人.
尽管我曾断言我所有的名称空间都是正确的,但这当然是问题所在.在唯一元素本身上,所有名称空间都是正确的.
我错误地认为,唯一元素不需要在名称空间中添加前缀,因为它位于上下文中.但事实并非如此.由于确实为文件声明了默认名称空间,因此我仍然需要前缀.

I'm posting my solution for anyone else who runs into this problem.
Although I had asserted that all of my namespaces were correct, that was, of course the problem. All of the namespaces were correct except on the unique element itself.
I erroneously assumed that the unique element would not need to prefix a namespace as it was within the context. But that is not the case. Since I did declare a default namespace for the file, I still needed the prefix.

因此,我唯一的更改以及解决方法如下:

So my only change, and the solution, is as follows:

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

这篇关于如何通过xs:unique使我的枚举明确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-18 18:45