You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Postman中为SOAP请求的XML响应配置Schema验证?

How to Validate XML SOAP Responses with XSD (Like JSON Schema Validation)

Great question! Since you're used to validating JSON with tools like tiny validator, XML uses XML Schema Definition (XSD) for equivalent schema validation. Let's walk through how to apply this to your SOAP endpoint response, including code snippets and Postman integration.

Step 1: Get the XSD for Your SOAP Response

First, you need the XSD that defines the structure of your SOAP response. For the W3Schools TempConvert endpoint, you can extract the relevant schema from its WSDL (available at the endpoint's ?wsdl path). Inside the WSDL, look for the <xs:schema> section that defines response types (e.g., CelsiusToFahrenheitResponse).

Here's a simplified snippet of the relevant XSD for the Celsius to Fahrenheit response:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="https://www.w3schools.com/xml/">
  <xs:element name="CelsiusToFahrenheitResponse">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="CelsiusToFahrenheitResult" type="xs:string"/>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

Step 2: Code Snippets for XSD Validation

Below are examples in common languages, mirroring the workflow you'd use for JSON schema validation.

JavaScript (Node.js) with libxmljs

First, install the library:

npm install libxmljs

Then use this code to validate your XML response against the XSD:

const libxmljs = require('libxmljs');

// Your SOAP XML response (copied from Postman)
const xmlResponse = `<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Body>
    <CelsiusToFahrenheitResponse xmlns="https://www.w3schools.com/xml/">
      <CelsiusToFahrenheitResult>32</CelsiusToFahrenheitResult>
    </CelsiusToFahrenheitResponse>
  </soap:Body>
</soap:Envelope>`;

// Load your XSD schema
const xsd = libxmljs.parseXml(`<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="https://www.w3schools.com/xml/">
  <xs:element name="CelsiusToFahrenheitResponse">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="CelsiusToFahrenheitResult" type="xs:string"/>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>`);

// Parse the XML response
const xmlDoc = libxmljs.parseXml(xmlResponse);

// Run validation
const isValid = xmlDoc.validate(xsd);

if (isValid) {
  console.log('✅ XML is valid against the schema!');
} else {
  console.error('❌ Validation errors:', xmlDoc.validationErrors);
}

Java with javax.xml.validation

Java has built-in support for XSD validation via the SchemaFactory class—no external libraries needed:

import javax.xml.XMLConstants;
import javax.xml.transform.stream.StreamSource;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;
import javax.xml.validation.Validator;
import java.io.StringReader;

public class XmlSchemaValidator {
    public static void main(String[] args) {
        try {
            // SOAP XML response
            String xmlResponse = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" +
                    "<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">\n" +
                    "  <soap:Body>\n" +
                    "    <CelsiusToFahrenheitResponse xmlns=\"https://www.w3schools.com/xml/\">\n" +
                    "      <CelsiusToFahrenheitResult>32</CelsiusToFahrenheitResult>\n" +
                    "    </CelsiusToFahrenheitResponse>\n" +
                    "  </soap:Body>\n" +
                    "</soap:Envelope>";

            // XSD schema definition
            String xsdSchema = "<xs:schema xmlns:xs=\"http://www.w3.org/2001/XMLSchema\" targetNamespace=\"https://www.w3schools.com/xml/\">\n" +
                    "  <xs:element name=\"CelsiusToFahrenheitResponse\">\n" +
                    "    <xs:complexType>\n" +
                    "      <xs:sequence>\n" +
                    "        <xs:element name=\"CelsiusToFahrenheitResult\" type=\"xs:string\"/>\n" +
                    "      </xs:sequence>\n" +
                    "    </xs:complexType>\n" +
                    "  </xs:element>\n" +
                    "</xs:schema>";

            // Initialize validator
            SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
            Schema schema = factory.newSchema(new StreamSource(new StringReader(xsdSchema)));
            Validator validator = schema.newValidator();

            // Validate the response
            validator.validate(new StreamSource(new StringReader(xmlResponse)));
            System.out.println("✅ XML is valid against the schema!");

        } catch (Exception e) {
            System.err.println("❌ Validation failed: " + e.getMessage());
        }
    }
}

Step 3: Validate XML Responses in Postman

Postman's native sandbox doesn't include a full XSD validator, but you have two practical options:

Option 1: Structure Validation (Simplified)

If you just need to confirm the response has all required elements, convert the XML to JSON and check with Postman's built-in assertions:

// Convert XML response to JSON for easier checking
const jsonResponse = xml2Json(pm.response.text());

pm.test('SOAP response has all required elements', () => {
    pm.expect(jsonResponse['soap:Envelope']).to.exist;
    pm.expect(jsonResponse['soap:Envelope']['soap:Body']).to.exist;
    pm.expect(jsonResponse['soap:Envelope']['soap:Body']['CelsiusToFahrenheitResponse']).to.exist;
    pm.expect(jsonResponse['soap:Envelope']['soap:Body']['CelsiusToFahrenheitResponse']['CelsiusToFahrenheitResult']).to.exist;
    
    // Optional: Verify the result is a string
    pm.expect(jsonResponse['soap:Envelope']['soap:Body']['CelsiusToFahrenheitResponse']['CelsiusToFahrenheitResult']).to.be.a('string');
});

Option 2: Strict XSD Validation with Newman

For full XSD enforcement, use Newman (Postman's command-line runner) with a Node.js script like the JavaScript example above:

  1. Save your validation logic to a file (e.g., validate-xsd.js).
  2. Export your Postman collection as a JSON file.
  3. Run Newman with your collection and validation script:
newman run your-collection.json -g validate-xsd.js

内容的提问来源于stack exchange,提问作者Tenkasi Subramanian

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 08:29:31