views:

190

answers:

1

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. Thank you.

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>

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>
+1  A: 

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>
heath
Thanks for posting your solution, I just had exactly the same problem.
Heinzi