简体   繁体   English

使用针对XSD的验证器验证soap xml

[英]Validate soap xml using validator against xsd

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. 我想针对给定的xsd验证soap请求xml,该请求可能很大,因此我不能只提取soap主体的内容并进行验证,而是基于流进行验证。 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. 我已经为soap主体(请求部分)制作了xsd,它不包含任何有关soap标头的信息,因此验证失败。我所做的是将soap模式导入到我的xsd中,以便验证程序可以识别soap标头。 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? 我们如何在xsd中指定肥皂主体至少应包含特定元素? My modified xsd is pasted below. 我修改过的xsd粘贴在下面。 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>

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));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM