给定架构:
<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
后命令,如何使用生成的绑定(bind)创建具有任意属性的 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
最佳答案
如果复杂类型支持通配符属性,则该类型的实例具有方法 wildcardAttributeMap()这是从扩展名称到表示属性值的 unicode 字符串的映射。每this ticket目前没有公共(public) API 来操作此 map ,但您可以手动执行此操作。
根据审查您的问题,我添加了 this ticket这证实了即使您将通配符属性放入映射中,它们也不会显示在生成的 XML 中。
对此感到抱歉。看来是时候进行另一轮 PyXB 错误修复了。
关于python - PyXB 将 anyAttribute 属性添加到生成的 Python 类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24049912/