XSD验证期间出现错误cvc-complex-type.2.4.d

时间:2018-12-31 17:07:11

标签: c# xml xsd

我正在尝试使用XSD架构验证某些XML文件,但出现错误cvc-complex-type.2.4.d:发现从元素“ Number”开始的无效内容。此时,不希望有任何子元素。第31行,第11列。

这涉及字段NumberDescription都从PrintableText类继承。

我已经尝试过更改minOccurs对象中的maxOccurs<xs:element />,但这会导致相同的行为。

XML文件:

<?xml version="1.0"?>
<Receipt>
  <Items>
    <FiscalItem>
      <Name>Przedmiot 1</Name>
      <ReturnablePackaging>false</ReturnablePackaging>
      <Quantity>1</Quantity>
      <PTU>A</PTU>
      <OriginalPrice>1.0</OriginalPrice>
      <Price>1.0</Price>
    </FiscalItem>
    <FiscalItem>
      <Name>Przedmiot 2</Name>
      <ReturnablePackaging>false</ReturnablePackaging>
      <Quantity>1</Quantity>
      <PTU>B</PTU>
      <OriginalPrice>1.0</OriginalPrice>
      <Price>5.0</Price>
    </FiscalItem>
  </Items>
  <Tedners>
    <FiscalTender>
      <Type>CASH</Type>
      <Amount>5.0</Amount>
    </FiscalTender>
    <FiscalTender>
      <Type>CREDITCARD</Type>
      <Amount>5.0</Amount>
    </FiscalTender>
  </Tedners>
  <!-- VVV THIS IS LINE VVV -->
  <Description>
    <Value>180</Value>
  </Description>
  <Number>
    <Value>12345</Value>
  </Number>
</Receipt>

XSD文件:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="Receipt" nillable="true" type="Receipt" />
  <xs:complexType name="Receipt">
    <xs:complexContent mixed="false">
      <xs:extension base="FiscalDocumentBase">
        <xs:sequence>
          <xs:element minOccurs="0" maxOccurs="1" name="Number" type="ReceiptNumber" />
          <xs:element minOccurs="0" maxOccurs="1" name="Description" type="ReceiptDescription" />
        </xs:sequence>
      </xs:extension>
    </xs:complexContent>
  </xs:complexType>
  <xs:complexType name="FiscalDocumentBase" abstract="true">
    <xs:sequence>
      <xs:element minOccurs="0" maxOccurs="unbounded" nillable="true" name="Items" type="ArrayOfFiscalItem" />
      <xs:element minOccurs="0" maxOccurs="unbounded" nillable="true" name="Tedners" type="ArrayOfFiscalTender" />
    </xs:sequence>
  </xs:complexType>
  <xs:complexType name="ArrayOfFiscalItem">
    <xs:sequence>
      <xs:element minOccurs="0" maxOccurs="unbounded" name="FiscalItem" nillable="true" type="FiscalItem" />
    </xs:sequence>
  </xs:complexType>
  <xs:complexType name="FiscalItem">
    <xs:sequence>
      <xs:element minOccurs="0" maxOccurs="1" name="Name" type="xs:string" />
      <xs:element minOccurs="1" maxOccurs="1" name="ReturnablePackaging" type="xs:boolean" />
      <xs:element minOccurs="1" maxOccurs="1" name="Quantity" type="xs:decimal" />
      <xs:element minOccurs="0" maxOccurs="1" name="PTU" type="xs:string" />
      <xs:element minOccurs="1" maxOccurs="1" name="OriginalPrice" type="xs:decimal" />
      <xs:element minOccurs="1" maxOccurs="1" name="Price" type="xs:decimal" />
    </xs:sequence>
  </xs:complexType>
  <xs:complexType name="ArrayOfFiscalTender">
    <xs:sequence>
      <xs:element minOccurs="0" maxOccurs="unbounded" name="FiscalTender" nillable="true" type="FiscalTender" />
    </xs:sequence>
  </xs:complexType>
  <xs:complexType name="FiscalTender">
    <xs:sequence>
      <xs:element minOccurs="1" maxOccurs="1" name="Type" type="FiscalTenderType" />
      <xs:element minOccurs="1" maxOccurs="1" name="Amount" type="xs:decimal" />
    </xs:sequence>
  </xs:complexType>
  <xs:simpleType name="FiscalTenderType">
    <xs:restriction base="xs:string">
      <xs:enumeration value="CASH" />
      <xs:enumeration value="CREDITCARD" />
      <xs:enumeration value="CHECK" />
      <xs:enumeration value="BOND" />
      <xs:enumeration value="CREDIT" />
      <xs:enumeration value="OTHER" />
      <xs:enumeration value="VOUCHER" />
      <xs:enumeration value="ACCOUNT" />
    </xs:restriction>
  </xs:simpleType>
  <xs:complexType name="PrintableText">
    <xs:simpleContent>
      <xs:extension base="xs:string" />
    </xs:simpleContent>
  </xs:complexType>
  <xs:complexType name="ReceiptNumber">
    <xs:simpleContent>
      <xs:extension base="PrintableText" />
    </xs:simpleContent>
  </xs:complexType>
  <xs:complexType name="ReceiptDescription">
    <xs:simpleContent>
      <xs:extension base="PrintableText" />
    </xs:simpleContent>
  </xs:complexType>
</xs:schema>

编辑:删除Number字段会导致通过验证。

1 个答案:

答案 0 :(得分:0)

好吧...所以我的朋友找到了问题。在XSD架构中,元素的顺序应该不同。

原文:

...
<xs:element minOccurs="0" maxOccurs="1" name="Number" type="ReceiptNumber" />
<xs:element minOccurs="0" maxOccurs="1" name="Description" type="ReceiptDescription" />
...

已修复:

...
<xs:element minOccurs="0" maxOccurs="1" name="Description" type="ReceiptDescription" />
<xs:element minOccurs="0" maxOccurs="1" name="Number" type="ReceiptNumber" />    
...