1

给定架构:

<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
4

1 回答 1

2

如果复杂类型支持通配符属性,则该类型的实例有一个方法wildcardAttributeMap(),它是从扩展名称到表示属性值的 unicode 字符串的映射。根据这张票,目前没有公共 API 来操作此地图,尽管您可能可以手动操作。

根据查看您的问题,我添加了这张票,确认即使您将通配符属性放入地图,它们也不会显示在生成的 XML 中。

对于那个很抱歉。看起来是时候进行另一轮 PyXB 错误修复了。

于 2014-06-06T01:33:06.510 回答