Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

XML schema validation



  • Hi,

    I am trying to get QT to validate an xml document using the schema contained in the document.
    I do not have a schemaLocation attribute because in different stages of the processing the schema is stored in different location (e.g. a database), so I just have the 'logical name' of the schema.

    What I would like to do is ask QT to validate the xml document and intercept the schema retrieval to provide the schema.

    The code below fails with the following output:

    Error XSDError in file:///C:/Users/dev/source/repos/QtConsoleApplication2/x64/Debug/QtConsoleApplication2.exe, at line 1, column 67: No definition for element {foo/bar}test available.
    instance document is invalid
    
        QByteArray data(
            "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
            "<test "
            "      xmlns=\"foo/bar\" "
            ">"
            "</test>");
    
        QBuffer buffer(&data);
        buffer.open(QIODevice::ReadOnly);
    
        class MyUriResolver : public QAbstractUriResolver
        {
            QUrl resolve(const QUrl &relative, const QUrl &baseURI) const
            {
                assert(false);// never called
                return QUrl();
            }
        };
    
        QXmlSchemaValidator validator;
        validator.setUriResolver(new MyUriResolver());
        if (validator.validate(&buffer))
            qDebug() << "instance document is valid";
        else
            qDebug() << "instance document is invalid";
    
    

    I would be grateful for any help.

    Henning



  • @_Henning_
    You're not asking why you get the error you get with the input you gave it, are you?
    You're using it as an illustration of where you would like to supply the schema for validation yourself, is that right?



  • @JonB
    you're not asking why you get the error you get with the input you gave it, are you?
    Yes

    You're using it as an illustration of where you would like to supply the schema for validation yourself, is that right?
    Yes

    The point is: I don't want to look into the xml file myself. Of course I could check inside the xml file, extract the xmlns attribute, lookup the schema location, load the schema, and validate then using the loaded schema. This is the workaround I am currently using but to me it feels very very ugly.
    So I am looking for a better way to do this. I was hoping that QT, when validating the xml file sees a namespace and will use the UriResolver to map it to a real QUrl.
    For me the api would be even better if I could return a Schema from the resolver. So QT asked me: to validate, please provide the schema 'foo/bar', so I return the schema.

    To me the use case does't seem very exotic. So I am not sure if I really understood the concepts or if QT just doesn't provide support for that case.


Log in to reply