无法解析“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>
I'm trying to validate a XML with a given XSD. But I'm continuously getting the following error.
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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
xsi:type
给出的类型必须可以从 XSD 中指定的类型派生,但您的 XSD 甚至没有定义MyType
类型。全局定义一个
MyType
类型,并确保它可以从Test
元素的类型派生。另请参阅
根据 OP 评论更新:
此 XSD 对于未更改的 XML 是有效的:
请注意,上述 XSD 使 XML 中的
xsi:type
属性变得多余。演示xsi:type
用法的示例 XSD 如下:对于第二个 XSD,XML 中的
xsi:type
属性导致Test 的类型
元素从默认的TestType
更改。这里的效果也是无关紧要的,但可以扩展MyType
以不同于TestType
。限制是MyType
仍必须派生自TestType
。另请参阅
The type given by
xsi:type
has to be derivable from the type specified in the XSD, yet your XSD doesn't even define aMyType
type.Globally define a
MyType
type and make sure it's derivable from the type of theTest
element.See also
Update per OP comment:
This XSD would be valid against the unchanged XML:
Note that the above XSD renders the
xsi:type
attribute in the XML redundant. An example XSD which demonstrates use ofxsi:type
is as follows:With this second XSD, the
xsi:type
attribute in the XML causes the type of theTest
element to change from its default ofTestType
. Here too the effect is inconsequential, but one could expand theMyType
to differ fromTestType
. The constraint is that itMyType
must still derive fromTestType
.See also