XSD xs:选择不应该允许空选择

时间:2015-11-11 12:30:19

标签: java c# xml xsd

我的XSD有问题。我的XSD包含一个xs:choice来实现选择其中一种类型的选项。不应该允许选择No Type(或Null)!

<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.exe从xsd创建了一个.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>

我在选择时尝试了minOccurs="1"nillable="false",但是Schema接受了空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 个答案:

答案 0 :(得分:1)

您的XML实际上 对您的XSD有效:

  

[错误] try.xml:9:24:cvc-complex-type.2.4.b:元素的内容   'TypeAndValue'不完整。 '{SimpleType,ArrayType,   ListType,ClassType}'是预期的。

由于这符合您的期望,您应该怀疑验证XML的方法。确保您使用的是验证解析器并且已启用验证。