XSD error: cvc-complex-type.2.4.a: Invalid Content Was Found Starting With Element

XSD error: cvc-complex-type.2.4.a: Invalid Content Was Found Starting With Element

我有一个 XML 文件需要用 XSD 文件验证,但是当我想用 [=25= 验证我的 XML 时出现此错误] 文件

Cvc-complex-type.2.4.a: Invalid Content Was Found Starting With Element 'ClientData'. One Of '{"http://www.myTest/xml/Import/user/data":ClientData}' Is Expected., Line '2', Column '27'.

这是我的 XML 文件

<?xml version="1.0" encoding="UTF-8"?>
<prefix:UteXmlComunicazione xmlns:prefix="http://www.myTest/xml/Import/user/data">
    <ClientData>
        <client>
            <pfPg>PF</pfPg>
            <Family>Alex White</Family>
            <name></name>
        </client>
        <vendor>
            <Timeperiod>
                <NumberofFactor></NumberofFactor>
                <year>2018</year>
            </Timeperiod>
            <Address>
                <Address1>
                    <top>Via</top>
                    <street>DANTE</street>
                    <number>108</number>
                    <Zipcode>20776</Zipcode>
                    <Code>033032</Code>
                </Address1>
            </Address>
        </vendor>
    </ClientData>
</prefix:UteXmlComunicazione>

这是 XSD

<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" targetNamespace="http://www.myTest/xml/Import/user/data" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="UteXmlComunicazione">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="ClientData" minOccurs="0">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="client" minOccurs="0">
                <xs:complexType>
                  <xs:sequence>
                    <xs:element type="xs:string" name="pfPg"/>
                    <xs:element type="xs:string" name="Family"/>
                    <xs:element type="xs:string" name="name"/>
                  </xs:sequence>
                </xs:complexType>
              </xs:element>
              <xs:element name="vendor" minOccurs="0">
                <xs:complexType>
                  <xs:sequence>
                    <xs:element name="TimePeriod" minOccurs="0">
                      <xs:complexType>
                        <xs:sequence>
                          <xs:element type="xs:short" name="year"/>
                          <xs:element type="xs:byte" name="NumberofFactor"/>
                        </xs:sequence>
                      </xs:complexType>
                    </xs:element>
                    <xs:element name="Address" minOccurs="0">

我不知道问题出在哪里...你能告诉我问题出在哪里吗?

在 XSD 中有 elementFormDefault="qualified" 这使得所有元素都需要限定,而不仅仅是根元素。在 XML 中看到您的 ClientData 上没有 prefix 这不起作用。

因此,或者使所有元素在 XML 中合格,或者更改 de XSD elementFormDefault 值。

https://www.w3schools.com/xml/el_schema.asp

elementFormDefault Optional. The form for elements declared in the target namespace of this schema. The value must be "qualified" or "unqualified". Default is "unqualified". "unqualified" indicates that elements from the target namespace are not required to be qualified with the namespace prefix. "qualified" indicates that elements from the target namespace must be qualified with the namespace prefix

您的直接错误是由于 ClientData 不在 XSD 的目标名称空间中。您似乎打算在根元素上声明一个默认名称空间,但您只更改了根元素的名称空间。

改变

<prefix:UteXmlComunicazione xmlns:prefix="http://www.myTest/xml/Import/user/data">

<UteXmlComunicazione xmlns="http://www.myTest/xml/Import/user/data">

修复您的直接错误。

这是您的 XML 的更新副本,它将根据您的 XSD 的更新副本进行验证。 (Address 元素已被省略,因为您没有在 XSD 中包含它的定义,不值得在这里构建。)

XML

<?xml version="1.0" encoding="UTF-8"?>
<UteXmlComunicazione 
    xmlns="http://www.myTest/xml/Import/user/data"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.myTest/xml/Import/user/data try.xsd"
    >
  <ClientData>
    <client>
      <pfPg>PF</pfPg>
      <Family>Alex White</Family>
      <name></name>
    </client>
    <vendor>
      <TimePeriod>
        <year>2018</year>
        <NumberofFactor>0</NumberofFactor>
      </TimePeriod>
    </vendor>
  </ClientData>
</UteXmlComunicazione>

XSD

<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified"
           targetNamespace="http://www.myTest/xml/Import/user/data"
           xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="UteXmlComunicazione">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="ClientData" minOccurs="0">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="client" minOccurs="0">
                <xs:complexType>
                  <xs:sequence>
                    <xs:element type="xs:string" name="pfPg"/>
                    <xs:element type="xs:string" name="Family"/>
                    <xs:element type="xs:string" name="name"/>
                  </xs:sequence>
                </xs:complexType>
              </xs:element>
              <xs:element name="vendor" minOccurs="0">
                <xs:complexType>
                  <xs:sequence>
                    <xs:element name="TimePeriod" minOccurs="0">
                      <xs:complexType>
                        <xs:sequence>
                          <xs:element type="xs:short" name="year"/>
                          <xs:element type="xs:byte" name="NumberofFactor"/>
                        </xs:sequence>
                      </xs:complexType>
                    </xs:element>
                  </xs:sequence>
                </xs:complexType>
              </xs:element>
            </xs:sequence>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>