无法将 'MyType' 解析为元素 'Test' 的类型定义

Cannot resolve 'MyType' to a type definition for element 'Test'

我正在尝试用给定的 XSD 验证 XML。但我不断收到以下错误。

cvc-elt.4.2: Cannot resolve 'MyType' to a type definition for element 'Test'.

XML

<notifications xmlns="http://soap.sforce.com/2005/09/outbound">
  <Notification>
    <Test xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="MyType">
       <Id>a2L1g000000OzM7EAK</Id>
     </Test>
  </Notification>
</notifications>

XSD

<xs:schema xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" attributeFormDefault="unqualified" elementFormDefault="qualified" targetNamespace="http://soap.sforce.com/2005/09/outbound" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="notifications">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="Notification">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="Test">
                <xs:complexType>
                  <xs:sequence>
                    <xs:element name="Id" type="xs:string" />
                  </xs:sequence>
                  <xs:anyAttribute processContents="skip"/>
                </xs:complexType>
              </xs:element>
            </xs:sequence>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

xsi:type 给出的类型必须从 XSD 中指定的类型派生而来,但您的 XSD 甚至没有定义 MyType 类型。

全局定义 MyType 类型并确保它可从 Test 元素的类型派生。

另见


根据 OP 评论更新:

Can you kindly let me know how my XSD needs to be updated? I really don't care about this type attribute. So simply want to make the XSD validation a success. Please note that I can't change the XML structure as it's coming from an external system.

此 XSD 对未更改的 XML:

有效
<?xml version="1.0" encoding="utf-8" ?>
<xs:schema xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           attributeFormDefault="unqualified"
           elementFormDefault="qualified"
           targetNamespace="http://soap.sforce.com/2005/09/outbound"
           xmlns:ob="http://soap.sforce.com/2005/09/outbound"
           xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="notifications">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="Notification">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="Test" type="ob:MyType"/>
            </xs:sequence>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
  <xs:complexType name="MyType">
    <xs:sequence>
      <xs:element name="Id" type="xs:string" />
    </xs:sequence>
    <xs:anyAttribute processContents="skip"/>
  </xs:complexType>
</xs:schema>

请注意,上面的 XSD 使 XML 中的 xsi:type 属性变得多余。 XSD 示例 xsi:type 的用法如下:

<?xml version="1.0" encoding="utf-8" ?>
<xs:schema xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           attributeFormDefault="unqualified"
           elementFormDefault="qualified"
           targetNamespace="http://soap.sforce.com/2005/09/outbound"
           xmlns:ob="http://soap.sforce.com/2005/09/outbound"
           xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="notifications">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="Notification">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="Test" type="ob:TestType"/>
            </xs:sequence>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
  <xs:complexType name="TestType">
    <xs:sequence>
      <xs:element name="Id" type="xs:string" />
    </xs:sequence>
    <xs:anyAttribute processContents="skip"/>
  </xs:complexType>
  <xs:complexType name="MyType">
    <xs:complexContent>
      <xs:extension base="ob:TestType"/>
    </xs:complexContent>
  </xs:complexType>
</xs:schema>

对于第二个 XSD,XML 中的 xsi:type 属性导致 Test 元素的类型从其默认值 TestType 更改.这里的效果也是无关紧要的,但可以扩展 MyType 以不同于 TestType。约束是它 MyType 仍然必须派生自 TestType.

另见