xsd schema error从元素开始找到无效的内容,此时不需要子元素

时间:2015-10-20 20:41:45

标签: c# asp.net xml xsd

我正在创建一个针对输入xml的模式,其中我的主要要求是强制要求ParcelNumberWorkArea所以这是我的输入xml

<?xml version="1.0" encoding="utf-8"?>
<NOCPlantMapRequest xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <NOCTypeID>0</NOCTypeID>
  <WorkLocation>
    <ParcelNumber>4545</ParcelNumber>
    <Roads>
      <WorkLocationRoad>
        <RoadName>chennai road</RoadName>
      </WorkLocationRoad>
    </Roads>
    <WorkArea>
      <WorkArea>
        <Coordinates>
          <WorkLocationCoordinate>
            <CoordinateX>56</CoordinateX>
            <CoordinateY>23</CoordinateY>
          </WorkLocationCoordinate>
        </Coordinates>
        <Communities />
      </WorkArea>
    </WorkArea>
  </WorkLocation>
</NOCPlantMapRequest>

以下是我为验证xml

而创建的架构
<?xml version="1.0" encoding="utf-8"?>
<xsd:schema
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:xsd="http://www.w3.org/2001/XMLSchema"
    attributeFormDefault="unqualified"
    elementFormDefault="qualified">
  <xsd:element name="NOCPlantMapRequest">
    <xsd:complexType>
      <xsd:sequence>
        <xsd:element name="NOCReference" minOccurs="0" type="xsd:string" />
        <xsd:element name="NOCTypeID" minOccurs="0" type="xsd:unsignedByte" />
        <xsd:element name="NOCTypeName" minOccurs="0" type="xsd:string" />
        <xsd:element name="ApplicationName" minOccurs="0" type="xsd:string" />
        <xsd:element name="Applicationtype" minOccurs="0" type="xsd:string" />
        <xsd:element name="RelatedNOCRefNumber" minOccurs="0" type="xsd:string" />
        <xsd:element name="WorkLocation" minOccurs="1" maxOccurs="1"  type="LocationType">
        </xsd:element>
      </xsd:sequence>
    </xsd:complexType>
  </xsd:element>
  <xsd:complexType name="LocationType">
    <xsd:choice>
      <xsd:sequence>
        <xsd:element name="ParcelNumber" type="ParcelNumberType" />
      </xsd:sequence>
      <xsd:sequence>
        <xsd:element name="WorkArea" type="WorkAreaType" />
      </xsd:sequence>
    </xsd:choice>
  </xsd:complexType>
  <xsd:simpleType name="ParcelNumberType">
    <xsd:restriction base="xsd:string"/>
  </xsd:simpleType>
  <xsd:complexType name="WorkAreaType">
    <xsd:sequence>
      <xsd:element name="WorkArea" minOccurs="0" maxOccurs="unbounded">
        <xsd:complexType>
          <xsd:sequence>
            <xsd:element name="Coordinates" minOccurs="1" type="CoordinatesType" />
          </xsd:sequence>
        </xsd:complexType>
      </xsd:element>
    </xsd:sequence>
  </xsd:complexType>
  <xsd:complexType name="CoordinatesType">
    <xsd:sequence>
      <xsd:element name="WorkLocationCoordinate" type="WorkLocationCoordinateType"/>
    </xsd:sequence>
  </xsd:complexType>
  <xsd:complexType name="WorkLocationCoordinateType">
    <xsd:sequence>
      <xsd:element name="CoordinateX" type="xsd:string" />
      <xsd:element name="CoordinateY" type="xsd:string" />
    </xsd:sequence>
  </xsd:complexType>
</xsd:schema>

但是我得到了像

这样的错误
Error - Line 6, 12: org.xml.sax.SAXParseException; lineNumber: 6; columnNumber: 12; cvc-complex-type.2.4.d: Invalid content was found starting with element 'Roads'. No child element is expected at this point.
Error - Line 19, 24: org.xml.sax.SAXParseException; lineNumber: 19; columnNumber: 24; cvc-complex-type.2.4.d: Invalid content was found starting with element 'Communities'. No child element is expected at this point.

我查看了visual studio和xml-xsd validation tool

1 个答案:

答案 0 :(得分:3)

在.xsd中,LocationType只接受ParcelNumber或WorkArea,道路或社区不在任何地方。

在xsd:choice中有2个xsd:序列也有点不寻常,通常你会这样做:

<xsd:complexType name="LocationType">
  <xsd:all>
    <xsd:element name="ParcelNumber" type="ParcelNumberType" maxOccurs="1"/>
    <xsd:element name="WorkArea" type="WorkAreaType" maxOccurs="1"/>
  </xsd:all>
</xsd:complexType>

这是一个完整的XSD,应该验证您的XML示例:

<?xml version="1.0" encoding="utf-8"?>
<xsd:schema xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"
            attributeFormDefault="unqualified" elementFormDefault="qualified">
    <xsd:element name="NOCPlantMapRequest">
        <xsd:complexType>
            <xsd:sequence>
                <xsd:element name="NOCReference" minOccurs="0" type="xsd:string"/>
                <xsd:element name="NOCTypeID" minOccurs="0" type="xsd:unsignedByte"/>
                <xsd:element name="NOCTypeName" minOccurs="0" type="xsd:string"/>
                <xsd:element name="ApplicationName" minOccurs="0" type="xsd:string"/>
                <xsd:element name="Applicationtype" minOccurs="0" type="xsd:string"/>
                <xsd:element name="RelatedNOCRefNumber" minOccurs="0" type="xsd:string"/>
                <xsd:element name="WorkLocation" minOccurs="1" maxOccurs="1" type="LocationType"></xsd:element>
            </xsd:sequence>
        </xsd:complexType>
    </xsd:element>
    <xsd:complexType name="LocationType">
        <xsd:all>
            <xsd:element name="ParcelNumber" type="ParcelNumberType" maxOccurs="1"/>
            <xsd:element name="WorkArea" type="WorkAreaType" maxOccurs="1"/>
            <xsd:element name="Roads" type="RoadListType" maxOccurs="1"/>
        </xsd:all>
    </xsd:complexType>

    <xsd:complexType name="RoadListType">
        <xsd:sequence>
            <xsd:element name="WorkLocationRoad" type="WorkLocationRoadType" minOccurs="0" maxOccurs="unbounded"/>
        </xsd:sequence>
    </xsd:complexType>

    <xsd:complexType name="WorkLocationRoadType">
        <xsd:sequence>
            <xsd:element name="RoadName" type="xsd:string"/>
        </xsd:sequence>
    </xsd:complexType>

    <xsd:complexType name="CommunitiesListType">
        <xsd:sequence>
            <xsd:element name="WorkLocationRoad" type="WorkLocationRoadType" minOccurs="0" maxOccurs="unbounded"/>
        </xsd:sequence>
    </xsd:complexType>


    <xsd:simpleType name="ParcelNumberType">
        <xsd:restriction base="xsd:string"/>
    </xsd:simpleType>

    <xsd:complexType name="WorkAreaType">
        <xsd:sequence>
            <xsd:element name="WorkArea" minOccurs="0" maxOccurs="unbounded">
                <xsd:complexType>
                    <xsd:sequence>
                        <xsd:element name="Coordinates" minOccurs="1" type="CoordinatesType"/>
                        <xsd:element name="Communities" type="CommunitiesListType" maxOccurs="1"/>
                    </xsd:sequence>
                </xsd:complexType>
            </xsd:element>
        </xsd:sequence>
    </xsd:complexType>

    <xsd:complexType name="CoordinatesType">
        <xsd:sequence>
            <xsd:element name="WorkLocationCoordinate" type="WorkLocationCoordinateType"/>
        </xsd:sequence>
    </xsd:complexType>

    <xsd:complexType name="WorkLocationCoordinateType">
        <xsd:sequence>
            <xsd:element name="CoordinateX" type="xsd:string"/>
            <xsd:element name="CoordinateY" type="xsd:string"/>
        </xsd:sequence>
    </xsd:complexType>
</xsd:schema>