SOAP 响应架构验证 [英] SOAP Response Schema Validation

查看:49
本文介绍了SOAP 响应架构验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个 XSD 来验证我的 SOAP 服务的响应.我觉得有必要只导入 http://schemas.xmlsoap.org/soap/envelope/而不是重新定义 SOAP 元素,例如 Envelope、Head 和 Body,但是 Body 的 xmlsoap.org 架构定义对于我来说太宽泛了 -- 一旦我导入 SOAP 架构,我的 XSD(我已经为我的服务精心定制)突然验证了所有 SOAP 消息.

I'm trying to write an XSD that will validate my SOAP service's responses. I feel compelled to just import http:// schemas.xmlsoap.org/soap/envelope/ instead of redefining the SOAP elements like Envelope, Head, and Body, but that xmlsoap.org schema definition of Body is too broad for my use--as soon as I import the SOAP Schema, suddenly my XSD (that I've carefully tailored to my service) validates all SOAP messages.

我应该如何处理 XSD 中 SOAP 信封、头部、正文的定义?

我怀疑问题在于我正在尝试重新使用我不应该尝试重新使用的其他模式.当然,这些 SOAP 模式旨在定义(所有)SOAP 消息应该是什么样子.也许我只需要在我的模式中定义我希望我的特定肥皂体看起来像什么.

I suspect the problem is that I'm trying to re-use other schemas that I shouldn't be trying to re-use. Certainly, those Schemas for SOAP are intended to define what (all) SOAP messages should look like. And maybe I just need to define in my schema what I want my particular soap body to look like.

我可能刚刚回答了我自己的问题.也许有人有不同的解决方案?

I might have just answered my own question. Maybe someone has a different solution?

我在编写 XSD 来描述来自我的 SOAP 服务之一的响应消息时遇到了一些麻烦.

I'm having a little trouble authoring an XSD to describe the response message from one of my SOAP services.

这是我正在尝试验证的来自我的服务的示例响应:

Here's an example response from my service that I'm trying to validate:

<?xml version="1.0" encoding="UTF-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
   <soap:Body>
      <helloResponse xmlns="http://justinfaulkner/wsdl/1.0">
         <Message>Hello, Justin!</Message>
         <Message>Your lucky numbers are: 329, 9</Message>
      </helloResponse>
   </soap:Body>
</soap:Envelope>

我的目标是使用 XSD 验证来自我的服务的响应.因此,我手工编写了一个 XSD 来描述属于我的服务的soap:Body 中的所有类型

My goal is to validate responses from my service with an XSD. So, I hand-authored an XSD that describes all the types that belong in my service's soap:Body

<?xml version="1.0"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://justinfaulkner/wsdl/1.0" xmlns:tns="http://justinfaulkner/wsdl/1.0">
   <xsd:complexType name="helloResponseType">
      <xsd:sequence>
         <xsd:element name="Message" type="xsd:string" minOccurs="0" maxOccurs="unbounded"/>
      </xsd:sequence>
   </xsd:complexType>
   <xsd:element name="helloResponse" type="tns:helloResponseType"/>
</xsd:schema>

当我尝试使用 PHP 的 DOMDocument::schemaValidateSource() 功能使用 Schema(第二个片段)验证示例响应(第一个 XML 片段)时,验证器指出了我的第一个明显错误:

When I tried to validate the example response (first XML snippet) with the Schema (2nd snippet) using PHP's DOMDocument::schemaValidateSource() functionality, the validator pointed out my first obvious mistake:

元素soap:Envelope":没有可用的匹配全局声明

Element 'soap:Envelope': No matching global declaration available

哎呀,呃,"我想,这些元素是在 SOAP 的命名空间中定义的,所以我需要导入 SOAP 的 XSD."

"Oops, duh," I thought, "Those elements are defined in SOAP's namespace, so I need to import SOAP's XSD."

所以我编辑了我的 XSD 并添加了一个导入:

So I edited my XSD and added an import:

<xsd:import namespace="http://schemas.xmlsoap.org/soap/envelope/" schemaLocation="http://schemas.xmlsoap.org/soap/envelope/"/>

它奏效了!当我使用 XSD 验证 soap 响应时,DOMDocument::schemaValidateSource 返回 true.

And it worked! DOMDocument::schemaValidateSource returns true when I validate the soap response with the XSD.

然后,作为健全性检查,我采取了不同的肥皂反应 XSD 我躺在身边:

Then, as a sanity check, I took a different soap response XSD I had lying around:

<?xml version="1.0"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://justinfaulkner/wsdl/1.0" xmlns:tns="http://justinfaulkner/wsdl/1.0">
   <xsd:import namespace="http://schemas.xmlsoap.org/soap/envelope/" schemaLocation="http://schemas.xmlsoap.org/soap/envelope/"/>
   <xsd:complexType name="OtherServiceResponseType">
      <xsd:all>
         <xsd:element name="CompletionCode" type="xsd:string"/>
         <xsd:element name="ResponseMessage" type="xsd:string"/>
      </xsd:all>
   </xsd:complexType>
   <xsd:element name="OtherServiceResponse" type="tns:OtherServiceResponseType"/>
</xsd:schema>

我试图用这个完全不相关的架构来验证我的肥皂响应......

And I tried to validate my soap response with this completely unrelated Schema...

乍一看根本没有描述此消息的架构,也验证了soap响应.

And the schema that, at first glance, doesn't describe this message at all, also validates the soap response.

然后我意识到 XSD 的架构一定是响应针对这两种不同架构进行验证的原因.我从 http://schemas.xmlsoap.org/soap/envelope/ 将 Body 元素定义为:

Then I realize that XSD's Schema must be the reason the response is validating against these two different schemas. The SOAP schema I'm importing from http://schemas.xmlsoap.org/soap/envelope/ defines the Body element to be:

<xs:element name="Body" type="tns:Body" />
<xs:complexType name="Body" >
<xs:sequence>
  <xs:any namespace="##any" minOccurs="0" maxOccurs="unbounded" processContents="lax" />
</xs:sequence>
<xs:anyAttribute namespace="##any" processContents="lax" >
  <xs:annotation>
 <xs:documentation>
   Prose in the spec does not specify that attributes are allowed on the Body element
 </xs:documentation>
  </xs:annotation>
</xs:anyAttribute>
</xs:complexType>

这允许 Body 标签的内容基本上是任何内容.

Which allows the contents of the Body tag to be basically anything.

这是有道理的,XSD 的 XSD 的目的是定义所有 XSD 应该是什么样子,而不仅仅是我的.

That makes sense, the purpose of XSD's XSD is to define what ALL XSDs should look like, not just mine.

所以我的问题是,我应该如何构建一个 XSD 来验证这些 SOAP 响应,如果可能,重用现有的 SOAP XSD?

So my question is, how should I build an XSD to validate these SOAP responses, reusing existing SOAP XSDs if possible?

这是我一直追求的方向...

Here's the direction I've been pursuing...

  • 我想我可以将 xmlsoap.org 的 XSD 架构扔出窗口并自己重新定义 Envelope 和 Body,准确指定应在 Body 元素中显示的内容.但我觉得我最终会在我自己的 XSD 中基本上拥有所有肥皂元素的副本,头部和主体定义略有不同,这感觉像是违反了 DRY.
  • 有没有一种方法可以导入 xmlsoap.org 的 XSD,然后从我的 XSD 中覆盖 soap:Body 的定义?

推荐答案

您应该使用 SOAP Web 服务框架来做到这一点.维基百科页面上列出了多种编程语言.您编写一个 WSDL 来指定您的 Web 服务 API,并在其中导入您的 XSD 以定义有效负载格式(契约优先方法).使用框架提供的 wsdl2xxx 工具生成 API 存根.您编写 API 实现代码.框架将负责其余的工作(处理 SOAP 消息并绑定到您的实现代码).

You should use a SOAP web service framework to do that. There are many for various programming languages listed on the wikipedia page. You write a WSDL to specify your web service API, and in which you import your XSD to define the payload formats (contract-first approach). Use the wsdl2xxx tool provided by the framework to generate the API stub. You write the API implementation code. The framework will take care of the rest (processing the SOAP messages and binding to your implementation code).

这篇关于SOAP 响应架构验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆