Unable to bind to pyxb classes with nested (anonymous) types

后端 未结 1 861
挽巷
挽巷 2021-01-24 20:46

I followed the instructions from this thread, and from this XML:


    



        
1条回答
  •  夕颜
    夕颜 (楼主)
    2021-01-24 21:42

    Denormalized schema are difficult, and you may need to try several approaches to provide the information required. Here's an annotated example, though I'm using PyXB 1.2.3 so the capabilities might be a little more complete:

    import pyxb
    import my_schema
    
    rep = my_schema.my_report()
    
    # The something element here is very simple, with a single string
    # element.  For the inner string element foo PyXB can figure things
    # out for itself.  For the outer element it needs help.
    #
    # In a normalized schema where the type of something was tSomething,
    # you would do:
    #
    #   rep.something = tSomething('yes')
    #
    # Without a tSomething easily reachable, to build it up piece-by-piece
    # you could do:
    rep.something = pyxb.BIND()  # Create an instance of whatever type satisfies something
    rep.something.foo = 'yes'    # Assign to the foo element of what got created
    
    # You can then optimize.  Here pyxb.BIND substitutes for the something
    # element wrapper around that string, and figures out for itself that
    # the "yes" can only go in the foo element:
    rep.something = pyxb.BIND('yes')
    
    # In fact, sometimes PyXB can even figure out the intermediate
    # intermediate layers:
    rep.something = 'yes'
    # No more than two of them, though (and the lowest must be a simple type).
    
    # Similarly here pyxb.BIND substitutes for the type of the
    # something_else element.  Again the inner content is unambiguous and
    # sequential, so the values can be provided in the constructor.
    rep.something_else = pyxb.BIND(4, 'finally', 0.2)
    
    print rep.toxml('utf-8')
    

    0 讨论(0)
提交回复
热议问题