문제

Given the schema:

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

and after running the pyxb -u <filename> -m person command, how does create XML with arbitrary attributes using the generated bindings.

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 )

But no matter what I try, I cannot get the temporary attributes to show up when calling p.toxml()

Is this not supported? or have I overlooked something?

Edit: profanity removed.

Edit: Made a work around (the syntax and variable names are way off, but hopefully it helps enough for others to figure it out)

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
도움이 되었습니까?

해결책

If a complex type supports wildcard attributes, instances of the type have a method wildcardAttributeMap() which is a map from extended names to unicode strings representing attribute values. Per this ticket there is currently no public API to manipulate this map, though you can probably do so manually.

Based on reviewing your question I've added this ticket which confirms that even if you do put wildcard attributes into the map, they won't show up in the generated XML.

Sorry about that. Looks like it's time to do another round of PyXB bug fixes.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top