dapper91 / pydantic-xml

python xml for humans
https://pydantic-xml.readthedocs.io
The Unlicense
155 stars 16 forks source link

How to handle XML Schema `sequence`, `any`, and `anyAttribute`? #24

Closed Jacob-Flasheye closed 1 year ago

Jacob-Flasheye commented 1 year ago

Hi.

I'm trying to implement a schema that includes sequence, any, and anyAttribute tags. I am still getting familiar with XML (have worked with JSON data mostly) and I'm not sure how, or if it's even possible currently, to implement such types in this library.

Here are two snippets from the schema file:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema>
  <xs:sequence>
    <xs:any namespace="##any" processContents="lax" minOccurs="0" maxOccurs="unbounded"/>
  <xs:sequence>
  <xs:anyAttribute processContents="lax"/>
</xs:schema>

If it is not possible to do, are there any plans to implement this feature? If there is interest, I'm willing to help!

sshishov commented 1 year ago

I guess sequence is implemented using list[...] and here minOccurs=0 means that it can be omitted, therefore you have to type something like:

class SequenceXmlModel(BaseXmlModel, tag='sequence'):
    sequence_item: list[str] = element()

Which will work with:

<sequence>
    <sequence_item>a</sequence_item>
    <sequence_item>b</sequence_item>
    <sequence_item>c</sequence_item>
</sequence>

RESULT: {'sequence': ['a', 'b', 'c']}

and

<sequence>
</sequence>

RESULT: {'sequence': []}
dapper91 commented 1 year ago

Discussed in https://github.com/dapper91/pydantic-xml/issues/100.