我正在尝试根据架构验证传入的XML请求。基本上有三种不同的模式CDM_GeneralLedger_v1.0.xsd,GLBookingTypes-v1.0.xsd,CDM_GeneralLedger_v1.0.xsd(CDM_GeneralLedger_v1.0.xsd导入CDM_ISOCurrencyCodes_v1.0.xsd和GLBookingTypes-v1.0.xsd导入CDM_GeneralLedger_v1.0。 xsd)。现在在WSO2 ESB中,我使用的是验证调解器,但是它给了我一个例外情况。
TID: [0] [ESB] [2014-11-26 09:58:36,989] INFO {org.apache.axis2.transport.jms.ServiceTaskManager} - Task manager for service : GLBookingService shutdown {org.apache.axis2.transport.jms.ServiceTaskManager}
TID: [0] [ESB] [2014-11-26 09:58:36,990] INFO {org.apache.axis2.transport.jms.ServiceTaskManager} - Task manager for service : GLBookingService [re-]initialized {org.apache.axis2.transport.jms.ServiceTaskManager}
TID: [0] [ESB] [2014-11-26 09:58:37,991] INFO {org.apache.axis2.transport.jms.ServiceTaskManager} - Reconnection attempt: 1 for service: GLBookingService was successful! {org.apache.axis2.transport.jms.ServiceTaskManager}
TID: [0] [ESB] [2014-11-26 09:59:13,409] INFO {org.apache.synapse.mediators.builtin.LogMediator} - To: ...CDMRequestReceived...WSAction: urn:mediate...CDMRequestReceived...SOAPAction: urn:mediate...CDMRequestReceived...MessageID: ID:WIN-RPVU8RVH42Q-49271-1416974777862-3:1:1:1:16...CDMRequestReceived...Direction: request {org.apache.synapse.mediators.builtin.LogMediator}
TID: [0] [ESB] [2014-11-26 09:59:13,422] WARN {org.apache.synapse.util.jaxp.SchemaResourceResolver} - Unable to resolve schema resource : "CDM_GeneralLedger_v1.0.xsd". External schema resources not defined in Validate mediator configuration {org.apache.synapse.util.jaxp.SchemaResourceResolver}
TID: [0] [ESB] [2014-11-26 09:59:13,441] ERROR {org.apache.synapse.mediators.builtin.ValidateMediator} - Error creating a new schema objects for schemas : [Value {name ='null', keyValue ='conf:/XSD/GLBookingTypes-v1.0.xsd'}, Value {name ='null', keyValue ='conf:/CDM_GeneralLedger_custom_v1.0.xsd'}] {org.apache.synapse.mediators.builtin.ValidateMediator}
org.xml.sax.SAXParseException; lineNumber: 11; columnNumber: 59; src-resolve: Cannot resolve the name 'cdm:GLBookingType' to a(n) 'type definition' component.
at org.apache.xerces.util.ErrorHandlerWrapper.createSAXParseException(Unknown Source)
at org.apache.xerces.util.ErrorHandlerWrapper.error(Unknown Source)
at org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown Source)
at org.apache.xerces.impl.xs.traversers.XSDHandler.reportSchemaError(Unknown Source)
at org.apache.xerces.impl.xs.traversers.XSDHandler.getGlobalDecl(Unknown Source)
at org.apache.xerces.impl.xs.traversers.XSDElementTraverser.traverseNamedElement(Unknown Source)
at org.apache.xerces.impl.xs.traversers.XSDElementTraverser.traverseLocal(Unknown Source)
at org.apache.xerces.impl.xs.traversers.XSDHandler.traverseLocalElements(Unknown Source)
at org.apache.xerces.impl.xs.traversers.XSDHandler.parseSchema(Unknown Source)
at org.apache.xerces.impl.xs.XMLSchemaLoader.loadSchema(Unknown Source)
at org.apache.xerces.impl.xs.XMLSchemaLoader.loadGrammar(Unknown Source)
at org.apache.xerces.impl.xs.XMLSchemaLoader.loadGrammar(Unknown Source)
at org.apache.xerces.jaxp.validation.XMLSchemaFactory.newSchema(Unknown Source)
at org.apache.synapse.mediators.builtin.ValidateMediator.mediate(ValidateMediator.java:158)
at org.apache.synapse.mediators.AbstractListMediator.mediate(AbstractListMediator.java:77)
at org.apache.synapse.mediators.AbstractListMediator.mediate(AbstractListMediator.java:47)
at org.apache.synapse.mediators.base.SequenceMediator.mediate(SequenceMediator.java:131)
at org.apache.synapse.mediators.base.SequenceMediator.mediate(SequenceMediator.java:196)
at org.apache.synapse.mediators.AbstractListMediator.mediate(AbstractListMediator.java:77)
at org.apache.synapse.mediators.AbstractListMediator.mediate(AbstractListMediator.java:47)
at org.apache.synapse.mediators.base.SequenceMediator.mediate(SequenceMediator.java:131)
at org.apache.synapse.core.axis2.ProxyServiceMessageReceiver.receive(ProxyServiceMessageReceiver.java:166)
at org.apache.axis2.engine.AxisEngine.receive(AxisEngine.java:180)
at org.apache.axis2.transport.base.AbstractTransportListener.handleIncomingMessage(AbstractTransportListener.java:328)
at org.apache.axis2.transport.jms.JMSMessageReceiver.processThoughEngine(JMSMessageReceiver.java:200)
at org.apache.axis2.transport.jms.JMSMessageReceiver.onMessage(JMSMessageReceiver.java:122)
at org.apache.axis2.transport.jms.ServiceTaskManager$MessageListenerTask.handleMessage(ServiceTaskManager.java:556)
at org.apache.axis2.transport.jms.ServiceTaskManager$MessageListenerTask.run(ServiceTaskManager.java:449)
at org.apache.axis2.transport.base.threads.NativeWorkerPool$1.run(NativeWorkerPool.java:172)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:744)
由于机密性,我无法发布我的XSD。但是同样的xsds在TIBCO,Talend等其他工具中运行良好。
Validate介体源代码是:
<sequence xmlns="http://ws.apache.org/ns/synapse" name="validateXml">
<property xmlns:ns="http://org.apache.synapse/xsd" name="CMMHeader" expression="$body/Message/CMMHeader" scope="default" type="OM"></property>
<validate xmlns:ns="http://org.apache.synapse/xsd" xmlns:tns="http://com.glbooking/v1/data" source="//tns:GLBookingMessage">
<schema key="conf:/XSD/CDM_GeneralLedger_v1.0.xsd"></schema>
<schema key="conf:/XSD/GLBookingTypes-v1.0.xsd"></schema>
<schema key="conf:/XSD/CDM_ISOCurrencyCodes_v1.0.xsd"></schema>
<on-fail>
<makefault version="pox">
<reason value="XML Request is invalid. Error while validating "></reason>
<detail>Invalid XML Request</detail>
</makefault>
<log level="full" category="ERROR"></log>
<payloadFactory media-type="xml">
<format>
<Error>
<Header>$2</Header>
<ErrorCode>101504</ErrorCode>
<ErrorMessage>Error while validating XML</ErrorMessage>
<ErrorException>$1</ErrorException>
</Error>
</format>
<args>
<arg expression="$ctx:ERROR_MESSAGE" evaluator="xml"></arg>
<arg expression="$ctx:CMMHeader" evaluator="xml"></arg>
</args>
</payloadFactory>
<send>
<endpoint key="conf:/GLBookingErrorQueue_EPLOCAL"></endpoint>
</send>
<drop></drop>
</on-fail>
</validate>
答案 0 :(得分:1)
应定义主模式外部的外部模式文件 使用资源元素
<validate>
<schema key="conf:/XSD/Employee.xsd"/>
<resource location="XSD/Address.xsd" key="conf:XSD/Address.xsd"/>
<on-fail>
<makefault version="soap11">
<code xmlns:soap11Env="http://schemas.xmlsoap.org/soap/envelope/" value="soap11Env:Client"/>
<reason value="Invalid xml"/>
<role/>
</makefault>
</on-fail>
</validate>
location属性的值(上例中的XSD / Address.xsd)应该是schemaLocation属性的值
以下是示例模式文件
的employee.xsd
<xs:schema elementFormDefault="qualified" targetNamespace="http://schemas.srd.com/Employee" xmlns="http://schemas.srd.com/Employee" xmlns:addr="http://schemas.srd.com/Address" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:import namespace="http://schemas.srd.com/Address" schemaLocation="XSD/Address.xsd"/>
<xs:element name="Employee">
<xs:complexType>
<xs:sequence>
<xs:element minOccurs="0" name="ename" type="xs:string"/>
<xs:element minOccurs="0" name="deptno" type="xs:string"/>
<xs:element minOccurs="0" name="hiredate" type="xs:string"/>
<xs:element minOccurs="0" name="job" type="xs:string"/>
<xs:element minOccurs="0" name="mgr" type="xs:string"/>
<xs:element minOccurs="0" name="sal" type="xs:decimal"/>
<xs:element minOccurs="0" name="comm" type="xs:decimal"/>
<xs:element ref="addr:Address"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
Address.xsd
<xs:schema elementFormDefault="qualified" targetNamespace="http://schemas.srd.com/Address" xmlns="http://schemas.srd.com/Address" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="Address">
<xs:complexType>
<xs:sequence>
<xs:element minOccurs="0" name="city" type="xs:string"/>
<xs:element minOccurs="0" name="state" type="xs:string"/>
<xs:element minOccurs="0" name="country" type="xs:string"/>
<xs:element minOccurs="0" name="postalCode" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>