Pyxb在循环中生成多个复杂类型

时间:2016-01-18 16:09:28

标签: python xml xsd complextype pyxb

我有一个数据结构,其中某个复杂类型的值(让我们称之为myComplexType)存储在列表中。 myComplexType属于另一种复杂类型(称为complexType1),位于" root"即(RootName -> complexType1 -> myComplexType

我正在尝试创建一个循环,其中我从读取数据文件中收集的值将填充此复杂类型。但是,这种复杂类型的最小出现次数为1,最大出现次数为4.

我的代码是这样的:

root = module.RootName():
for a,b,c,d,e,f,g,h in zip(A,B,C,D,E,F,G,H):
    for d1,e1,f1 in zip(d,e,f):
        root.complexType1_name.append(module.complexType1(a,b,c,(myComplexType(d1,e1,f1))),g,h)

我如何创建" n" myComplexType的数量,其中" n"是嵌套列表的长度d,e,f?

进一步澄清:

A = [1,2,3,4]
B = [Blue,Red,Orange,Yellow]
C = ['cat', 'dog', 'mouse', 'bird']
D = [[0,1],[0],[1,12,1,2],[2,0,4]]
E = [[Red,Blue],[Yellow],[Orange,Black,Red,White],[Blue,Green,Violet]]
F = [[True,False],[False],[False,True,False,False],[True,True,False]]
G = [0,0,1,0]
H = [1,0,1,1]

我需要这样的东西:

<Root>
<complexType1>
    <A_value> 1 </A_value>
    <B_value> Blue </B_value>
    <C_value> cat </C_value>
    <myComplexType>
        <D_value> 0 <D_value>
        <E_value> Red <E_value>
        <F_value> True <F_value>
    </myComplexType>
    <myComplexType>
        <D_value> 1 <D_value>
        <E_value> Blue <E_value>
        <F_value> False <F_value>
    </myComplexType>
    <G_value> 0 <G_value>
    <H_value> 1 <H_value>
</complexType1>
<complexType1>
    <A_value> 2 </A_value>
    <B_value> Red </B_value>
    <C_value> dog </C_value>
    <myComplexType>
        <D_value> 0 <D_value>
        <E_value> Yellow <E_value>
        <F_value> False <F_value>
    </myComplexType>
    <G_value> 0 <G_value>
    <H_value> 0 <H_value>
</complexType1>
<complexType1>
    <A_value> 3 </A_value>
    <B_value> Orange </B_value>
    <C_value> mouse </C_value>
    <myComplexType>
        <D_value> 1 <D_value>
        <E_value> Orange <E_value>
        <F_value> False <F_value>
    </myComplexType>
    <myComplexType>
        <D_value> 12 <D_value>
        <E_value> Black <E_value>
        <F_value> True <F_value>
    </myComplexType>
    <myComplexType>
        <D_value> 1 <D_value>
        <E_value> Red <E_value>
        <F_value> False <F_value>
    </myComplexType>
    <myComplexType>
        <D_value> 2 <D_value>
        <E_value> White <E_value>
        <F_value> False <F_value>
    </myComplexType>
    <G_value> 1 <G_value>
    <H_value> 0 <H_value>
</complexType1>
<complexType1>
    <A_value> 4 </A_value>
    <B_value> Yellow </B_value>
    <C_value> bird </C_value>
    <myComplexType>
        <D_value> 2 <D_value>
        <E_value> Blue <E_value>
        <F_value> True <F_value>
    </myComplexType>
    <myComplexType>
        <D_value> 0 <D_value>
        <E_value> Green <E_value>
        <F_value> True <F_value>
    </myComplexType>
    <myComplexType>
        <D_value> 4 <D_value>
        <E_value> Violet <E_value>
        <F_value> False <F_value>
    </myComplexType>
    <G_value> 0 <G_value>
    <H_value> 1 <H_value>

任何人都可以告诉我如何轻松创建一个&#34; n&#34;我的循环中每次迭代/ myComplexType的{​​{1}}个数(基于嵌套循环的长度d,e,f)?

如果有任何混淆,请告诉我,以便我可以尝试进一步澄清。

提前致谢。

1 个答案:

答案 0 :(得分:2)

请不要同时在多个论坛上提出相同的问题。这是我在help forum上提供的内容。

架构:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:complexType name="ct1_t">
    <xs:sequence>
      <xs:element name="A_value" type="xs:integer"/>
      <xs:element name="B_value" type="xs:string"/>
      <xs:element name="C_value" type="xs:string"/>
      <xs:element ref="myComplexType" minOccurs="1" maxOccurs="4"/>
      <xs:element name="G_value" type="xs:integer"/>
      <xs:element name="H_value" type="xs:integer"/>
    </xs:sequence>
  </xs:complexType>
  <xs:element name="myComplexType" type="mct_t"/>
  <xs:complexType name="mct_t">
    <xs:sequence>
      <xs:element name="D_value" type="xs:integer"/>
      <xs:element name="E_value" type="xs:string"/>
      <xs:element name="F_value" type="xs:boolean"/>
    </xs:sequence>
  </xs:complexType>
  <xs:element name="complexType1" type="ct1_t"/>
  <xs:element name="Root">
    <xs:complexType>
      <xs:sequence>
        <xs:element ref="complexType1" maxOccurs="unbounded"/>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

构建

llc[32]$ pyxbgen -u /tmp/x.xsd -m mmod
Python for AbsentNamespace0 requires 1 modules

程序:

import mmod

Blue = 'Blue'
Red = 'Red'
Orange = 'Orange'
Yellow = 'Yellow'
Black = 'Black'
White = 'White'
Green = 'Green'
Violet = 'Violet'
A = [1,2,3,4]
B = [Blue,Red,Orange,Yellow]
C = ['cat', 'dog', 'mouse', 'bird']
D = [[0,1],[0],[1,12,1,2],[2,0,4]]
E = [[Red,Blue],[Yellow],[Orange,Black,Red,White],[Blue,Green,Violet]]
F = [[True,False],[False],[False,True,False,False],[True,True,False]]
G = [0,0,1,0]
H = [1,0,1,1]

root = mmod.Root()
for a,b,c,d,e,f,g,h in zip(A,B,C,D,E,F,G,H):
    ct1 = mmod.complexType1(a, b, c)
    for d1,e1,f1 in zip(d,e,f):
        ct1.append(mmod.myComplexType(d1, e1, f1))
    ct1.append(g);
    ct1.append(h);
    root.complexType1.append(ct1)
print root.toDOM().toprettyxml();

输出:

<?xml version="1.0" ?>
<Root>
    <complexType1>
        <A_value>1</A_value>
        <B_value>Blue</B_value>
        <C_value>cat</C_value>
        <myComplexType>
            <D_value>0</D_value>
            <E_value>Red</E_value>
            <F_value>true</F_value>
        </myComplexType>
        <myComplexType>
            <D_value>1</D_value>
            <E_value>Blue</E_value>
            <F_value>false</F_value>
        </myComplexType>
        <G_value>0</G_value>
        <H_value>1</H_value>
    </complexType1>
    <complexType1>
        <A_value>2</A_value>
        <B_value>Red</B_value>
        <C_value>dog</C_value>
        <myComplexType>
            <D_value>0</D_value>
            <E_value>Yellow</E_value>
            <F_value>false</F_value>
        </myComplexType>
        <G_value>0</G_value>
        <H_value>0</H_value>
    </complexType1>
    <complexType1>
        <A_value>3</A_value>
        <B_value>Orange</B_value>
        <C_value>mouse</C_value>
        <myComplexType>
            <D_value>1</D_value>
            <E_value>Orange</E_value>
            <F_value>false</F_value>
        </myComplexType>
        <myComplexType>
            <D_value>12</D_value>
            <E_value>Black</E_value>
            <F_value>true</F_value>
        </myComplexType>
        <myComplexType>
            <D_value>1</D_value>
            <E_value>Red</E_value>
            <F_value>false</F_value>
        </myComplexType>
        <myComplexType>
            <D_value>2</D_value>
            <E_value>White</E_value>
            <F_value>false</F_value>
        </myComplexType>
        <G_value>1</G_value>
        <H_value>1</H_value>
    </complexType1>
    <complexType1>
        <A_value>4</A_value>
        <B_value>Yellow</B_value>
        <C_value>bird</C_value>
        <myComplexType>
            <D_value>2</D_value>
            <E_value>Blue</E_value>
            <F_value>true</F_value>
        </myComplexType>
        <myComplexType>
            <D_value>0</D_value>
            <E_value>Green</E_value>
            <F_value>true</F_value>
        </myComplexType>
        <myComplexType>
            <D_value>4</D_value>
            <E_value>Violet</E_value>
            <F_value>false</F_value>
        </myComplexType>
        <G_value>0</G_value>
        <H_value>1</H_value>
    </complexType1>
</Root>