2

I have a concern using the validation of an XML file with an XML Schema file. I tried to reproduce the basic sample provided by the documentation, but I have an error.

Here is my XML Schema :

<?xml version="1.0" encoding="UTF-8" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element type="xs:string" name="toto">
  </xs:element>
</xs:schema>

And my XML file to validate :

<?xml version="1.0" encoding="UTF-8" ?>
<toto>titi</toto>

When I try to validate this couple of files using other validation tools (for example http://www.xmlvalidation.com/), it is a success.

But when I run this Qt code, I have an error :

Error XSDError in myFile.xml, at line 2, column 6: No definition for element toto available.

Code :

bool isConfigurationFileValidAgainstSchema(const QString &filePath)
{
  // Retrieve the schema :
  QXmlSchema schema;
  schema.load(MY_XSD_URL);
  // The xsd resource file can't be invalid :
  assert(schema.isValid() && "The  file schema (.xsd) is invalid.");

  // Validate the user file :
  QFile file{filePath};
  file.open(QIODevice::ReadOnly);
  QXmlSchemaValidator validator{schema};
  auto ok = validator.validate(&file, QUrl::fromLocalFile(file.fileName()));
  return ok;
}

The error appears during the line :

auto ok = validator.validate(&file, QUrl::fromLocalFile(file.fileName()));

Would anyone have an idea of ​​the problem?

Thank you, Ben

Ben L.
  • 23
  • 1
  • 5

1 Answers1

0

Your xml is validated fine with Qt 5.8.0 msvc2015 on Windows.

In your code you should check result value of xml schema loading and open xml file operations:

if (!schema.load(MY_XSD_URL))
    qDebug() << "Can't load xsd schema";
    return false;

...

if (!file.open(QIODevice::ReadOnly))
    qDebug() << "Can't open xml file";
    return false;

And also you need to check output from QXmlSchemaValidator's Message Handler. If default is not enough for your needs, you can set custom.

Max Go
  • 2,092
  • 1
  • 16
  • 26
  • Yes of course i need to perform these checks. But here the issue is during the validation, and the error message from the validator is _"Error XSDError in myFile.xml, at line 2, column 6: No definition for element toto available"_. This message that I do not understand. – Ben L. Apr 20 '17 at 09:53
  • that should be because you haven't checked for `schema.load(MY_XSD_URL)` result. Xsd schema was not loaded fine in your case, looks like, so validation failed. Or Qt 5.6.0 doesn't support some parts of used xsd or xml. For example, namespaces of elements. – Max Go Apr 20 '17 at 10:03
  • I checked it. The assert on the next row checks it. "assert(schema.isValid()" is equivalent to "if (!schema.load(MY_XSD_URL)){return false}" ... – Ben L. Apr 20 '17 at 10:13
  • what is the value of MY_XSD_URL ? – Max Go Apr 20 '17 at 11:15
  • QUrl::fromLocalFile(":/resources/MyFile.xsd"); I don't think the issue is with this file because "schema.load" returns true. – Ben L. Apr 20 '17 at 11:44
  • Okay, thank you, loading from resources is working fine for me with Qt 5.8.0 as well, so there is might be some difference in xml support in comparison with Qt 5.6.0 – Max Go Apr 20 '17 at 11:57