Forum Discussion

Target_MN_-_soa's avatar
Target_MN_-_soa
Occasional Contributor
12 years ago

How to validate a xsd file against the xml response.

I want to know how to validate the xsd file against the xml response for rest services.
The following is what i did
1. Browsed and attached the schema directory in WSDL Settings tab in preferences and click ok
2. open the test case and add assertion -> schema compliance (and i gave the path of the schema directory) "H://Schema/Location.xsd". (not sure if i am doing the right thing)
3. Run the test case and it gives me the error compliance - failed - java.lang.NullPointerException

Can somebody help?

3 Replies

  • SmartBear_Suppo's avatar
    SmartBear_Suppo
    SmartBear Alumni (Retired)
    Hi,

    unfortunately the SchemaCompliance assertion only handles WSDL files - not standalone XSDs. You would have to create a script assertion that uses standard Groovy XML Processing code to load your XSD and validate the response message with it.

    regards,

    /Ole
    SmartBear Software
  • Try validating ur XSD's hosted in this way

    1) Make request
    2) Groovy should be used to validate XSD's, by the below groovy script

    import javax.xml.XMLConstants
    import javax.xml.transform.stream.StreamSource
    import javax.xml.validation.*
    // setup validator
    Validator validator;
    def url = "This should the path where u hosted the xsd file"
    log.info url
    URI uri = new URI(url);
    InputStream inp = uri.toURL().openStream();
    try
    {
    SchemaFactory factory =
    SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
    StreamSource entitySs = new StreamSource(inp);
    Schema schema = factory.newSchema(entitySs);
    assert(schema != null);
    validator = schema.newValidator();
    }
    finally
    {
    inp.close();
    inp = null;
    }



    def response = context.expand( 'This should be XML response u want to validate against' )



    log.info response
    validator.validate(new StreamSource(new StringReader(response)))

    Let me know if it helps?


    Abhinav