Validate Response Against XSD Schema

Applies to ReadyAPI 3.5, last modified on January 19, 2021

By using a Groovy assertion, you can validate a response against an XSD schema.

Where To Use

You can run this script as a Groovy assertion.

Example

The following script validates a response against a specified XSD schema:

Groovy

import javax.xml.XMLConstants
import javax.xml.transform.stream.StreamSource
import javax.xml.validation.SchemaFactory

// Specify an XSD Schema
def xsdFilePath = "C:\\temp\\Schema.xsd"

// Get the response as XML
def response = messageExchange.getResponse().contentAsXml

// Create validation objects
def factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
def schema = factory.newSchema(new StreamSource(xsdFilePath));
def validator = schema.newValidator();

// Validate the response against the schema
assert validator.validate(new StreamSource(new StringReader(response))) == null;

See Also

Request Editor Interface
Scripting

Highlight search results