pabigot / pyxb

Python XML Schema Bindings
Apache License 2.0
130 stars 74 forks source link

UBL 2.1 generated bindings, unable to read or write xml files #102

Closed malicz closed 6 years ago

malicz commented 6 years ago

Hi,

What I did:

wget -r --no-parent http://docs.oasis-open.org/ubl/os-UBL-2.1/xsd/
pyxbgen -u docs.oasis-open.org/ubl/os-UBL-2.1/xsd/maindoc/UBL-Invoice-2.1.xsd -m invoice --module-prefix=ubl
wget http://docs.oasis-open.org/ubl/os-UBL-2.1/xml/UBL-Invoice-2.1-Example-Trivial.xml

the code to call it:

from __future__ import print_function
import ubl.invoice as invoice

xml = open('UBL-Invoice-2.1-Example-Trivial.xml').read()
simple_invoice = invoice.CreateFromDocument(xml)

print('Invoice IssueDate: ' + simple_invoice.IssueDate)

This results in:

TypeError: cannot concatenate 'str' and 'IssueDateType' objects

With the pycharm debugger I have looked into the objects and I cannot find the "2011-09-22" stored anywhere. This is a complex schema.

Did i miss anything?

pabigot commented 6 years ago

You need to be familiar with the content of the schemas to use PyXB effectively.

IssueDateType is derived from {urn:oasis:names:specification:ubl:schema:xsd:UnqualifiedDataTypes-2}DateType, which is a complex type with simple content derived from {http://www.w3.org/2001/XMLSchema}date. It is not a string. Its value is an instance of a binding class that extends Python's date type.

print('Invoice IssueDate: ' + str(simple_invoice.IssueDate.value()))
print('Invoice IssueDate: ' + simple_invoice.IssueDate.value())

produces

Invoice IssueDate: 2011-09-22 00:00:00
Traceback (most recent call last):
  File "issue102.py", line 8, in <module>
    print('Invoice IssueDate: ' + simple_invoice.IssueDate.value())
TypeError: cannot concatenate 'str' and 'date' objects

which is correct behavior.

malicz commented 6 years ago

Thank you for your reply and off course for this powerful library. I understood the complex types but I did not see a way to call the date object from the IssueDateType. In the generated code I saw:

IssueDate = property(__IssueDate.value, __IssueDate.set, None, None)

So the value() was already called, using the debugger to inspect the object brought some confusion so then I asked.

Everything is working now, so thank you again

pabigot commented 6 years ago

Any complex type that has simple content provides access to the content through the value() method. This allows attributes to be accessed through other properties.

You're welcome.