Forum Discussion

StephenC's avatar
StephenC
Occasional Contributor
14 years ago

Validation of certain sections of response against XSD

Is it possible to apply XSD validation to certain sections of a XML response?
Schema compliance assertion validates whole response against XSD from WSDL but I want to validate part of response against local xsd.

5 Replies

  • StephenC's avatar
    StephenC
    Occasional Contributor
    Is this script correct

    import com.eviware.soapui.support.XmlHolder
    import javax.xml.XMLConstants
    import javax.xml.transform.stream.StreamSource
    import javax.xml.validation.SchemaFactory

    def holder = new XmlHolder( messageExchange.responseContentAsXml )
    holder.namespaces["q1"] = '...'
    holder.namespaces["ns1"]= '...'

    def node = holder["...xpath..."]

    def factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI)
    def schema = factory.newSchema(new StreamSource(new FileReader("c:/Temp/test.xsd")))

    def validator = schema.newValidator()

    validator.validate(new StreamSource(new StringReader(node))


    I got "Premature end o file" error
  • StephenC's avatar
    StephenC
    Occasional Contributor
    RJanecek wrote:
    and in which row you have this exception ?


    When I run script assertion a popup shows up with this error.
    The last line is causing it because when I comment it there is no error.
  • StephenC's avatar
    StephenC
    Occasional Contributor
    I had to modify code to following:

    - get XML using
    holder.getDomNode(XPath)

    - cast XML to string in StringReader method:
    validator.validate(new StreamSource(new StringReader(node as String)))



    import com.eviware.soapui.support.XmlHolder
    import javax.xml.XMLConstants
    import javax.xml.transform.stream.StreamSource
    import javax.xml.validation.SchemaFactory

    def holder = new XmlHolder( messageExchange.responseContentAsXml )
    holder.namespaces["q1"] = '...'
    holder.namespaces["ns1"]= '...'
    def XPath = "..."
    def node = holder.getDomNode(XPath)

    def factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI)
    def schema = factory.newSchema(new StreamSource(new FileReader(pathToXsd)))

    def validator = schema.newValidator()

    validator.validate(new StreamSource(new StringReader(node as String)))