oboratav / python-onix

Dataclasses for EDItEUR's ONIX standards
MIT License
3 stars 0 forks source link

ParserError when trying to use the Onixmessage class #3

Open smcalilly opened 11 hours ago

smcalilly commented 11 hours ago

I try to run the code from the README in my local environment with python-onix==0.0.3 and Python 3.12.3, but I get this error:

  File "/Users/sam/code/sample_files_and_onix_guide/test.py", line 5, in <module>
    message = parser.parse("g-sample.onix", Onixmessage)
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/sam/.pyenv/versions/3.12.3/lib/python3.12/site-packages/xsdata/formats/dataclass/parsers/bases.py", line 63, in parse
    result = handler.parse(source, ns_map)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/sam/.pyenv/versions/3.12.3/lib/python3.12/site-packages/xsdata/formats/dataclass/parsers/handlers/lxml.py", line 41, in parse
    return self.process_context(ctx, ns_map)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/sam/.pyenv/versions/3.12.3/lib/python3.12/site-packages/xsdata/formats/dataclass/parsers/handlers/lxml.py", line 59, in process_context
    self.parser.start(
  File "/Users/sam/.pyenv/versions/3.12.3/lib/python3.12/site-packages/xsdata/formats/dataclass/parsers/bases.py", line 99, in start
    child = item.child(qname, attrs, ns_map, len(objects))
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/sam/.pyenv/versions/3.12.3/lib/python3.12/site-packages/xsdata/formats/dataclass/parsers/nodes/element.py", line 472, in child
    raise ParserError(f"Unknown property {self.meta.qname}:{qname}")
xsdata.exceptions.ParserError: Unknown property {http://ns.editeur.org/onix/3.0/short}ONIXmessage:{http://ns.editeur.org/onix/3.0/reference}Header

Here is my code:

from onix.book.v3_0.short.strict import Onixmessage
from xsdata.formats.dataclass.parsers import XmlParser

parser = XmlParser()
message = parser.parse("google-sample.onix", Onixmessage)

I'm using the onix file you can download in this Google Documentation: https://support.google.com/books/partner/answer/6374180?hl=en. It appears to be the right onix version for this code. I'm having this issue when I try other sample onix files, as well.

oboratav commented 9 hours ago

Hello!

This is a library that I put together really quickly, and then never really got around to polishing. Thank you so much for reminding me that it exists—I'll try to put some more time into it and turn it into something more useable.

It looks like you're trying to parse a file that uses the reference namespace (http://ns.editeur.org/onix/3.0/reference) with the parser that's intended for the short namespace (http://ns.editeur.org/onix/3.0/short).

An easy way to tell between the two namespaces is that the reference namespace uses human-readable tags (like <NamesBeforeKey>Jack</NamesBeforeKey>) as opposed to coded tags that are shorter (<b039>Jack</b039>).

Try using the parser for the reference namespace, and you should get much better results:

from onix.book.v3_0.reference import Onixmessage

from xsdata.formats.dataclass.parsers import XmlParser

parser = XmlParser()
message = parser.parse("google-sample.onix", Onixmessage)

Please give that a try when you get a chance, and let me know if anything else comes up!