Ok. Using pyxb.BIND() that way makes sense. However, i followed your second
suggestion for defining complexTypes at the top and then referencing them
and it was working until the point i wanted to lump them all together into
an xml.
*snippet:*
import organization
import pyxb
pyxb.RequireValidWhenGenerating(False)
org = organization.Organization()
empl = organization.EmployeeType()
addr = organization.AddressType()
org.Name = 'company1'
empl.Name = 'name1'
addr.Street = 'address1'
addr.Town = 'town1'
addr.County = 'county1'
addr.PostCode = '54545'
addr.Country = 'US'
print (org.toxml('utf-8', element_name='Organization'))
*error:*
Traceback (most recent call last):
File "testorganization.py", line 24, in <module>
print (org.toxml('utf-8', element_name='Organization'))
File "/Library/Python/2.7/site-packages/pyxb/binding/basis.py", line 541,
in toxml
dom = self.toDOM(bds, element_name=element_name)
File "/Library/Python/2.7/site-packages/pyxb/binding/basis.py", line 513,
in toDOM
self._toDOM_csc(bds, element)
File "/Library/Python/2.7/site-packages/pyxb/binding/basis.py", line
2661, in _toDOM_csc
content.elementDeclaration.toDOM(dom_support, parent, content.value)
File "/Library/Python/2.7/site-packages/pyxb/binding/content.py", line
1106, in toDOM
raise pyxb.LogicError('toDOM with unrecognized value type %s: %s' %
(type(value), value))
pyxb.exceptions_.LogicError: toDOM with unrecognized value type <class
'pyxb.binding.content._PluralBinding'>: []
I do see the objects for complexTypes getting created if i just use "print
org or print empl or print addr", but get the above error when converting
it to xml.
Tried using append, but couldn't really get it to work.
On Fri, Apr 3, 2015 at 3:10 PM, Peter Bigot <big...@acm.org> wrote:
> The new Employee attribute is a sequence. You should append to it or
> assign a sequence to it, not assign a single value.
>
> org.Employee.append(pyxb.BIND())
> org.Employee = [ pyxb.BIND() ]
>
> If you control the schema, which it seems you do, you'll also find it a
> lot easier to normalize it: define an Employee element at the top level
> just as you do for Organization, and reference that within your complex
> types instead of defining the elements internal to the types. Then you can
> create instances without having to use pyxb.BIND.
>
> Peter
>
> On Fri, Apr 3, 2015 at 1:51 PM, Ajinkya Fotedar <ajinkyafote...@gmail.com>
> wrote:
>
>> Thank you Peter for the prompt reply. Made changes to the schema, but now
>> i get the following error on using pyxb.BIND()
>>
>> *Schema:*
>> <xs:schema elementFormDefault="qualified" xmlns:xs="
>> http://www.w3.org/2001/XMLSchema">
>>
>> <xs:element name="Organization" type="OrganizationType"/>
>>
>> <xs:complexType name="OrganizationType">
>> <xs:sequence>
>> <xs:element name="Name" type="xs:string"/>
>> <xs:element name="Employee" type="EmployeeType" minOccurs="1"
>> maxOccurs="unbounded"/>
>> </xs:sequence>
>> </xs:complexType>
>>
>> <xs:complexType name="EmployeeType">
>> <xs:sequence>
>> <xs:element name="Name" type="xs:string"/>
>> <xs:element name="Address" type="AddressType" minOccurs="1"
>> maxOccurs="unbounded"/>
>> </xs:sequence>
>> </xs:complexType>
>>
>> <xs:complexType name="AddressType">
>> <xs:sequence>
>> <xs:element name="Street" type="xs:string"/>
>> <xs:element name="Town" type="xs:string"/>
>> <xs:element name="County" type="xs:string"/>
>> <xs:element name="PostCode" type="xs:integer”/>
>> <xs:element name="Country" type="xs:string"/>
>> </xs:sequence>
>> </xs:complexType>
>>
>> </xs:schema>
>>
>>
>> *Creating XML:*
>> import organization
>> import pyxb
>>
>> pyxb.RequireValidWhenGenerating(False)
>>
>> org = organization.Organization()
>> org.Name = 'company1'
>> org.Employee = pyxb.BIND()
>> print org.toxml()
>>
>>
>> *Error:*
>> Traceback (most recent call last):
>> File "testorganization.py", line 12, in <module>
>> org.Employee = pyxb.BIND()
>> File "/Library/Python/2.7/site-packages/pyxb/binding/basis.py", line
>> 98, in __setattr__
>> return super(_TypeBinding_mixin, self).__setattr__(name, value)
>> File "/Library/Python/2.7/site-packages/pyxb/binding/content.py", line
>> 1043, in set
>> value = self.__elementBinding.compatibleValue(value,
>> is_plural=self.isPlural())
>> File "/Library/Python/2.7/site-packages/pyxb/binding/basis.py", line
>> 1618, in compatibleValue
>> raise pyxb.SimplePluralValueError(self.typeDefinition(), value)
>> pyxb.exceptions_.SimplePluralValueError: Type EmployeeType cannot be
>> created from: <pyxb.BIND object at 0x10fd8ec50>
>>
>>
>> Following pyxb's documentation, BIND was woking before i made the change,
>> for dealing with complexTypes, or am i missing something fundamental here.
>>
>> Thank you.
>>
>>
>> On Fri, Apr 3, 2015 at 10:50 AM, Peter Bigot <big...@acm.org> wrote:
>>
>>> This isn't a PyXB issue. Your schema extract only allows for a single
>>> Employee element with a single Address element. You're going to need to
>>> use maxOccurs="unbounded" or some similar directive in the schema
>>> definition.
>>>
>>> Peter
>>>
>>> On Fri, Apr 3, 2015 at 9:14 AM, Ajinkya Fotedar <
>>> ajinkyafote...@gmail.com> wrote:
>>>
>>>> I'd like to have multiple instances of complexTypes in an XML created
>>>> from the bindings. Not sure if/how this can be done using BIND and/or
>>>> append.
>>>> Will try and explain with an example i'm playing with.
>>>>
>>>> *XML Schema…*
>>>> <xs:schema elementFormDefault="qualified" xmlns:xs="
>>>> http://www.w3.org/2001/XMLSchema">
>>>>
>>>> <xs:element name="Organization" type="OrganizationType"/>
>>>>
>>>> <xs:complexType name="OrganizationType">
>>>> <xs:sequence>
>>>> <xs:element name="Name" type="xs:string"/>
>>>> <xs:element name="Employee" type="EmployeeType"/>
>>>> </xs:sequence>
>>>> </xs:complexType>
>>>>
>>>> <xs:complexType name="EmployeeType">
>>>> <xs:sequence>
>>>> <xs:element name="Name" type="xs:string"/>
>>>> <xs:element name="Address" type="AddressType"/>
>>>> </xs:sequence>
>>>> </xs:complexType>
>>>>
>>>> <xs:complexType name="AddressType">
>>>> <xs:sequence>
>>>> <xs:element name="Street" type="xs:string"/>
>>>> <xs:element name="Town" type="xs:string"/>
>>>> <xs:element name="County" type="xs:string"/>
>>>> <xs:element name="PostCode" type="xs:integer”/>
>>>> <xs:element name="Country" type="xs:string"/>
>>>> </xs:sequence>
>>>> </xs:complexType>
>>>>
>>>> </xs:schema>
>>>>
>>>>
>>>> *Used the above schema for generating bindings…*
>>>> pyxbgen -u organization.xsd -m organization
>>>>
>>>>
>>>> *Used the ‘organization’ module for generating an xml...*
>>>> import organization
>>>> import pyxb
>>>>
>>>> org = organization.Organization()
>>>> org.Employee = pyxb.BIND()
>>>> org.Employee.Address = pyxb.BIND()
>>>> org.Name = 'company1’
>>>> org.Employee.Name = 'name1’
>>>> org.Employee.Address.Street = 'street1’
>>>> org.Employee.Address.Town = 'town1’
>>>> org.Employee.Address.County = 'county1’
>>>> org.Employee.Address.PostCode = 54545
>>>> org.Employee.Address.Country = 'US'
>>>> print (org.toxml('utf-8', element_name='Organization'))
>>>>
>>>>
>>>> *Output…*
>>>> <Organization>
>>>> <Name>company1</Name>
>>>> <Employee>
>>>> <Name>name1</Name>
>>>> <Address>
>>>> <Street>street1</Street>
>>>> <Town>town1</Town>
>>>> <County>county1</County>
>>>> <PostCode>54545</PostCode>
>>>> <Country>US</Country>
>>>> </Address>
>>>> </Employee>
>>>> </Organization>
>>>>
>>>>
>>>> *What i'd like the o/p to be…*
>>>> <Organization>
>>>> <Name>company1</Name>
>>>> <Employee>
>>>> <Name>name1</Name>
>>>> <Address>
>>>> <Street>street1</Street>
>>>> <Town>town1</Town>
>>>> <County>county1</County>
>>>> <PostCode>54545</PostCode>
>>>> <Country>US</Country>
>>>> </Address>
>>>> <Address>
>>>> <Street>street2</Street>
>>>> <Town>town2</Town>
>>>> <County>county2</County>
>>>> <PostCode>77777</PostCode>
>>>> <Country>US</Country>
>>>> </Address>
>>>> </Employee>
>>>> </Organization>
>>>>
>>>> *OR…*
>>>> <Organization>
>>>> <Name>company1</Name>
>>>> <Employee>
>>>> <Name>name1</Name>
>>>> <Address>
>>>> <Street>street1</Street>
>>>> <Town>town1</Town>
>>>> <County>county1</County>
>>>> <PostCode>54545</PostCode>
>>>> <Country>US</Country>
>>>> </Address>
>>>> </Employee>
>>>> <Employee>
>>>> <Name>name2</Name>
>>>> <Address>
>>>> <Street>street2</Street>
>>>> <Town>town2</Town>
>>>> <County>county2</County>
>>>> <PostCode>45454</PostCode>
>>>> <Country>UK</Country>
>>>> </Address>
>>>> </Employee>
>>>> </Organization>
>>>>
>>>> Would really appreciate if any one here could point me to the right
>>>> direction. I believe BIND and append/extend will do the job. I have tried
>>>> them from this link
>>>> http://sourceforge.net/p/pyxb/discussion/956708/thread/c3da791a/#922f
>>>> but nothing has worked out so far.
>>>>
>>>> Thank you.
>>>>
>>>>
>>>> ------------------------------------------------------------------------------
>>>> Dive into the World of Parallel Programming The Go Parallel Website,
>>>> sponsored
>>>> by Intel and developed in partnership with Slashdot Media, is your hub
>>>> for all
>>>> things parallel software development, from weekly thought leadership
>>>> blogs to
>>>> news, videos, case studies, tutorials and more. Take a look and join the
>>>> conversation now. http://goparallel.sourceforge.net/
>>>> _______________________________________________
>>>> pyxb-users mailing list
>>>> pyxb-users@lists.sourceforge.net
>>>> https://lists.sourceforge.net/lists/listinfo/pyxb-users
>>>>
>>>>
>>>
>>
>
------------------------------------------------------------------------------
Dive into the World of Parallel Programming The Go Parallel Website, sponsored
by Intel and developed in partnership with Slashdot Media, is your hub for all
things parallel software development, from weekly thought leadership blogs to
news, videos, case studies, tutorials and more. Take a look and join the
conversation now. http://goparallel.sourceforge.net/
_______________________________________________
pyxb-users mailing list
pyxb-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/pyxb-users