Forum Discussion

deadlock_gr's avatar
deadlock_gr
New Contributor
16 years ago

Can I add assertion to validate with local XML Schema file?

Hi all,

I have created a Web Service (with Java, Axis). Depending on the content of the request, the response may have a different XSD. So, the WSDL only specifies that the response is of a generic XSD, and the responses comply to XSDs that import and extend the generic XSD.

Unfortunately, the Schema assertion fail because the XSD specified in the WSDL can only the generic one. Is there a way to manually specify which XSD I want the assertion to use? For instance, depending on the request I prepare, I know the specific XSD of the response. So, it would be perfect if I could say to SoapUI to assert the response by that XSD, which I can store either locally or at a url.

So, is there a way to make a schema assertion using a locally (or remotely) stored XMS schema?

Thanks,

Markos
  • Hello,

    You could use a Script assertion and do the validation in a Groovy script:


    import org.apache.xmlbeans.XmlBeans
    import org.apache.xmlbeans.XmlObject
    import org.apache.xmlbeans.SchemaTypeLoader

    def xsd = XmlObject.Factory.parse( new File('d:\\test.xsd').text )

    def sts = XmlBeans.compileXsd( ( XmlObject[] )[ xsd ], XmlBeans.getBuiltinTypeSystem(), null );
    def stl = XmlBeans.typeLoaderUnion( ( SchemaTypeLoader[] )[ sts, XmlBeans.getBuiltinTypeSystem() ] )

    def xml = stl.parse( messageExchange.responseContent, null, null )

    assert xml.validate()


    Obviously you'll have to replace the file path to reflect the location of your schema. You'll also probably need to change messageExchange.responseContent if the XML you are asserting is surrounded by other XML, which you could do using XPath. Good luck!

    Regards,
    Dain
    eviware.com
  • Thanks for the reply!

    Unfirtunately now I get a "network downloads disabled" error.

    Any ideas?

  • Hello,

    I've moved a little bit further, by modifying the call of the compileXsd().
    Provided the XmlOptions so as to download external XSDs.

    However it throws another error, while trying to compile the XSDs.
    Probably further options are needed for the validation since these XSDs are valid.
    Also it could be useful to get debug log messages from the XMLBeans but I don't know how.

  • Hello,

    Thanks for all the help. I have made some more progress in creating the XPATH expression I need and applying it. However, the result, which should comply to the XSD defined in the beginning of the Groovy script, does not validate correctly.

    I had the logger print the result of the XPATH, and validated it in XML SPY. The validation was correct.

    Can you please take a look at the current version of my script and see if something is wrong? I am also attaching below the complete response and the XPATH result (I had it printed out by the logger).



    SOAP RESPONSE:
    <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
      <soapenv:Body>
          <nsiwspref:GetCompactDataResponse xmlns:nsiwspref="http://localhost:8080/axis/services/NSIService">
            <ns1:CompactData xsi:schemaLocation="http://www.SDMX.org/resources/SDMXML/schemas/v2_0/message SDMXMessage.xsd  urn:sdmx:org.sdmx.infomodel.keyfamily.KeyFamily=ESTAT:STS:2.2:compact ESTAT_STS_Compact.xsd" xmlns="http://www.SDMX.org/resources/SDMXML/schemas/v2_0/message" xmlns:sts="urn:sdmx:org.sdmx.infomodel.keyfamily.KeyFamily=ESTAT:STS:2.2:compact" xmlns:ns1="http://www.SDMX.org/resources/SDMXML/schemas/v2_0/message">
                <ns1:Header>
                  <ns1:ID>JD014</ns1:ID>
                  <ns1:Test>true</ns1:Test>
                  <ns1:Name xml:lang="en">Trans46302</ns1:Name>
                  <ns1:Prepared>2001-03-11T09:30:47-05:00</ns1:Prepared>
                  <ns1:Sender id="BIS">
                      <ns1:Name xml:lang="en">Bank for International Settlements</ns1:Name>
                      <ns1:Contact>
                        <ns1:Name xml:lang="en">G.B. Smith</ns1:Name>
                        <ns1:Department xml:lang="en">Statistics</ns1:Department>
                        <ns1:Role xml:lang="en">SMith</ns1:Role>
                        <ns1:Telephone>210 2222222</ns1:Telephone>
                        <ns1:Fax>210 00010999</ns1:Fax>
                        <ns1:URI>www.sdmx.org</ns1:URI>
                        <ns1:Email>lala@sdmx.com</ns1:Email>
                      </ns1:Contact>
                  </ns1:Sender>
                  <ns1:Receiver id="ECB">
                      <ns1:Name xml:lang="en">European Central Bank</ns1:Name>
                      <ns1:Contact>
                        <ns1:Name xml:lang="en">John John</ns1:Name>
                        <ns1:Department xml:lang="en">Statistics</ns1:Department>
                        <ns1:Role xml:lang="en">Jack Over</ns1:Role>
                        <ns1:Telephone>210 1234567</ns1:Telephone>
                        <ns1:Fax>210 3810999</ns1:Fax>
                        <ns1:X400>lalala</ns1:X400>
                        <ns1:URI>www.sdmx.org</ns1:URI>
                        <ns1:Email>lala@sdmx.com</ns1:Email>
                      </ns1:Contact>
                  </ns1:Receiver>
                  <ns1:DataSetAgency>BIS</ns1:DataSetAgency>
                  <ns1:DataSetID>BIS_JD_237</ns1:DataSetID>
                  <ns1:DataSetAction>Append</ns1:DataSetAction>
                  <ns1:Extracted>2001-03-11T09:30:47-05:00</ns1:Extracted>
                  <ns1:ReportingBegin>2000-12-01T00:00:00</ns1:ReportingBegin>
                  <ns1:ReportingEnd>2006-01-01T00:00:00</ns1:ReportingEnd>
                </ns1:Header>
                <sts:DataSet>
                  <sts:Series ADJUSTMENT="W" AVAILABILITY="B" COLLECTION="A" FREQ="Q" REF_AREA="IT" STS_ACTIVITY="NS0020" STS_BASE_YEAR="2000" STS_INDICATOR="PROD" STS_INSTITUTION="1" TIME_FORMAT="P1M">
                      <sts:Obs OBS_CONF="F" OBS_STATUS="A" OBS_VALUE="1.51" TIME_PERIOD="2000"/>
                      <sts:Obs OBS_CONF="F" OBS_STATUS="A" OBS_VALUE="1.52" TIME_PERIOD="2001"/>
                      <sts:Obs OBS_CONF="F" OBS_STATUS="A" OBS_VALUE="1.53" TIME_PERIOD="2002"/>
                      <sts:Obs OBS_CONF="F" OBS_STATUS="A" OBS_VALUE="1.54" TIME_PERIOD="2003"/>
                      <sts:Obs OBS_CONF="F" OBS_STATUS="A" OBS_VALUE="1.61" TIME_PERIOD="2004"/>
                  </sts:Series>
                  <sts:Series ADJUSTMENT="W" AVAILABILITY="B" COLLECTION="A" FREQ="Q" REF_AREA="IT" STS_ACTIVITY="NS0030" STS_BASE_YEAR="2000" STS_INDICATOR="PROD" STS_INSTITUTION="1" TIME_FORMAT="P1M">
                      <sts:Obs OBS_CONF="F" OBS_STATUS="A" OBS_VALUE="1.51" TIME_PERIOD="2000"/>
                      <sts:Obs OBS_CONF="C" OBS_STATUS="A" OBS_VALUE="1.52" TIME_PERIOD="2001"/>
                      <sts:Obs OBS_CONF="C" OBS_STATUS="A" OBS_VALUE="1.53" TIME_PERIOD="2002"/>
                      <sts:Obs OBS_CONF="C" OBS_STATUS="A" OBS_VALUE="1.54" TIME_PERIOD="2003"/>
                      <sts:Obs OBS_CONF="F" OBS_STATUS="A" OBS_VALUE="1.61" TIME_PERIOD="2004"/>
                  </sts:Series>
                </sts:DataSet>
            </ns1:CompactData>
          </nsiwspref:GetCompactDataResponse>
      </soapenv:Body>
    </soapenv:Envelope>


    XPATH RESULT:
    <xml-fragment xmlns:nsiwspref="http://localhost:8080/axis/services/NSIService" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
      <ns1:CompactData xsi:schemaLocation="http://www.SDMX.org/resources/SDMXML/schemas/v2_0/message SDMXMessage.xsd  urn:sdmx:org.sdmx.infomodel.keyfamily.KeyFamily=ESTAT:STS:2.2:compact ESTAT_STS_Compact.xsd" xmlns="http://www.SDMX.org/resources/SDMXML/schemas/v2_0/message" xmlns:sts="urn:sdmx:org.sdmx.infomodel.keyfamily.KeyFamily=ESTAT:STS:2.2:compact" xmlns:ns1="http://www.SDMX.org/resources/SDMXML/schemas/v2_0/message">
        <ns1:Header>
          <ns1:ID>JD014</ns1:ID>
          <ns1:Test>true</ns1:Test>
          <ns1:Name xml:lang="en">Trans46302</ns1:Name>
          <ns1:Prepared>2001-03-11T09:30:47-05:00</ns1:Prepared>
          <ns1:Sender id="BIS">
            <ns1:Name xml:lang="en">Bank for International Settlements</ns1:Name>
            <ns1:Contact>
              <ns1:Name xml:lang="en">G.B. Smith</ns1:Name>
              <ns1:Department xml:lang="en">Statistics</ns1:Department>
              <ns1:Role xml:lang="en">SMith</ns1:Role>
              <ns1:Telephone>210 2222222</ns1:Telephone>
              <ns1:Fax>210 00010999</ns1:Fax>
              <ns1:URI>www.sdmx.org</ns1:URI>
              <ns1:Email>lala@sdmx.com</ns1:Email>
            </ns1:Contact>
          </ns1:Sender>
          <ns1:Receiver id="ECB">
            <ns1:Name xml:lang="en">European Central Bank</ns1:Name>
            <ns1:Contact>
              <ns1:Name xml:lang="en">John John</ns1:Name>
              <ns1:Department xml:lang="en">Statistics</ns1:Department>
              <ns1:Role xml:lang="en">Jack Over</ns1:Role>
              <ns1:Telephone>210 1234567</ns1:Telephone>
              <ns1:Fax>210 3810999</ns1:Fax>
              <ns1:X400>lalala</ns1:X400>
              <ns1:URI>www.sdmx.org</ns1:URI>
              <ns1:Email>lala@sdmx.com</ns1:Email>
            </ns1:Contact>
          </ns1:Receiver>
          <ns1:DataSetAgency>BIS</ns1:DataSetAgency>
          <ns1:DataSetID>BIS_JD_237</ns1:DataSetID>
          <ns1:DataSetAction>Append</ns1:DataSetAction>
          <ns1:Extracted>2001-03-11T09:30:47-05:00</ns1:Extracted>
          <ns1:ReportingBegin>2000-12-01T00:00:00</ns1:ReportingBegin>
          <ns1:ReportingEnd>2006-01-01T00:00:00</ns1:ReportingEnd>
        </ns1:Header>
        <sts:DataSet>
          <sts:Series ADJUSTMENT="W" AVAILABILITY="B" COLLECTION="A" FREQ="Q" REF_AREA="IT" STS_ACTIVITY="NS0020" STS_BASE_YEAR="2000" STS_INDICATOR="PROD" STS_INSTITUTION="1" TIME_FORMAT="P1M">
            <sts:Obs OBS_CONF="F" OBS_STATUS="A" OBS_VALUE="1.51" TIME_PERIOD="2000"/>
            <sts:Obs OBS_CONF="F" OBS_STATUS="A" OBS_VALUE="1.52" TIME_PERIOD="2001"/>
            <sts:Obs OBS_CONF="F" OBS_STATUS="A" OBS_VALUE="1.53" TIME_PERIOD="2002"/>
            <sts:Obs OBS_CONF="F" OBS_STATUS="A" OBS_VALUE="1.54" TIME_PERIOD="2003"/>
            <sts:Obs OBS_CONF="F" OBS_STATUS="A" OBS_VALUE="1.61" TIME_PERIOD="2004"/>
          </sts:Series>
          <sts:Series ADJUSTMENT="W" AVAILABILITY="B" COLLECTION="A" FREQ="Q" REF_AREA="IT" STS_ACTIVITY="NS0030" STS_BASE_YEAR="2000" STS_INDICATOR="PROD" STS_INSTITUTION="1" TIME_FORMAT="P1M">
            <sts:Obs OBS_CONF="F" OBS_STATUS="A" OBS_VALUE="1.51" TIME_PERIOD="2000"/>
            <sts:Obs OBS_CONF="C" OBS_STATUS="A" OBS_VALUE="1.52" TIME_PERIOD="2001"/>
            <sts:Obs OBS_CONF="C" OBS_STATUS="A" OBS_VALUE="1.53" TIME_PERIOD="2002"/>
            <sts:Obs OBS_CONF="C" OBS_STATUS="A" OBS_VALUE="1.54" TIME_PERIOD="2003"/>
            <sts:Obs OBS_CONF="F" OBS_STATUS="A" OBS_VALUE="1.61" TIME_PERIOD="2004"/>
          </sts:Series>
        </sts:DataSet>
      </ns1:CompactData>
    </xml-fragment>
  • Try using this:


    def dataXpath = """
    declare namespace soapenv="http://schemas.xmlsoap.org/soap/envelope/";
    declare namespace nsiwspref="http://localhost:8080/axis/services/NSIService";
    declare namespace ns1="http://www.SDMX.org/resources/SDMXML/schemas/v2_0/message";
    //soapenv:Envelope/soapenv:Body/nsiwspref:GetCompactDataResponse/ns1:CompactData
    """

    def elementText = xml.selectPath( dataXpath )[0].xmlText( new XmlOptions().setSaveOuter() )

    def xml2 = stl.parse( elementText, null, null )


    and replace any occurances of xml2[0] with xml2 which comes after it.

    Regards,
    Dain
    eviware.com