Pallavibhaskar
15 years agoOccasional Contributor
How to validate response xml with an xsd file
Hi,
I tried using this script assertion to validate an xml response, with the xsd file as:
import javax.xml.XMLConstants
import javax.xml.transform.stream.StreamSource
import javax.xml.validation.SchemaFactory
//Load the XSD from a file
def xsd = new File('/Users/pbhaskar/Desktop/error.xsd').text
//Get the XML from the response
def xmlHolder = new com.eviware.soapui.support.XmlHolder( messageExchange.responseContentAsXml )
def xml = xmlHolder['//error']
def factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI)
def schema = factory.newSchema(new StreamSource(new StringReader(xsd)))
def validator = schema.newValidator()
try
{
validator.validate(new StreamSource(new StringReader(xml)))
}
catch( Exception )
{
assert false
}
It gives me : "null"
could you please help to get this up and running, do let me know if I have gone wrong anywhere...
the following is the responsexml:
<error>
<code>ValidationError</code>
<message>Type mismatch for field with value [2011-85-99]. Required type [Date].</message>
</error>
and the xsd file content is :
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="error">
<xs:complexType>
<xs:all>
<xs:element name="code" type="xs:string"/>
<xs:element name="message" type="xs:string" minOccurs="0"/>
</xs:all>
</xs:complexType>
</xs:element>
</xs:schema>
I tried using this script assertion to validate an xml response, with the xsd file as:
import javax.xml.XMLConstants
import javax.xml.transform.stream.StreamSource
import javax.xml.validation.SchemaFactory
//Load the XSD from a file
def xsd = new File('/Users/pbhaskar/Desktop/error.xsd').text
//Get the XML from the response
def xmlHolder = new com.eviware.soapui.support.XmlHolder( messageExchange.responseContentAsXml )
def xml = xmlHolder['//error']
def factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI)
def schema = factory.newSchema(new StreamSource(new StringReader(xsd)))
def validator = schema.newValidator()
try
{
validator.validate(new StreamSource(new StringReader(xml)))
}
catch( Exception )
{
assert false
}
It gives me : "null"
could you please help to get this up and running, do let me know if I have gone wrong anywhere...
the following is the responsexml:
<error>
<code>ValidationError</code>
<message>Type mismatch for field with value [2011-85-99]. Required type [Date].</message>
</error>
and the xsd file content is :
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="error">
<xs:complexType>
<xs:all>
<xs:element name="code" type="xs:string"/>
<xs:element name="message" type="xs:string" minOccurs="0"/>
</xs:all>
</xs:complexType>
</xs:element>
</xs:schema>