I followed the instructions from this thread, and from this XML:
我遵循了这个线程和这个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 the following XSD schema using this tool online.
我在网上使用这个工具创建了以下XSD模式。
<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 then called pyxben -u my_schema.csd -m my_schema
in the shell, and then tried to use the bindings build objects:
然后我调用pyxben - myu _schema。csd -m my_schema在shell中,然后尝试使用绑定构建对象:
from my_schema import my_report
my_xml_report = my_report()
This seems to work so far (I can access my_xml_report.something
). However, when I try to fill in a nested element:
这似乎起了作用(我可以访问my_xml_report.something)。但是,当我尝试填充嵌套元素时:
my_xml_report.something.foo = "No"
I get the error 'NoneType'object has no atttribute 'foo'
.
我得到错误'NoneType'对象没有对应的'foo'
The documentation talks about anonymous types
which seem to be related to my problem, but I still can't get it to work:
文件中提到的匿名类型似乎与我的问题有关,但我还是不能让它发挥作用:
import pyxb
my_xml_report.something = pyxb.BIND('foo', "No")
I get the error MixedContentError: invalid non-element content
我得到了error MixedContentError:无效的非元素内容
How can I fill in this XML?
如何填写这个XML?
1 个解决方案
#1
5
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:
非规范化模式很困难,您可能需要尝试几种方法来提供所需的信息。这里有一个带注释的例子,尽管我使用的是PyXB 1.2.3,所以功能可能更完整一些:
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')
#1
5
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:
非规范化模式很困难,您可能需要尝试几种方法来提供所需的信息。这里有一个带注释的例子,尽管我使用的是PyXB 1.2.3,所以功能可能更完整一些:
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')