python - Unable to bind to pyxb classes with nested (anonymous) types -
i followed instructions this thread, , xml:
<?xml version="1.0" encoding="utf-8" ?> <my_report> <something> <foo> yes </foo> </something> <something_else> <id>4</id> <foo>finally</foo> <score>0.2</score> </something_else> </my_report>
i created following xsd schema using this tool online.
<xs:schema attributeformdefault="unqualified" elementformdefault="qualified" xmlns:xs="http://www.w3.org/2001/xmlschema"> <xs:element name="my_report"> <xs:complextype> <xs:sequence> <xs:element name="something"> <xs:complextype> <xs:sequence> <xs:element type="xs:string" name="foo"/> </xs:sequence> </xs:complextype> </xs:element> <xs:element name="something_else"> <xs:complextype> <xs:sequence> <xs:element type="xs:byte" name="id"/> <xs:element type="xs:string" name="foo"/> <xs:element type="xs:float" name="score"/> </xs:sequence> </xs:complextype> </xs:element> </xs:sequence> </xs:complextype> </xs:element> </xs:schema>
i called pyxben -u my_schema.csd -m my_schema
in shell, , tried use bindings build objects:
from my_schema import my_report my_xml_report = my_report()
this seems work far (i can access my_xml_report.something
). however, when try fill in nested element:
my_xml_report.something.foo = "no"
i error 'nonetype'object has no atttribute 'foo'
.
the documentation talks anonymous types
seem related problem, still can't work:
import pyxb my_xml_report.something = pyxb.bind('foo', "no")
i error mixedcontenterror: invalid non-element content
how can fill in xml?
denormalized schema difficult, , may need try several approaches provide information required. here's annotated example, though i'm using pyxb 1.2.3 capabilities might little more complete:
import pyxb import my_schema rep = my_schema.my_report() # element here simple, single string # element. inner string element foo pyxb can figure things # out itself. outer element needs help. # # in normalized schema type of tsomething, # do: # # rep.something = tsomething('yes') # # without tsomething reachable, build piece-by-piece # do: rep.something = pyxb.bind() # create instance of whatever type satisfies rep.something.foo = 'yes' # assign foo element of got created # can optimize. here pyxb.bind substitutes # element wrapper around string, , figures out # "yes" can go in foo element: rep.something = pyxb.bind('yes') # in fact, pyxb can figure out intermediate # intermediate layers: rep.something = 'yes' # no more 2 of them, though (and lowest must simple type). # here pyxb.bind substitutes type of # something_else element. again inner content unambiguous , # sequential, values can provided in constructor. rep.something_else = pyxb.bind(4, 'finally', 0.2) print rep.toxml('utf-8')
Comments
Post a Comment