我正在嘗試使用給定的 XSD 驗證 XML。但我不斷收到以下錯誤。
cvc-elt.4.2:無法將“MyType”決議為元素“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>
uj5u.com熱心網友回復:
給出的型別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 使xsi:typeXML 中的屬性變得多余。演示使用的示例 XSDxsi: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>
With this second XSD, the xsi:type attribute in the XML causes the type of the Test element to change from its default of TestType. Here too the effect is inconsequential, but one could expand the MyType to differ from TestType. The constraint is that it MyType must still derive from TestType.
See also
- Example of extending complex types in XSD?
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/434188.html
下一篇:遞回屬性計算?
