Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. XML schema validation
QtWS25 Last Chance

XML schema validation

Scheduled Pinned Locked Moved Unsolved General and Desktop
3 Posts 2 Posters 607 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • _ Offline
    _ Offline
    _Henning_
    wrote on 19 Feb 2019, 12:58 last edited by
    #1

    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

    J 1 Reply Last reply 19 Feb 2019, 18:50
    0
    • _ _Henning_
      19 Feb 2019, 12:58

      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

      J Offline
      J Offline
      JonB
      wrote on 19 Feb 2019, 18:50 last edited by
      #2

      @_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?

      1 Reply Last reply
      0
      • _ Offline
        _ Offline
        _Henning_
        wrote on 27 Feb 2019, 09:57 last edited by
        #3

        @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.

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved