You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何定义接受任意XML文档的WSDL操作/消息(Python Zeep客户端)

修复WSDL以支持任意XML输入输出

要让WSDL的操作支持接收和返回任意XML结构,核心是用XML Schema的<xsd:any>类型来定义操作的输入输出参数。以下是具体的修改步骤和Zeep客户端的使用方法:

1. 修改WSDL的核心部分

步骤1:添加类型定义(可选但推荐)

在WSDL的<types>块中定义一个包含任意XML的复合类型,方便复用:

<wsdl:types>
  <xsd:schema targetNamespace="http://your-service-namespace.com"
              xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <xsd:complexType name="AnyXmlPayload">
      <xsd:sequence>
        <!-- ##any 允许任意命名空间的XML元素,processContents="lax" 宽松验证 -->
        <xsd:any namespace="##any" processContents="lax" minOccurs="1" maxOccurs="unbounded"/>
      </xsd:sequence>
    </xsd:complexType>
  </xsd:schema>
</wsdl:types>

processContents可选值说明:

  • strict:默认值,必须匹配已定义的Schema类型,不适合任意XML场景
  • lax:尝试验证能识别的元素,无法识别的直接跳过
  • skip:完全跳过验证,最适合完全任意的XML结构

步骤2:更新操作的输入输出消息

把原来空的operationRequest和operationResponse消息,替换为包含任意XML参数的定义:

<!-- 输入消息 -->
<wsdl:message name="operationRequest">
  <wsdl:part name="inputPayload" type="tns:AnyXmlPayload"/>
</wsdl:message>

<!-- 输出消息 -->
<wsdl:message name="operationResponse">
  <wsdl:part name="outputPayload" type="tns:AnyXmlPayload"/>
</wsdl:message>

步骤3:确保操作绑定正确

确认<operation>节点正确关联修改后的消息:

<wsdl:operation name="operation">
  <wsdl:input message="tns:operationRequest"/>
  <wsdl:output message="tns:operationResponse"/>
</wsdl:operation>

如果不想单独定义复合类型,也可以直接在消息中使用<xsd:any>:

<wsdl:message name="operationRequest">
  <wsdl:part name="inputPayload" element="xsd:any"/>
</wsdl:message>

2. Zeep客户端的使用示例

修改完WSDL后,用Zeep生成客户端,就可以传入任意XML结构了:

from zeep import Client, xsd

# 加载修改后的WSDL
client = Client('path/to/modified.wsdl')

# 方式1:直接传入XML字符串
raw_xml = '<CustomRoot><UserInfo>John</UserInfo><Age>30</Age></CustomRoot>'
response = client.service.operation(xsd.AnyXml(raw_xml))

# 方式2:用Zeep的API构建XML元素
root_element = xsd.Element('{http://your-custom-namespace.com}CustomRoot')
user_info = xsd.Element('{http://your-custom-namespace.com}UserInfo', xsd.String())
age_element = xsd.Element('{http://your-custom-namespace.com}Age', xsd.Integer())

root_element.append(user_info('John'))
root_element.append(age_element(30))
response = client.service.operation(root_element)

内容的提问来源于stack exchange,提问作者Seweryn Niemiec

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.03 05:05:15