Error when using QXmlSchema and QXmlSchemaValidator



  • Hello,

    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 file:ArchiverConfiguration.xml, at line 2, column 6: No definition for element toto available.

    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


  • Lifetime Qt Champion

    Hi,

    Are you running a debug or release version of your application ? If a release then your assert won't help diagnosing if something's wrong.



  • Hi, I think you're right. I'm in a release version. And indeed my schema seems to be invalid. Thanks.


  • Lifetime Qt Champion

    You're welcome !

    Once you have your schema fixed, please mark the thread as solved using the "Topic Tools" button so that other forum users may know a solution has been found :)


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.