首页 文章

针对 2 XSD 验证 XML

提问于
浏览
0

我正在尝试使用 SAX 解析和验证 SOAP 请求。需要两个 XSD,一个用于 SOAP 信封(http://schemas.xmlsoap.org/soap/envelope/)和我定义的一个。我找不到一种方法来正确验证针对这两个 XSD 的请求。

这是我用来解析请求并针对 soapenv.xsd 验证它的代码。它工作正常。如果我改为指定我的 XSD,则验证将失败并显示“无法找到元素声明'soapenv:Envelope'”。

SAXParserFactory factory = SAXParserFactory.newInstance();
factory.setNamespaceAware(true);
factory.setValidating(true);

SAXParser saxParser = factory.newSAXParser();       
saxParser.setProperty("http://java.sun.com/xml/jaxp/properties/schemaLanguage", "http://www.w3.org/2001/XMLSchema");
saxParser.setProperty("http://java.sun.com/xml/jaxp/properties/schemaSource", MyClass.class.getResourceAsStream("/xml/soapenv.xsd"));

InputSource is = new InputSource(MyClass.class.getResourceAsStream("/xml/request.xml"));
XMLReader reader = saxParser.getXMLReader();
reader.setContentHandler(new MyHandler());
reader.setErrorHandler(new MyErrorHandler());
reader.parse(is);

如何指定第二个 XSD?

有没有更好的方法来解析和验证 SOAP 请求?

编辑

正如提议的那样,我创建了 thirdpty.xsd 来导入我的两个 XSD。

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema targetNamespace="thirdparty:general"
xmlns="thirdparty:general"
xmlns:xs="http://www.w3.org/2001/XMLSchema">

    <xs:import schemaLocation="D:\ucfed\ValidateWSDL\src\xml\soapenv.xsd" 
       namespace="http://schemas.xmlsoap.org/soap/envelope/"/>

    <xs:import schemaLocation="D:\ucfed\ValidateWSDL\src\xml\Presence.xsd" 
       namespace="thirdparty:presence"/>    
</xs:schema>

我为验证指定了这个新的 XSD:

saxParser.setProperty("http://java.sun.com/xml/jaxp/properties/schemaSource", MyClass.class.getResourceAsStream("/xml/thidpty.xsd"));

但是,只有 SOAP 信封 XSD 用于验证。如果从我的其他 XSD 修改一个元素,则验证不会检测到它。

这是我想要验证的 xml

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:urn="thirdparty:presence">
    <soapenv:Header/>
    <soapenv:Body>
        <urn:getPresenceQuery>
            <urn:origAccount uri="test@origin.com"/>
            <urn:destAccount uri="test@destination.com"/>
        </urn:getPresenceQuery>
    </soapenv:Body>
</soapenv:Envelope>

其他想法?

1 回答

  • 0

    写一个驱动程序模式文件,导入另外两个;验证驱动程序。

相关问题