【问题标题】:Validating XML against multiple schemas extracted from WSDL针对从 WSDL 提取的多个模式验证 XML
【发布时间】:2014-02-09 21:27:53
【问题描述】:

我正在尝试使用 SAAJ 手动处理 Web 服务请求,但仍会根据 Web 服务的 WSDL 架构验证接收到的请求。这个特定的 WSDL 包含多个元素,我使用 wsdl4j 提取这些元素,当我尝试创建一个新的验证器时,我收到一条错误消息,说明验证器无法解析一个架构中的引用另一个元素中定义的元素:

org.xml.sax.SAXParseException; src-resolve: Cannot resolve the name 'ns0:CustomerNumber' to a(n) 'element declaration' component.

我已经将实际的 WSDL 和代码简化为一些小的东西,以创建可重现的东西。 “simple.wsdl”也在 Soap-UI 中正确加载。这是在带有 jdk1.7.0_51 的 Windows 7 上。

请注意,我注意到此问题的原始 WSDL 是由 TIBCO BusinessWorks 生成的,所以我不认为这是一个无效 WSDL 的问题。

我在 SO 上看到了与此类似的其他问题,但提供的解决方案似乎并不完全符合我的情况,所以我想我会再问一次。

有没有人知道发生了什么以及我如何才能让它发挥作用?

测试代码:

import java.util.ArrayList;

import javax.wsdl.Definition;
import javax.wsdl.extensions.schema.Schema;
import javax.wsdl.factory.WSDLFactory;
import javax.wsdl.xml.WSDLReader;
import javax.xml.XMLConstants;
import javax.xml.transform.dom.DOMSource;
import javax.xml.validation.SchemaFactory;
import javax.xml.validation.Validator;

import org.junit.Test;
import org.w3c.dom.Element;

public class TestIt {
    @Test
    public void testWSDLSchema() throws Exception {
        WSDLFactory wsdlFactory    = WSDLFactory.newInstance();
        WSDLReader reader         = wsdlFactory.newWSDLReader();
        Definition wsdlDefinition = reader.readWSDL("test/resources/simple.wsdl");

        ArrayList<Element> wsdlSchemas = new ArrayList<Element>();

        for (Object o : wsdlDefinition.getTypes().getExtensibilityElements()) {
            if (o instanceof Schema) {
                wsdlSchemas.add(((Schema) o).getElement());
            }
        }

        SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
        ArrayList<DOMSource> asrcs   = new ArrayList<DOMSource>();

        for (Element e : wsdlSchemas) {
            asrcs.add(new DOMSource(e));
        }
        DOMSource sources[] = asrcs.toArray(new DOMSource[0]);
        javax.xml.validation.Schema schema  = factory.newSchema(sources);

        Validator schemaValidator = schema.newValidator();
    }
}

完整的堆栈跟踪:

Retrieving document at 'test/resources/simple.wsdl'.

org.xml.sax.SAXParseException; src-resolve: Cannot resolve the name 'ns0:CustomerNumber' to a(n) 'element declaration' component.
    at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(ErrorHandlerWrapper.java:198)
    at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.error(ErrorHandlerWrapper.java:134)
    at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:437)
    at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:347)
    at com.sun.org.apache.xerces.internal.impl.xs.traversers.XSDHandler.reportSchemaErr(XSDHandler.java:4166)
    at com.sun.org.apache.xerces.internal.impl.xs.traversers.XSDHandler.reportSchemaError(XSDHandler.java:4145)
    at com.sun.org.apache.xerces.internal.impl.xs.traversers.XSDHandler.getGlobalDecl(XSDHandler.java:1678)
    at com.sun.org.apache.xerces.internal.impl.xs.traversers.XSDElementTraverser.traverseLocal(XSDElementTraverser.java:170)
    at com.sun.org.apache.xerces.internal.impl.xs.traversers.XSDHandler.traverseLocalElements(XSDHandler.java:3618)
    at com.sun.org.apache.xerces.internal.impl.xs.traversers.XSDHandler.parseSchema(XSDHandler.java:633)
    at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaLoader.loadSchema(XMLSchemaLoader.java:616)
    at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaLoader.loadGrammar(XMLSchemaLoader.java:574)
    at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaLoader.loadGrammar(XMLSchemaLoader.java:540)
    at com.sun.org.apache.xerces.internal.jaxp.validation.XMLSchemaFactory.newSchema(XMLSchemaFactory.java:252)
    at TestIt.testWSDLSchema(TestIt.java:37)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:160)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:74)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:202)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:65)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)

WSDL:

<?xml version="1.0" encoding="UTF-8"?>
<wsdl:definitions xmlns:ns10="http://www.abc.com/ServiceA"
                  xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
                  xmlns:ns13="http://www.abc.com/Common"
                  xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
                  xmlns:ns2="http://www.abc.com/DetailsResponse"
                  xmlns:ns3="http://www.abc.com/ErrorSchema"
                  xmlns:ns1="http://www.abc.com/DetailsRequest"
                  xmlns:tns="http://www.abc.com/Service"
                  name="Untitled"
                  targetNamespace="http://www.abc.com/Service">
    <wsdl:types>
        <xs:schema
                xmlns="http://www.abc.com/DetailsRequest"
                xmlns:ns0="http://www.abc.com/Common"
                xmlns:xs="http://www.w3.org/2001/XMLSchema"
                targetNamespace="http://www.abc.com/DetailsRequest"
                elementFormDefault="qualified"
                attributeFormDefault="unqualified">
            <xs:import namespace="http://www.w3.org/XML/1998/namespace"/>
            <xs:import namespace="http://www.abc.com/Common"/>
            <xs:element name="DetailsRequest">
                <xs:complexType>
                    <xs:sequence>
                        <xs:element ref="ns1:RequestBody"/>
                    </xs:sequence>
                </xs:complexType>
            </xs:element>
            <xs:element name="RequestBody">
                <xs:complexType>
                    <xs:sequence>
                        <xs:element ref="ns0:CustomerNumber"/>
                    </xs:sequence>
                </xs:complexType>
            </xs:element>
        </xs:schema>
        <xs:schema
                xmlns="http://www.abc.com/DetailsResponse"
                xmlns:ns0="http://www.abc.com/Common"
                xmlns:xs="http://www.w3.org/2001/XMLSchema"
                targetNamespace="http://www.abc.com/DetailsResponse"
                elementFormDefault="qualified"
                attributeFormDefault="unqualified">
            <xs:import namespace="http://www.abc.com/Common"/>
            <xs:element name="DetailsResponse">
                <xs:complexType>
                    <xs:sequence>
                        <xs:element ref="ns0:AccountNumber"/>
                    </xs:sequence>
                </xs:complexType>
            </xs:element>
        </xs:schema>
        <xs:schema
                xmlns="http://www.abc.com/Common"
                xmlns:xs="http://www.w3.org/2001/XMLSchema"
                targetNamespace="http://www.abc.com/Common"
                elementFormDefault="qualified"
                attributeFormDefault="unqualified">
            <xs:element name="CustomerNumber">
                <xs:simpleType>
                    <xs:restriction base="xs:string">
                        <xs:maxLength value="20"/>
                    </xs:restriction>
                </xs:simpleType>
            </xs:element>
            <xs:element name="AccountNumber">
                <xs:simpleType>
                    <xs:restriction base="xs:string">
                        <xs:maxLength value="20"/>
                    </xs:restriction>
                </xs:simpleType>
            </xs:element>
        </xs:schema>
        <xs:schema
                xmlns="http://www.abc.com/ErrorSchema"
                xmlns:xs="http://www.w3.org/2001/XMLSchema"
                targetNamespace="http://www.abc.com/ErrorSchema"
                elementFormDefault="qualified"
                attributeFormDefault="unqualified">
            <xs:element name="ErrorSchema">
                <xs:complexType>
                    <xs:sequence>
                        <xs:element name="ErrorResponseBody" type="xs:string"/>
                    </xs:sequence>
                </xs:complexType>
            </xs:element>
        </xs:schema>
    </wsdl:types>
    <wsdl:service name="ServiceA">
        <wsdl:port name="ServiceAEndpoint" binding="tns:ServiceABinding">
            <soap:address location="http://localhost:7232/ServiceA/ServiceAEndpoint"/>
        </wsdl:port>
    </wsdl:service>
    <wsdl:portType name="ServiceA">
        <wsdl:operation name="GetDetails">
            <wsdl:input message="tns:DetailsRequest"/>
            <wsdl:output message="tns:DetailsResponse"/>
            <wsdl:fault name="fault1" message="tns:DetailsErrorResponse"/>
        </wsdl:operation>
    </wsdl:portType>
    <wsdl:binding name="ServiceABinding" type="tns:ServiceA">
        <soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
        <wsdl:operation name="GetDetails">
            <soap:operation style="document" soapAction="/ServiceA/ServiceAEndpoint/GetDetails"/>
            <wsdl:input>
                <soap:body use="literal" parts="DetailsRequest"/>
            </wsdl:input>
            <wsdl:output>
                <soap:body use="literal" parts="DetailsResponse"/>
            </wsdl:output>
            <wsdl:fault name="fault1">
                <soap:fault use="literal" name="fault1"/>
            </wsdl:fault>
        </wsdl:operation>
    </wsdl:binding>
    <wsdl:message name="DetailsRequest">
        <wsdl:part name="DetailsRequest" element="ns1:DetailsRequest"/>
    </wsdl:message>
    <wsdl:message name="DetailsResponse">
        <wsdl:part name="DetailsResponse" element="ns2:DetailsResponse"/>
    </wsdl:message>
    <wsdl:message name="DetailsErrorResponse">
        <wsdl:part name="DetailsErrorResponse" element="ns3:ErrorSchema"/>
    </wsdl:message>
</wsdl:definitions>

【问题讨论】:

  • 您是否尝试以与导入相反的顺序提供架构?
  • 我做到了,在这个简单的例子中它可以工作。我只是使用 wsdlSchemas.add(0, ...) 来反转代码中的顺序,而不修改实际的 WSDL。但是,对于真正的 WSDL,这无济于事。我猜在这种情况下,要弄清楚依赖关系有点棘手?
  • 由于在简单的测试用例中我可以通过更改 Source[] 条目的顺序来使其工作,我不确定这里是否需要 LSResourceResolver?
  • 您似乎暗示重新排序在复杂情况下没有帮助。如果没有 LSResourceResolver,你打算如何解决这个问题?
  • 嗨,我想我对 LSResourceResolver 的用途有点困惑。我的理解是,您使用它外部“模式部件/命名空间”,这些在已提供给 factory.newSchema(Source[]) 的信息中不可用。就我而言,我已经 提供了模式的所有“部分”,只是 Xerces(?) 似乎没有意识到这一点。好的,所以我将尝试使用 LSResourceResolver。问题:我的架构部分没有纯文本形式。我只在 DOM 模型中有它。如何将其融入 LSInput 实现?

标签: java xsd wsdl schema wsdl4j


【解决方案1】:

我必须同时使用 LSResourceResolver 并将名称空间声明从 wsdl 文档复制到架构元素才能完成这项工作。代码如下:

public static Schema makeSchema(String pathToWsdl)
    throws ParserConfigurationException, IOException, SAXException, InstantiationException, IllegalAccessException, ClassNotFoundException {
    // read wsdl document
    File wsdlFile = new File(pathToWsdl);
    DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
    dbFactory.setNamespaceAware(true);
    DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
    Document wsdlDoc = dBuilder.parse(wsdlFile);

    // read namespace declarations from wsdl document, in case they are referred from a schema
    NamedNodeMap attributes = wsdlDoc.getDocumentElement().getAttributes();
    Map<String, String> namespacesFromWsdlDocument = new HashMap<>();
    for (int i = 0; i < attributes.getLength(); i++) {
        Node n = attributes.item(i);
        if (n.getNamespaceURI() != null && n.getNamespaceURI().equals("http://www.w3.org/2000/xmlns/")) {
            namespacesFromWsdlDocument
                .put(n.getLocalName(), n.getNodeValue());
        }
    }

    // read the schema nodes from the wsdl
    NodeList schemas = wsdlDoc.getElementsByTagNameNS("http://www.w3.org/2001/XMLSchema", "schema");
    Map<String, DOMSource> sources = new HashMap<>();
    for (int i = 0; i < schemas.getLength(); i++) {
        // create a document for each schema and copy the source schema
        Document schema = dBuilder.newDocument();
        Element schemaElement = (Element)schema.importNode(schemas.item(i), true);

        // add all non-existing namespace declarations from the wsdl node
        String targetNs = schemaElement.getAttribute("targetNamespace");
        for (Map.Entry<String, String> ns : namespacesFromWsdlDocument.entrySet()) {
            String name = ns.getKey();
            String value = ns.getValue();
            if (schemaElement.getAttributeNodeNS("http://www.w3.org/2000/xmlns/", name) == null) {
                schemaElement.setAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns:" + name, value);
            }
        }

        // map schemas by their target namespace
        schema.appendChild(schemaElement);
        DOMSource domSource = new DOMSource(schema);
        sources.put(targetNs, domSource);
    }

    SchemaFactory factory =
        SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);

    // Create a ResourceResolver that can find the correct schema from the map
    DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance();

    final DOMImplementationLS domImplementationLS = (DOMImplementationLS) registry.getDOMImplementation("LS");
    factory.setResourceResolver(new LSResourceResolver() {
        @Override public LSInput resolveResource(String type, String namespaceURI, String publicId, String systemId, String baseURI) {
            Source xmlSource = sources.get(namespaceURI);
            if (xmlSource != null) {
                LSInput input = domImplementationLS.createLSInput();
                ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
                Result outputTarget = new StreamResult(outputStream);
                try {
                    TransformerFactory.newInstance().newTransformer().transform(xmlSource, outputTarget);
                } catch (TransformerException e) {
                    e.printStackTrace();
                }
                InputStream is = new ByteArrayInputStream(outputStream.toByteArray());
                input.setByteStream(is);
                input.setSystemId(systemId);
                return input;
            } else {
                return null;
            }
        }
    });

    // create the schema object from the sources
    return factory.newSchema(sources.values().toArray(new DOMSource[]{}));
}

这里也回答了同样的问题: Java, Validate XML against an embedded schema in WSDL

【讨论】:

    【解决方案2】:

    我认为SchemaFactory 对它是如何导致导入的“愚蠢”。我相信如果您以相反的顺序(从最低级别到最高级别)提供模式,那应该可以。在您有更复杂的依赖关系(或同一命名空间的多个模式)的情况下,您可能需要实现自定义 LSResourceResolver 并将其设置在 SchemaFactory 上。

    【讨论】:

    • 你确定吗? SchemaFactory.newSchema(Source[]) 的 javadocs 说:“生成的架构包含来自指定源的组件。如果将所有这些源导入到单个架构文档中,使用适当的 schemaLocation 和命名空间值,将获得相同的结果如果导入元素以与源相同的顺序给出,则具有不同的 targetNamespace 并且没有自己的组件”。我可能读错了,但对我来说,这听起来像 LSResourceResolver 不应该是必要的。
    • @MaartenBoekhold - 我不确定您的合成模式如何帮助任何事情。你必须在导入之前提供导入的模式,所以合成模式最终还是最后?
    猜你喜欢
    • 2016-09-29
    • 2011-02-07
    • 2014-07-13
    • 1970-01-01
    • 2010-11-08
    • 2014-06-10
    • 2012-09-20
    • 2021-04-11
    • 1970-01-01
    相关资源
    最近更新 更多