0
# XSD and Type Handling
1
2
XML Schema processing, type conversion, and handling of complex XML structures. Zeep provides comprehensive tools for working with XSD types and converting between Python objects and XML representations.
3
4
## Capabilities
5
6
### AnyObject Container
7
8
Container for arbitrary XML content that doesn't fit standard XSD types.
9
10
```python { .api }
11
class AnyObject:
12
def __init__(self, xsd_object, value):
13
"""
14
Create container for arbitrary XML content.
15
16
Parameters:
17
- xsd_object: XSD type or element definition
18
- value: Python value to wrap
19
"""
20
21
@property
22
def xsd_type(self):
23
"""XSD type definition for this object."""
24
25
@property
26
def xsd_elm(self):
27
"""XSD element definition for this object."""
28
```
29
30
### Schema Processing
31
32
Core XSD schema processing and type registry.
33
34
```python { .api }
35
class Schema:
36
def __init__(self, node, location=None, types=None):
37
"""
38
Process XSD schema document.
39
40
Parameters:
41
- node: XML schema node (lxml.etree._Element)
42
- location: Schema location URL
43
- types: Parent types registry
44
"""
45
46
def get_type(self, qname: str):
47
"""Get XSD type by qualified name."""
48
49
def get_element(self, qname: str):
50
"""Get XSD element by qualified name."""
51
```
52
53
### Value Objects
54
55
Structured value containers for complex XSD types.
56
57
```python { .api }
58
class CompoundValue:
59
"""
60
Container for complex type values with attribute access.
61
62
Provides dict-like and object-like access to complex type data.
63
"""
64
65
def __getitem__(self, key: str):
66
"""Dictionary-style access to values."""
67
68
def __setitem__(self, key: str, value):
69
"""Dictionary-style value assignment."""
70
71
def __getattr__(self, key: str):
72
"""Attribute-style access to values."""
73
```
74
75
### XSD Constants
76
77
Special values for XSD processing.
78
79
```python { .api }
80
# XSD nil value representation
81
Nil: object
82
83
# Skip value placeholder for optional elements
84
SkipValue: object
85
```
86
87
### Helper Functions
88
89
Utility functions for working with XSD objects and serialization.
90
91
```python { .api }
92
def serialize_object(obj):
93
"""
94
Serialize zeep objects to plain Python dictionaries.
95
96
Parameters:
97
- obj: Zeep object to serialize
98
99
Returns:
100
dict: Plain Python dictionary representation
101
"""
102
```
103
104
## Usage Examples
105
106
### Working with Complex Types
107
108
```python
109
from zeep import Client
110
111
client = Client('http://example.com/service.wsdl')
112
113
# Get complex type definition
114
person_type = client.get_type('{http://example.com}Person')
115
116
# Create instance of complex type
117
person = person_type(
118
name='John Doe',
119
age=30,
120
address={
121
'street': '123 Main St',
122
'city': 'Anytown',
123
'state': 'CA'
124
}
125
)
126
127
# Use in SOAP operation
128
result = client.service.UpdatePerson(person=person)
129
```
130
131
### AnyObject Usage
132
133
```python
134
from zeep import Client
135
from zeep.xsd import AnyObject
136
137
client = Client('http://example.com/service.wsdl')
138
139
# Create AnyObject for arbitrary XML content
140
any_type = client.get_type('{http://www.w3.org/2001/XMLSchema}anyType')
141
custom_data = AnyObject(any_type, {
142
'custom_field': 'custom_value',
143
'nested': {
144
'data': 'nested_value'
145
}
146
})
147
148
# Use in operation that accepts any content
149
result = client.service.ProcessAnyData(data=custom_data)
150
```
151
152
### Type Inspection
153
154
```python
155
from zeep import Client
156
157
client = Client('http://example.com/service.wsdl')
158
159
# Inspect available types
160
print("Available types:")
161
for qname in client.wsdl.types.documents[0].types:
162
print(f" {qname}")
163
164
# Get type details
165
address_type = client.get_type('{http://example.com}Address')
166
print(f"Type signature: {address_type.signature()}")
167
168
# Inspect type structure
169
print("Type elements:")
170
for element in address_type.elements:
171
print(f" {element.name}: {element.type}")
172
```
173
174
### Element Creation
175
176
```python
177
from zeep import Client
178
179
client = Client('http://example.com/service.wsdl')
180
181
# Get element definition
182
address_element = client.get_element('{http://example.com}Address')
183
184
# Create element instance
185
address = address_element(
186
street='456 Oak Ave',
187
city='Other City',
188
state='NY',
189
zip='54321'
190
)
191
192
# Use element in operation
193
result = client.service.ValidateAddress(address)
194
```
195
196
### Handling Nil Values
197
198
```python
199
from zeep import Client
200
from zeep.xsd import Nil
201
202
client = Client('http://example.com/service.wsdl')
203
204
# Create object with nil value
205
person_type = client.get_type('{http://example.com}Person')
206
person = person_type(
207
name='Jane Doe',
208
age=25,
209
middle_name=Nil, # Explicitly set to nil
210
phone_number=None # Will be omitted
211
)
212
213
result = client.service.CreatePerson(person=person)
214
```
215
216
### Skip Optional Elements
217
218
```python
219
from zeep import Client
220
from zeep.xsd import SkipValue
221
222
client = Client('http://example.com/service.wsdl')
223
224
# Skip optional elements completely
225
config_type = client.get_type('{http://example.com}Configuration')
226
config = config_type(
227
required_setting='value',
228
optional_setting=SkipValue, # Skip this element entirely
229
another_setting='another_value'
230
)
231
232
result = client.service.UpdateConfiguration(config=config)
233
```
234
235
### Custom Type Creation
236
237
```python
238
from zeep import Client
239
from zeep.xsd import ComplexType, Element, Sequence
240
241
client = Client('http://example.com/service.wsdl')
242
243
# Create custom type dynamically (advanced usage)
244
custom_type = ComplexType(
245
Sequence([
246
Element('name', client.wsdl.types.get_type('{http://www.w3.org/2001/XMLSchema}string')),
247
Element('value', client.wsdl.types.get_type('{http://www.w3.org/2001/XMLSchema}int'))
248
])
249
)
250
251
# Create instance
252
custom_obj = custom_type(name='test', value=42)
253
```
254
255
### Type Conversion and Serialization
256
257
```python
258
from zeep import Client
259
from zeep.helpers import serialize_object
260
261
client = Client('http://example.com/service.wsdl')
262
263
# Call operation and get complex result
264
result = client.service.GetPersonDetails(person_id=123)
265
266
# Convert zeep objects to plain Python dict
267
plain_dict = serialize_object(result)
268
print(plain_dict)
269
270
# Now you can use standard JSON serialization, etc.
271
import json
272
json_data = json.dumps(plain_dict, default=str)
273
```
274
275
### Working with Arrays/Lists
276
277
```python
278
from zeep import Client
279
280
client = Client('http://example.com/service.wsdl')
281
282
# Create list of complex objects
283
person_type = client.get_type('{http://example.com}Person')
284
people = [
285
person_type(name='Alice', age=25),
286
person_type(name='Bob', age=30),
287
person_type(name='Charlie', age=35)
288
]
289
290
# Use list in operation
291
result = client.service.ProcessPeople(people=people)
292
293
# Handle array results
294
for person in result.processed_people:
295
print(f"{person.name}: {person.status}")
296
```