2015-11-11 90 views
2

我的XSD存在問題。我的XSD包含一個xs:choice以實現選擇這些類型中的一個的選項。不應該允許選擇類型(或空值)!XSD xs:選擇不應該允許空選擇

<xs:complexType name="TypeAndValue"> 
<xs:choice minOccurs="1"> 
    <xs:element minOccurs="1" name="SimpleType" nillable="false" type="SimpleDataTypeAndValue" /> 
    <xs:element minOccurs="1" name="ArrayType" nillable="false" type="ArrayTypeAndValue" /> 
    <xs:element minOccurs="1" name="ListType" nillable="false" type="ListTypeAndValue" /> 
    <xs:element minOccurs="1" name="ClassType" nillable="false" type="IdentifierTypeAndValue" /> 
</xs:choice> 

我創建從XSD通過XSD.EXE一個cs文件。這工作正常,沒有錯誤。但是,如果我嘗試一個XML文件中像這樣的XML將被接受:

<?xml version="1.0"?> 
<Block xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> 
    <Class Identifier="Test"> 
     <Declaration Identifier="List"> 
     <TypeAndValue> 
     </TypeAndValue> 
     </Declaration> 
    </Class> 
</Block> 

我試過和nillable="false"在選擇和元素,但架構接受空TypeAndValue塊。

難道是我需要修改cs文件嗎?

編輯: 下面是完整的XSD:

<?xml version="1.0" encoding="utf-8"?> 
<xs:schema elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema"> 
    <xs:element name="Block" nillable="true" type="Block" /> 
    <xs:complexType name="Block"> 
    <xs:sequence> 
     <xs:element minOccurs="0" maxOccurs="unbounded" name="Class" nillable="true" type="Class" /> 
    </xs:sequence> 
    </xs:complexType> 
    <xs:complexType name="Class"> 
    <xs:sequence> 
     <xs:element minOccurs="0" maxOccurs="unbounded" name="Declaration" nillable="true" type="Declaration" /> 
    </xs:sequence> 
    <xs:attribute name="Accessmodifier" type="Accessmodifier" /> 
    <xs:attribute name="Identifier" type="xs:string" use="required"/> 
    </xs:complexType> 
    <xs:complexType name="Declaration"> 
    <xs:sequence> 
     <xs:element minOccurs="0" maxOccurs="unbounded" name="Modifier" nillable="true" type="Modifier" /> 
     <xs:element minOccurs="1" maxOccurs="1" name="TypeAndValue" type="TypeAndValue" /> 
    </xs:sequence> 
    <xs:attribute name="Accessmodifier" type="Accessmodifier" /> 
    <xs:attribute name="Identifier" type="xs:string" use="required" /> 
    </xs:complexType> 
    <xs:simpleType name="Modifier"> 
    <xs:restriction base="xs:string"> 
     <xs:enumeration value="Static"/> 
     <xs:enumeration value="Constant"/> 
    </xs:restriction> 
    </xs:simpleType> 
    <xs:simpleType name="Accessmodifier"> 
    <xs:restriction base="xs:string"> 
     <xs:enumeration value="Public"/> 
     <xs:enumeration value="Private"/> 
     <xs:enumeration value="Internal"/> 
    </xs:restriction> 
    </xs:simpleType> 
    <xs:complexType name="SimpleDataTypeAndValue"> 
    <xs:attribute name="Type" type="SimpleType" use="required"/> 
    <xs:attribute name="Value" type="xs:string" /> 
    </xs:complexType> 
    <xs:complexType name="IdentifierTypeAndValue"> 
    <xs:attribute name="Type" type="xs:string"/> 
    </xs:complexType> 
    <xs:complexType name="ArrayTypeAndValue"> 
    <xs:choice> 
     <xs:element minOccurs="1" maxOccurs="1" name="Dimension" type="ArrayDimension" /> 
     <xs:element minOccurs="1" maxOccurs="1" name="DataInitialization" type="ArrayDataInitialization" /> 
    </xs:choice> 
    <xs:attribute name="Type" type="SimpleType" use="required"/> 
    </xs:complexType> 
    <xs:complexType name="ArrayDimension"> 
    <xs:attribute name="Amount" type="xs:int" use="required"/> 
    </xs:complexType> 
    <xs:complexType name="ArrayDataInitialization"> 
    <xs:sequence> 
     <xs:element minOccurs="0" maxOccurs="unbounded" name="Value" type="xs:string" /> 
    </xs:sequence> 
    </xs:complexType> 
    <xs:complexType name="ListTypeAndValue"> 
    <xs:sequence> 
     <xs:element minOccurs="0" maxOccurs="unbounded" name="Value" type="xs:string" /> 
    </xs:sequence> 
    <xs:attribute name="Type" type="SimpleType" use="required" /> 
    </xs:complexType> 
    <xs:complexType name="TypeAndValue"> 
    <xs:choice minOccurs="1"> 
     <xs:element minOccurs="1" name="SimpleType" nillable="false" type="SimpleDataTypeAndValue" /> 
     <xs:element minOccurs="1" name="ArrayType" nillable="false" type="ArrayTypeAndValue" /> 
     <xs:element minOccurs="1" name="ListType" nillable="false" type="ListTypeAndValue" /> 
     <xs:element minOccurs="1" name="ClassType" nillable="false" type="IdentifierTypeAndValue" /> 
    </xs:choice> 
    </xs:complexType> 
    <xs:simpleType name="SimpleType"> 
    <xs:restriction base="xs:string"> 
     <xs:enumeration value="Int"/> 
     <xs:enumeration value="Float"/> 
     <xs:enumeration value="Double"/> 
     <xs:enumeration value="Bool"/> 
     <xs:enumeration value="String"/> 
     <xs:enumeration value="Char"/> 
     <xs:enumeration value="Byte"/> 
     <xs:enumeration value="Short"/> 
     <xs:enumeration value="Long"/> 
     <xs:enumeration value="Date"/> 
    </xs:restriction> 
    </xs:simpleType> 
    <xs:element name="Class" nillable="true" type="Class" /> 
    <xs:element name="Declaration" nillable="true" type="Declaration" /> 
</xs:schema> 

回答

1

你的XML實際上是有效防止您的XSD:

[錯誤] try.xml:9:24 :cvc-complex-type.2.4.b:元素 'TypeAndValue'的內容不完整。預計會出現'{SimpleType,ArrayType, ListType,ClassType}'之一。

由於這是您的期望,您應該懷疑驗證XML的方法。確保您使用的是驗證解析器,並且驗證已打開。