使用Zeep处理含xsd:choice的getDetail请求参数问题
<choice> for the getDetail WebService Operation Got it, let's tackle this Zeep + XSD choice issue step by step. First, let's recap what your XSD is saying: the getDetail operation expects a request where you send exactly one of four possible parameters—uno, dos, tres, or cuatro. Zeep natively supports XSD <choice> elements, but you need to make sure you're passing only the selected field in your service call.
How to Structure Your Zeep Calls
First, initialize your Zeep client as usual:
from zeep import Client # Replace with your actual WSDL URL or local path client = Client("http://your-webservice-endpoint?wsdl")
Then, use one of the following examples based on which parameter you need to send:
1. Using the uno string parameter
Simply pass the uno keyword argument with your string value:
response = client.service.getDetail(uno="your-unique-string-for-uno")
2. Using the dos custom DosType parameter
First, create an instance of the custom DosType using Zeep's get_type method, then pass it as the dos argument:
# Get the DosType definition from the WSDL DosType = client.get_type("uct:DosType") # Create an instance (fill in all required fields for DosType here) dos_value = DosType( example_field_1="some-value", example_field_2=456, # Add any other required fields for your specific DosType ) # Call the service with the dos parameter response = client.service.getDetail(dos=dos_value)
3. Using the tres custom E164Type parameter
Same approach as DosType—create an instance of the custom type first:
# Get the E164Type definition E164Type = client.get_type("uct:E164Type") # Create an instance (adjust fields to match your E164Type structure) tres_value = E164Type( phone_number="+1-800-555-1234" ) # Call the service with the tres parameter response = client.service.getDetail(tres=tres_value)
4. Using the cuatro string parameter
Just like uno, pass the string value directly:
response = client.service.getDetail(cuatro="your-string-value-for-cuatro")
Common Pitfalls to Avoid
- Don't pass multiple choice parameters: The XSD strictly requires only one of the four options. If you send more than one (e.g.,
uno="foo"anddos=bar_instance), Zeep will throw a validation error. - Validate custom types: If
DosTypeorE164Typehave required fields, you must populate all of them. You can inspect the structure of these types by printing the result ofclient.get_type("uct:DosType")—it will show you all required and optional fields. - Debug if things go wrong: If you're still getting errors, enable Zeep's debug logging to see the raw SOAP request being sent. This helps you confirm if the payload matches what the WebService expects:
import logging logging.basicConfig(level=logging.DEBUG)
内容的提问来源于stack exchange,提问作者mhkore

