Forum Discussion

marlonumali's avatar
marlonumali
Occasional Contributor
8 years ago

Validate xml response with an xsd file.

I'm searching for a way to validate an XML response in soapui pro using an xsd file:

 

The XML response I got is below:

 

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
  <s:Body>
     <GetDataResponse xmlns="http://tempuri.org/">
     <GetDataResult>You entered: 5</GetDataResult>
     </GetDataResponse>
  </s:Body>
</s:Envelope>

 

I'm trying to validate it with an XSD file as:

 

<?xml version="1.0"?>
<schema
   xmlns="http://www.w3.org/2001/XMLSchema"
   targetNamespace="http://tempuri.org/"
   xmlns:s="http://tempuri.org/"
   elementFormDefault="qualified">

   <element name="GetDataResponse" type="GetDataResult"/>

   <element name="GetDataResult" type="string"/>

</schema>

 

The groovy script I use is:

 

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

String response = messageExchange.responseContent

new File( 'C:\\Projects\\WcfServiceAPITest\\WcfServiceAPITest.xsd' ).withReader
{ xsd ->
SchemaFactory.newInstance( XMLConstants.W3C_XML_SCHEMA_NS_URI )
.newSchema( new StreamSource( xsd ) )
.newValidator()
.validate( new StreamSource( new StringReader( response ) ) )

}

 

but I'm getting the following error:

 

src-resolve.4.2: Error resolving component 'GetDataResult'. It was detected that 'GetDataResult' is in namespace 'http://www.w3.org/2001/XMLSchema', but components
from this namespace are not referenceable from schema document 'null'. If this is the incorrect namespace, perhaps the prefix of 'GetDataResult' needs to be changed.
If this is the correct namespace, then an appropriate 'import' tag should be added to 'null'.

 

I would appreciate any help from the community. Thank you!

8 Replies

  • nmrao's avatar
    nmrao
    Champion Level 3

    It would be easy to validate using schema compliance assertion which does the same unless you have any custom requirement.