PyXB向生成的Python类添加anyAttribute属性
给定模式:PyXB向生成的Python类添加anyAttribute属性,python,xml,custom-attributes,pyxb,Python,Xml,Custom Attributes,Pyxb,给定模式: <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:anyAttr
<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>
但是无论我怎么做,调用p.toxml()
这是否不受支持?还是我忽略了什么
编辑:删除亵渎
编辑:做了一个变通(语法和变量名有点不对劲,但希望它能帮助其他人解决这个问题)
如果复杂类型支持通配符属性,则该类型的实例具有一个方法,该方法是从扩展名称到表示属性值的unicode字符串的映射。尽管您可能可以手动操作,但目前没有公共API来操作此映射 在回顾您的问题的基础上,我添加了一个问题,它确认了即使您将通配符属性放入映射中,它们也不会显示在生成的XML中
很抱歉。看来是时候进行另一轮PyXB bug修复了。没什么大不了的。除此之外,PyXB非常棒。不过我找到了一份工作。我无法将代码放在注释中,因此我将编辑原始问题。您应该可以使用
实例。\u setAttribute(name,value)
其中name
是一个扩展名(名称空间+本地名,或者只是一个没有名称空间的本地名)而value
是属性的unicode值:无需构造AttributeUse。这些问题源于您在上游github存储库中提出的问题。谢谢,这绝对更干净。感谢您继续致力于此库并使其变得更好!
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 )
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