It's harder with your schema because the elements are defined within the
complex types, rather than globally where they can be referenced by their
element name (as they appear in the document)

If you want to create the inner elements independently, you can do so using
their type, as with inv.invoiceHeaderType() below.  Alternatively, you can
ask PyXB to pick an appropriate type based on the content model, as with the
assignment to ivh.account below.

If you want to create them dynamically as parameters to a constructor, you
can often just provide the value as a keyword parameter; other times you
might need to use the
pyxb.BIND<http://pyxb.sourceforge.net/userref_usebind.html#creating-instances-of-anonymous-types>feature.

I suggest building the document component-by-component, then putting it
together at the end.  Here's one approach (not complete, but it should give
the idea):

# -*- coding: utf-8 -*-
import pyxb
import data
import _inv as inv
import pyxb.binding.datatypes as xsd;

pyxb.utils.domutils.BindingDOMSupport.DeclareNamespace(data.Namespace,
'dat')
pyxb.utils.domutils.BindingDOMSupport.DeclareNamespace(inv.Namespace, 'inv')

dp = data.dataPack(id='008129', ico='63303396', application='app',
version='1.0', note='Import')

ivh = inv.invoiceHeaderType()
ivh.invoiceType = 'issuedInvoice'
ivh.date = xsd.date(2010, 6, 28)
ivh.accounting = '2Pslub'
ivh.account = pyxb.BIND(accountNo='381957162', bankCode='0300')

ivd = inv.invoiceDetailType(invoiceItem=[pyxb.BIND(text='Here goes the
text', quantity=12.0, homeCurrency=pyxb.BIND(149.00))])

iv = inv.invoice(version='1.1', invoiceHeader=ivh, invoiceDetail=ivd)

dpi = data.dataPackItemType(id=1, version='1.0', invoice=iv)

dp.dataPackItem.append(dpi)

pyxb.RequireValidWhenGenerating(False)
print dp.toxml()

Peter

2010/7/2 David Hrbáč <[email protected]>

> Hi,
> I'm trying to produce this XML:
>
> <?xml version="1.0" encoding="UTF-8"?>
> <dat:dataPack id="008129" ico="63303396" application="app" version = "1.0"
> note="Import" xmlns:dat="http://www.stormware.cz/schema/data.xsd";
> xmlns:inv="http://www.stormware.cz/schema/invoice.xsd";        xmlns:vch="
> http://www.stormware.cz/schema/voucher.xsd";        xmlns:int="
> http://www.stormware.cz/schema/intDoc.xsd";        xmlns:typ="
> http://www.stormware.cz/schema/type.xsd"; >
> <dat:dataPackItem id="1" version="1.0">
>                <inv:invoice version="1.1">
>                        <inv:invoiceHeader>
>
>  <inv:invoiceType>issuedInvoice</inv:invoiceType>
>                                <inv:date>2010-06-28</inv:date>
>                                <inv:accounting>
>                                        <typ:ids>2Pslužb</typ:ids>
>                                </inv:accounting>
>                                <inv:classificationVAT>
>
>  <typ:classificationVATType>inland</typ:classificationVATType>
>                                </inv:classificationVAT>
>                                <inv:text>Here goes the text</inv:text>
>                                <inv:partnerIdentity>
>                                        <typ:id>240</typ:id>
>                                </inv:partnerIdentity>
>                                <inv:paymentType>
>                                        <typ:ids>příkazem</typ:ids>
>                                </inv:paymentType>
>                                <inv:account>
>
>  <typ:accountNo>381957163</typ:accountNo>
>                                        <typ:bankCode>0300</typ:bankCode>
>                                </inv:account>
>                                <inv:symConst>0308</inv:symConst>
>                        </inv:invoiceHeader>
>                        <inv:invoiceDetail>
>                                <inv:invoiceItem>
>                                        <inv:text>Here goes the
> text</inv:text>
>                                        <inv:quantity>12</inv:quantity>
>                                        <inv:unit>měs</inv:unit>
>                                       <inv:rateVAT>high</inv:rateVAT>
>                                        <inv:homeCurrency>
>
>  <typ:unitPrice>149.000000</typ:unitPrice>
>                                        </inv:homeCurrency>
>                                </inv:invoiceItem>
>                        </inv:invoiceDetail>
>                        <inv:invoiceSummary>
>
>  <inv:roundingDocument>math2one</inv:roundingDocument>
>                        </inv:invoiceSummary>
>                </inv:invoice>
>        </dat:dataPackItem>
> </dat:dataPack>
>
>
> I have created the binding with:
> pyxbgen -u "http://www.stormware.cz/schema/data.xsd"; -m data
>
> Now with the following script I'm trying to produce XML:
>
> # -*- coding: utf-8 -*-
> import pyxb
> import data
> from _inv import invoice
> import time
> import pyxb.utils.domutils
>
> pyxb.utils.domutils.BindingDOMSupport.DeclareNamespace(data.Namespace,
> 'dat')
> pyxb.utils.domutils.BindingDOMSupport.DeclareNamespace(invoice.Namespace,
> 'inv')
>
> v=data.dataPack(ico="333",id="4567",application="app",version =
> "1.0",note="Import")
> v.dataPackItem.append(invoice())
> v.dataPackItem[0].invoice.version="1.0"
>
> pyxb.RequireValidWhenGenerating(False)
> print v.toxml()
>
> Could you please help me with inner elemets like invoice, invoiceheader,
> etc? I can't het any further.
>
> Thanks a lot,
> David Hrbac
>
>
> ------------------------------------------------------------------------------
> This SF.net email is sponsored by Sprint
> What will you do first with EVO, the first 4G phone?
> Visit sprint.com/first -- http://p.sf.net/sfu/sprint-com-first
> _______________________________________________
> pyxb-users mailing list
> [email protected]
> https://lists.sourceforge.net/lists/listinfo/pyxb-users
>
------------------------------------------------------------------------------
This SF.net email is sponsored by Sprint
What will you do first with EVO, the first 4G phone?
Visit sprint.com/first -- http://p.sf.net/sfu/sprint-com-first
_______________________________________________
pyxb-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/pyxb-users

Reply via email to