给定架构:
<xs:element name="person">
<xs:complexType>
<xs:sequence>
<xs:element name="firstname" type="xs:string"/>
<xs:element name="lastname" type="xs:string"/>
</xs:sequence>
<xs:anyAttribute/>
</xs:complexType>
</xs:element>
运行pyxb -u <filename> -m person
命令后,如何使用生成的绑定创建具有任意属性的 XML。
import person
p = person()
p.firstname = 'bob'
p.lastname = 'bobbington'
#I want behavior like this
p.middlename = 'bobby'
#I would settle for behavior like this:
p.add_attribute( 'middlename', 'bobby' )
#Heck*, I'd even settle for:
temp_attr = pyxb.AttributeUse( name, value, blah, blah, blay )
p._AttributeMap.update( temp_attr.name(), temp_attr )
但是无论我尝试什么,我都无法在调用时显示临时属性p.toxml()
这不支持吗?还是我忽略了什么?
编辑:脏话被删除。
编辑:做了一个解决方法(语法和变量名很遥远,但希望它足以帮助其他人弄清楚)
class PersonType():
'''
regular generated PersonType stuff here
'''
def add_attribute(self, name, value):
t = pyxb.blahblahblah._AttributeUse(
pyxb.blahblahblah._ExtendedName( None, attr_name),
attr_name,
pyxb.blahblah.StringDataType)
t.setValue(self, value) # or maybe _setValue
self._AttributeMap[t.name()] = t