0

I have been trying to import an XML file using a XSD to validate it. However when I try to import the XML file it fails. Then I compared the XML with the XSD file and found that the XML doesn't have all the elements specified in the XSD file. Is this what is causing the problem? Should an XML file necessarily use all the elements of the XSD file?

Consider the XSD below:

<?xml version="1.0" encoding="UTF-8" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:complexType>
    <xs:element name="PersonInfo">
    <xs:element name="orderperson" type="xs:string"/>
    <xs:element name="name" type="xs:string"/>
    <xs:element name="address" type="xs:string"/>
    <xs:element name="city" type="xs:string"/>
    <xs:element name="country" type="xs:string"/>
    <xs:element name="title" type="xs:string"/>
    <xs:element name="note" type="xs:string"/>
    <xs:element name="quantity" type="xs:positiveInteger"/>
    <xs:element name="price" type="xs:decimal"/>
</xs:complexType>
</xs:element>
</xs:schema>

And I create an XML which contains only below elements:

<PersonInfo>
    <name>Mark</name>
    <title>XYZ</title>
    <quantity>10</quantity>
    <price>100</price>
</PersonInfo>

Is this a valid implementation of the XSD? When I try to import the XML file through loadXML() function, the function returns S_FALSE? I am not able to understand why the import is failing.

vsk
  • 35
  • 5
  • The question is rather broad. It would be easier to answer for your specific case if you shared the XSD (or at least the snippet defining any of the elements that you are getting errors for). Also, if you could post the validation error message that is returned. – Mads Hansen Sep 30 '18 at 11:48
  • @MadsHansen: I have edited the question. Also I am trying to import the XML using Visual Studio using the loadXML() function. This particular function returns S_FALSE when I try to import the XML. – vsk Sep 30 '18 at 12:22
  • That is not an XML file. You will get XML parse errors before you get any Schema validation errors. An XML file must have a single root element. – Mads Hansen Sep 30 '18 at 12:53
  • Your XML is not well-formed. (An XML document may only have a single root element.) Your XSD is invalid. Your use of terminology is non-standard (*import an XML file*) and unclear (*fails* how?), so you're having trouble being understood. I'm closing your question as a duplicate of the closest questions I could find that might help you. If those don't help, read basic tutorials on XML and XSD as well as [ask] before retrying to ask here. Thanks. – kjhughes Sep 30 '18 at 22:09

1 Answers1

0

An XSD schema says which elements in the XML are mandatory and which are optional. If the XSD says that an element is required, then the XML instance is invalid if it is absent.

Michael Kay
  • 138,236
  • 10
  • 76
  • 143
  • How do we get to know that the elements are mandatory and optional? Can you shed some light on that? Thanks in Advance – vsk Sep 30 '18 at 12:05
  • The XML Schema spec is very complicated and I'm not going to try and give you a potted summary here. There are books and tutorials you can read. They won't be easy reading. – Michael Kay Sep 30 '18 at 16:14
  • Your example schema is rather unrealistic because it allows no nested elements: all the elements listed have a simple type such as xs:decimal, and none can contain other elements. – Michael Kay Sep 30 '18 at 17:42