Menu

Generating XML - multiple "n" number of complex types in a loop

Help
2016-01-18
2016-01-20
  • Michael Issa

    Michael Issa - 2016-01-18

    I have a data structure where values of a certain complex type (let's call it myComplexType) are stored in a list. myComplexType is under another complex type (called complexType1) which is under the "root" i.e (RootName -> complexType1 -> myComplexType)

    I am trying to create a loop where the values that I gathered from reading a data file will fill out this complex type. However, this complex type has a minimum occurrence of 1 and a maximum occurrence of 4.

    The code I have goes something like this:

    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)
    

    How can I create "n" number of myComplexType where "n" is the length of the nested lists d,e,f?

    Further clarification:

    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]
    

    I need something like this:

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

    Can anyone show me how to easily create an "n" number of myComplexType (based on the length of the nested loops d, e, f) for each iteration/complexType1 in my loop?

    Please let me know if there is any confusion so that I may attempt to clarify further.

    Thanks in advance.

     
  • Peter A. Bigot

    Peter A. Bigot - 2016-01-18

    Schema:

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

    Build:

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

    Program:

    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();
    

    Output:

    <?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>
    
     
    • Michael Issa

      Michael Issa - 2016-01-20

      Thank you so much Peter.

       
  • Michael Issa

    Michael Issa - 2016-01-20

    Ignore this

     

    Last edit: Michael Issa 2016-01-21
  • Michael Issa

    Michael Issa - 2016-01-20

    I have one more problem I hope you can help me address.

    Let's say there are two more lists:

    I = [['ADD'], ['SUBTRACT','DIVIDE','MULTIPLY'], 'None', ['ADD'], 'None', ['DIVIDE']]
    J = [[12,2], [5,1,4,5,6], 'None', [8,1], 'None', [6,3]]

    The list I would correspond to "mathOperator" which is defined in the schema as follows:

    <xs:element name="mathOperator" type="mathOperatorType" minOccurs="0" maxOccurs="10">
                    <xs:annotation>
                        <xs:documentation>Enumerated +, -, /, *</xs:documentation>
                    </xs:annotation>
                </xs:element>
    <xs:simpleType name="mathOperatorType">
            <xs:restriction base="xs:string">
                <xs:enumeration value="ADD"/>
                <xs:enumeration value="SUBTRACT"/>
                <xs:enumeration value="MULTIPLY"/>
                <xs:enumeration value="DIVIDE"/>
            </xs:restriction>
    </xs:simpleType>
    

    And the list J would correspond to "mathOperand" which is defined as follows:

    <xs:element name="mathOperand" minOccurs="0" maxOccurs="11">
                    <xs:annotation>
                        <xs:documentation>Value or register to be operated on. Could be numeric or alphanumeric. </xs:documentation>
                    </xs:annotation>
                    <xs:simpleType>
                        <xs:restriction base="xs:string">
                            <xs:minLength value="1"/>
                            <xs:maxLength value="32"/>
                        </xs:restriction>
                    </xs:simpleType>
    </xs:element>
    

    I want the XML to output something like this (example are the first two steps of the expected XML generation):

    <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>
        <mathOperatorType>
            <mathOperator>'ADD'</mathOperator>
        </mathOperatorType>
        <mathOperand> 12 </mathOperand>
        <mathOperand> 2 </mathOperand>
        <extraInfo> 'foo' </extraInfo>
    </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>
        <mathOperatorType>
            <mathOperator>'SUBTRACT'</mathOperator>
            <mathOperator>'DIVIDE'</mathOperator>
            <mathOperator>'MULTIPLY'</mathOperator>
        </mathOperatorType>
        <mathOperand> 5 </mathOperand>
        <mathOperand> 1 </mathOperand>
        <mathOperand> 4 </mathOperand>
        <mathOperand> 5 </mathOperand>
        <mathOperand> 6 </mathOperand>
        <extraInfo> 'bar' </extraInfo>
    </complexType1>
    </Root>
    

    (This is what I think the expected XML would look like based on what I understand from the schema)

    The math operators and math operands need to go in between the h and extraInfo elements of the XML.

    I tried creating a conditional statement in my loop that goes something like this:

    for a,b,c,d,e,f,g,h,i,j,extra in zip(A,B,C,D,E,F,G,H,I,J,EXTRA):
        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);
    
        if i != 'None':
            for mathOptr in i:
                ct1.append((str(mathOptr)))
            for mathOpnd in j:
                ct1.append(str(mathOpnd))
        else:
            continue
        root.complexType1.append(ct1)
    

    Could you please help me understand what I'm doing wrong?

    Thanks in advance.

     

    Last edit: Michael Issa 2016-01-21

Log in to post a comment.