Layer7 API Management

 View Only
  • 1.  XML Schema Validation against multiple candidates

    Posted Mar 14, 2017 09:39 AM

    Let's assume

    • there is a message format that exists in multiple versions (1.0, 1.1, 2.0 etc)
    • each version has a dedicated schema definition
    • all message versions are sent to the same channel (mixed versions)


    When I implement a service to consume these messages, what is the best practice to validate the XML schema of the individual messages?


    The most simple approach would be to just validate the message against ALL schema versions in an "at least one". 


    This works fine, but has the downside that if I get an invalid message with version 2.0 the resulting validation message is not very helpful since it would be the message(s) from the validation against version 2.1. 


    So the question finally is: How can I validate only against the matching schema version?


    Do I have to extract the document namespace and, based on that, build a "switch-statement" to select the correct schema validation? Or is there are simpler way?




  • 2.  Re: XML Schema Validation against multiple candidates

    Posted Mar 14, 2017 10:40 AM

    Your idea of using a "switch-statement" seems like the right way to go here. You can extract the document namespace and put that into a Context Variable to be used for comparison. The use a Compare Variable assertion within 'All assertions must evaluate to true' assertions to determine which schema you want to validate against. The last assertion within the VALIDATE SCHEMA (At least one...) assertion will handle your error condition where no matching schema version is found. Something like this:

  • 3.  Re: XML Schema Validation against multiple candidates
    Best Answer

    Posted Mar 14, 2017 11:38 AM

    Thanks Bill 


    Just one small addition: The last "case" in the VALIDATE SCHEMA (At least one...) "switch" statement is not only an unknown version, but also error handling in general. 


    When the version is identified, but the validation was not successful, the last "case" must also handle it. It would probably check for validation errors. If present, a validation happened, but with errors. If not present no validation happened, so the version is unknown.


    With this strategy we just don't know what version was identified. To solve this, every version would have to set a version variable before validating the message.


    By the way: this is quite a bit of policy logic to build for such a simple case. It would be nice to have an assertion that takes a collection of schemas (with different namespaces) and uses the one with the matching namespace for validation.