Reputation: 1947
I want to validate a soap request xml against a given xsd, The request can be huge so I can't just extract the content of soap body and do the validation, instead I do streaming based validation. I have already made xsd for the soap body (request part) and it does not contain any information about soap headers thus the validation gets failed, What I did was importing the soap schema to my xsd so that validator can identify the soap headers. It worked, but the validation gets successfull even if the soap body doesn't contain anything. How can we specify in the xsd that soap body should contain at least of particular element? My modified xsd is pasted below. thanks.
<?xml version="1.0" encoding="utf-8"?>
<xsd:schema attributeFormDefault="qualified" targetNamespace="urn:sample"
xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:tns="sample">
<xsd:import namespace="http://schemas.xmlsoap.org/soap/envelope/"
schemaLocation="http://schemas.xmlsoap.org/soap/envelope/"/>
<xsd:complexType name="type1">
<xsd:sequence>
<xsd:element name="item" type="tns:type2" minOccurs="0" maxOccurs="unbounded"/>
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="type3">
<xsd:sequence>
<xsd:element name="item" type="tns:type4" minOccurs="0" maxOccurs="unbounded"/>
</xsd:sequence>
</xsd:complexType>
<xsd:element name="element1">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="RETURN" type="tns:type3" minOccurs="0"/>
<xsd:element name="HEADER" type="tns:type1"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:element name="element2">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="RETURN" type="tns:type3" minOccurs="0"/>
<xsd:element name="ERRORS" type="xsd:int"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>
Upvotes: 0
Views: 2552
Reputation: 64
Try something similar to:
SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
FileInputStream fileInputStream = new FileInputStream(new File("Simple.xsd"));
Schema schema = factory.newSchema(new StreamSource(fileInputStream));
Validator val = schema.newValidator();
FileInputStream fileInputStream2 = new FileInputStream(new File("Input.xml"));
val.validate(new StreamSource(fileInputStream2));
Upvotes: 1