我有一个xsd架构
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="Data_Requests">
<xs:complexType>
<xs:sequence>
<xs:element name="Admin">
<xs:complexType>
<xs:sequence>
<xs:element type="xs:string" name="SystemId"/>
<xs:element type="xs:string" name="OSName"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="SentRequest">
<xs:complexType>
<xs:sequence>
<xs:element name="database">
<xs:complexType>
<xs:sequence>
<xs:element name="datasetRequest">
<xs:complexType>
<xs:attribute type="xs:string" name="name1111"/>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
根据xsd架构使用PyXB我创建了类:
python pyxbgen -u schema3.xsd -m schema3
我编写了python脚本,但我不知道属性name111
的haw设置值def create_general_xml():
rep = Data_Requests()
rep.Admin = pyxb.BIND('system name', 'Win32NT')
rep.SentRequest = ???
如何设置属性'name111'的值?
答案 0 :(得分:0)
由于您的xsd架构有多个图层,因此您需要逐个对它们进行充气。
所以你的代码应该是这样的:
rep = Data_Requests()
rep.Admin = pyxb.BIND(SystemId = 'system name', OSName = 'Win32NT')
rep.SentRequest = pyxb.BIND()
rep.SentRequest.database = pyxb.BIND()
rep.SentRequest.database.datasetRequest = pyxb.BIND(name1111 = "example name")
&#13;
现在您可以使用以下方法测试:
rep.toxml()
这应该根据您的xsd架构生成以下XML:
<?xml version="1.0" ?>
<Data_Requests>
<Admin>
<SystemId>system name</SystemId>
<OSName>Win32NT</OSName>
</Admin>
<SentRequest>
<database>
<datasetRequest name1111="example name"/>
</database>
</SentRequest>
</Data_Requests>'
&#13;