python - PyXB 将 anyAttribute 属性添加到生成的 Python 类

标签 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: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/

相关文章:

c# - 反射使HashCode不稳定

c# - .NET,C# : How to add a custom serialization attribute that acts as ISerializable interface

java - 将浮点时间戳从 Python 转换为 Java 中的 Date

python - CPython 是否保证 dict.keys 与 dict.values 具有相同的顺序?

python - 在 Python 中解包字典时,单(非双)星号 * 是什么意思?

android - 更改文本开/关颜色以切换 Android

c# - Xml 反序列化 - 将两个元素合并为一个 List<T> 对象

asp.net-mvc-3 - MVC3 - 在哪里放置自定义属性类

python - 从 Python 使用 OpenCV TLD

xml - Wix - ICE60 和 ICE69 警告