Ask a Question

Validate xml response with an xsd file.

marlonumali
Occasional Contributor

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 8
nmrao
Champion Level 3

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



Regards,
Rao.
marlonumali
Occasional Contributor

Thank you for your advice nmrao, but the schema compliance doesn't seem to validate element data types, like if I have a Date element and the actual response value is an integer, the test step will still evaluate to true. Also, we like to control the restriction on some elements, like limiting the length of a string value which may change from time to time, this is why I thought it will be best to validate it against an xsd...

I believe that you had same question in the below thread which was accepted answered
https://community.smartbear.com/t5/SoapUI-Open-Source/How-to-verify-response-content-if-it-has-valid...

What made you to open another question?


Regards,
Rao.
marlonumali
Occasional Contributor

Thanks nmrao, we like to use xsd validation as we have lots of data fields declaration and restriction that we want to validate, and I thought it would be very tedious to test each field by plain groovy scripts.

 

Regards, 

Marlon

marlonumali
Occasional Contributor

I rewrite the schema file and break it into two separate files to allow the use of two different namespaces ("http://schemas.xmlsoap.org/soap/envelope/" and "http://tempuri.org/") in the xml file. I validate the original xml successfully using Eclipse with the two schema files below:

 

First Schema NewXMLSchema.xsd:

 

<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:tns="http://tempuri.org/"
elementFormDefault="qualified">

<import schemaLocation="NewXMLSchema1.xsd" namespace="http://tempuri.org/" >

</import>

<element name="Envelope" type="s:Body" />

<complexType name="Body">
<sequence>
<element name="Body" type="tns:GetDataResponse" />
</sequence>
</complexType>
</schema>

 

The imported Schema NewXMLSchema1.xsd:

 

<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://tempuri.org/"
xmlns:tns="http://tempuri.org/"
elementFormDefault="qualified">

<complexType name="GetDataResponse" >
<sequence>
<element name="GetDataResponse" type="tns:GetDataResult"/>
</sequence>
</complexType>
<complexType name="GetDataResult" >
<sequence>
<element name="GetDataResult" type="string" />
</sequence>
</complexType>
</schema>

 

 

But when I tried to validate the original xml file in the soapui, i receive the following error message:

 

src-resolve: Cannot resolve the name 'tns:GetDataResponse' to a(n) 'type of definition' component.

alexisrl733
Occasional Contributor

Hello nmrao, 

 

I have an issue when using an xsd file that contains this:

<xsd:include schemaLocation="edist_commontypes.xsd"/>

<xsd:include schemaLocation="aidm_commontypes.xsd"/>

 

I mean, it "includes" another xsd (reference). How to deal with this? I'm trying to validate an xml file using this xsd file (that also includes reference to another xsd).

Hi @alexisrl733 ,

 

Did you happen to find a solution for this which you have asked ?

It would be of real help.

Thanks,

Sonal

alexisrl733
Occasional Contributor

Hello @snand,

 

No, I haven't. Sorry.

cancel
Showing results for 
Search instead for 
Did you mean: