I'm dealing with a third party XSD schema to generate python classes.
The application will parse XML files to extract data. It's not about
creating xml files.
The generated code works fine when parsing any valid `xml` data file.

Anyway the results differ somewhat than expected. I got about 50 class
names which look like CTD_ANON... I guess this is due to the extensive
use of anonymous types in the schema. In turn I can not bind them
because I don't know how to.

On the other hand perhaps everything is because of my own ignorance.
Well, I can not deny that either.

In case someone is interested the complete xsd schema and other details
here. https://bitbucket.org/emezeta/pyefuy

So to generate the classes:

    $~ pyxbgen -u EnvioCFE_entreEmpresas.xsd -m eceelib

produces three files:

    _ds.py
    _xenc.py
    eceelib.py

then:

    $~ python
      Python 2.7.9 (default, Jun 29 2016, 13:08:31) [GCC 4.9.2] on linux2

    >>>
    >>> import eceelib
    >>> with open("./xml/Sob_111111111111_20161207_99999.xml") as f:
    ...     xml_str = f.read()
    ...
    >>> xml_doc = eceelib.CreateFromDocument(xml_str)


results in:

    Python 2.7.9 (default, Jun 29 2016, 13:08:31) [GCC 4.9.2] on linux2
    >>>
    >>> import eceelib
    >>> with open("./xml/Sob_111111111111_20161207_99999.xml") as f:
    ...     xml_str = f.read()
    ...
    >>> xml_doc = eceelib.CreateFromDocument(xml_str)

    Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    File "eceelib.py", line 71, in CreateFromDocument
        saxer.parse(io.BytesIO(xmld))
    File "/usr/lib/python2.7/xml/sax/expatreader.py", line 107, in parse
        xmlreader.IncrementalParser.parse(self, source)
    File "/usr/lib/python2.7/xml/sax/xmlreader.py", line 123, in parse
        self.feed(buffer)
    File "/usr/lib/python2.7/xml/sax/expatreader.py", line 210, in feed
        self._parser.Parse(data, isFinal)
    File "/usr/lib/python2.7/xml/sax/expatreader.py", line 351, in 
end_element_ns
        self._cont_handler.endElementNS(pair, None)
    File "/usr/local/lib/python2.7/dist-packages/pyxb/binding/saxer.py", line 
388, in endElementNS
        binding_object = this_state.endBindingElement()
    File "/usr/local/lib/python2.7/dist-packages/pyxb/binding/saxer.py", line 
226, in endBindingElement
        self.__constructElement(self.__delayedConstructor, self.__attributes, 
args)
    File "/usr/local/lib/python2.7/dist-packages/pyxb/binding/saxer.py", line 
116, in __constructElement
        self.__bindingInstance = new_object_factory(*content, **kw)
    File "/usr/local/lib/python2.7/dist-packages/pyxb/binding/basis.py", line 
1613, in __call__
        args = [ self.compatibleValue(args[0], **kw) ]
    File "/usr/local/lib/python2.7/dist-packages/pyxb/binding/basis.py", line 
1635, in compatibleValue
        raise pyxb.ElementChangeError(self, value)

    pyxb.exceptions_.ElementChangeError: Value 2 for element 
{http://cfe.dgi.gub.uy}TipoDocRecep incompatible with fixed content
    >>>

full ipython trace image http://bit.ly/2kSoRVa

Then I slightly modified the "CFEType.xsd" file to deal with this issue
despite '2' being the proper `fixed` value in both xsd schema and xml
data file.

My workarround on "CFEType.xsd" file was:

<    <xs:element name="TipoDocRecep" type="DGICFE:DocType" fixed="2">
---
>    <xs:element name="TipoDocRecep" type="DGICFE:DocType">   # this
line works


Now the xml file is parsed smoothly.

    >>> xml_doc = eceelib.CreateFromDocument(xml_str)
    >>>

As far as I can get things seems to go fine,

This is a small experience with pyxb, hopefully useful.
I'll be glad receiving any suggestion, insights, fixes to improve and
learn or simply find a better way doing so.

Thanks,
emezeta

ps: pyxb.exceptions_.ElementChangeError: exception as an issue can be
seen here http://bit.ly/2kSQAoV

Attachment: 0x2B86902A.asc
Description: application/pgp-keys

Attachment: signature.asc
Description: OpenPGP digital signature

------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, SlashDot.org! http://sdm.link/slashdot
_______________________________________________
pyxb-users mailing list
pyxb-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/pyxb-users

Reply via email to