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



我正在尝试使用给定的XSD验证XML。但我一直在犯以下错误。

cvc elt.4.2:无法将"MyType"解析为元素的类型定义"测试"。

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元素的类型派生。

另请参阅

  • 如何在XSD中使用xsi:type限制XML元素的值
  • 在XML中直接与一起使用数据类型

根据OP注释更新:

你能告诉我我的XSD需要如何更新吗?我真的不在乎这个类型属性。因此,只想使XSD验证成功。请注意,我不能更改XML结构,因为它来自外部系统。

此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派生。

另请参阅

  • 在XSD中扩展复杂类型的示例