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>
CodePudding user response:
The type given by xsi:type
has to be derivable from the type specified in the XSD, yet your XSD doesn't even define a MyType
type.
Globally define a MyType
type and make sure it's derivable from the type of the Test
element.
See also
- How to restrict the value of an XML element using xsi:type in XSD?
- Use datatype directly with in XML
Update per OP comment:
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.
This XSD would be valid against the unchanged 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>
Note that the above XSD renders the xsi:type
attribute in the XML redundant. An example XSD which demonstrates use of xsi:type
is as follows:
<?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
.