Skip to content

Commit e85f4f4

Browse files
committed
Implements anyType elements handling
1 parent dfb363e commit e85f4f4

File tree

3 files changed

+31
-12
lines changed

3 files changed

+31
-12
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,5 +73,5 @@ For each entry:
7373
- `length`, `minLength` and `maxLength` fields are optional
7474

7575

76-
## Current limitations
76+
## Current known limitations
7777
- `<xsd:duration>` and `<xsd:QName>` data types not supported

src/xml_factory/xml_generator.py

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
from xmlschema.validators import (
88
XMLSchema,
99
XMLSchemaValidationError,
10+
XsdAnyAttribute,
11+
XsdAnyElement,
1012
XsdAttribute,
1113
XsdElement,
1214
XsdFacet,
@@ -205,17 +207,18 @@ def _generate_xml_complex_type_element(self, xsd_element: XsdElement) -> Element
205207
xsd_simple_type=xsd_simple_type
206208
)
207209
attribute_name: str
208-
xsd_attribute: XsdAttribute
210+
xsd_attribute: XsdAttribute | XsdAnyAttribute
209211
for attribute_name, xsd_attribute in xsd_element.attributes.items():
210-
if xsd_attribute.fixed is not None:
211-
xml_element.attrib[attribute_name] = xsd_attribute.fixed
212-
elif xsd_attribute.default is not None and self._force_default_value:
213-
xml_element.attrib[attribute_name] = xsd_attribute.default
214-
else:
215-
xml_element.attrib[attribute_name] = self._generate_xml_simple_type_value(
216-
element_name=attribute_name,
217-
xsd_simple_type=xsd_attribute.type
218-
)
212+
if isinstance(xsd_attribute, XsdAttribute):
213+
if xsd_attribute.fixed is not None:
214+
xml_element.attrib[attribute_name] = xsd_attribute.fixed
215+
elif xsd_attribute.default is not None and self._force_default_value:
216+
xml_element.attrib[attribute_name] = xsd_attribute.default
217+
else:
218+
xml_element.attrib[attribute_name] = self._generate_xml_simple_type_value(
219+
element_name=attribute_name,
220+
xsd_simple_type=xsd_attribute.type
221+
)
219222
return xml_element
220223

221224
def _populate_xml_group_element(self, xml_parent_element: Element, xsd_group: XsdGroup) -> None:
@@ -233,13 +236,19 @@ def _populate_xml_group_element(self, xml_parent_element: Element, xsd_group: Xs
233236
else:
234237
raise NotImplementedError(f'Unknown group model \'{xsd_group.model}\'')
235238

236-
def _handle_group_content(self, xml_parent_element: Element, group_content: XsdElement | XsdGroup) -> None:
239+
def _handle_group_content(
240+
self,
241+
xml_parent_element: Element,
242+
group_content: XsdElement | XsdAnyElement | XsdGroup
243+
) -> None:
237244
number_of_occurrences: int = (
238245
self._group_content_number_of_occurrences_getter.get_group_content_number_of_occurrences(group_content)
239246
)
240247
for _ in range(number_of_occurrences):
241248
if isinstance(group_content, XsdElement):
242249
xml_parent_element.append(self._generate_xml_element(group_content))
250+
elif isinstance(group_content, XsdAnyElement):
251+
pass
243252
elif isinstance(group_content, XsdGroup):
244253
self._populate_xml_group_element(xml_parent_element=xml_parent_element, xsd_group=group_content)
245254
else:

tests/fixtures/files/test_schema.xsd

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,16 @@
1010
<xs:element ref="Payment" minOccurs="0"/>
1111
<xs:element ref="Shipping"/>
1212
<xs:element ref="Notes" minOccurs="0" maxOccurs="5"/>
13+
<xs:element name="CustomerAdditionalData" type="xs:anyType" minOccurs="0"/>
14+
<xs:element name="PaymentAdditionalData" minOccurs="0">
15+
<xs:complexType>
16+
<xs:complexContent>
17+
<xs:extension base="xs:anyType">
18+
<xs:attribute name="id" type="xs:string" use="required"/>
19+
</xs:extension>
20+
</xs:complexContent>
21+
</xs:complexType>
22+
</xs:element>
1323
</xs:sequence>
1424
<xs:attribute name="orderDate" type="xs:date" use="required"/>
1525
<xs:attribute name="id" type="OrderNumberType" use="required"/>

0 commit comments

Comments
 (0)