【问题标题】:xsd schema error Invalid content was found starting with element, No child element is expected at this point [duplicate]xsd架构错误发现以元素开头的内容无效,此时不应有子元素[重复]
【发布时间】:2016-01-19 16:32:35
【问题描述】:

我正在针对输入 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.

我检查了视觉工作室和xml-xsd validation tool

【问题讨论】:

标签: c# asp.net xml xsd


【解决方案1】:

在您的 .xsd 中,LocationType 仅接受 ParcelNumber 或 WorkArea,没有任何地方可以找到 Roads 或 Communities。

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

<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>

【讨论】:

  • 你是冠军,让我做进一步的测试,虽然在接受答案之前最初是成功的
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-09-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多