Home > Blockchain >  How to print line for specific conditions using lxml in a long XML
How to print line for specific conditions using lxml in a long XML

Time:11-11

XML example as follows:

<File>
    <Sub_Function_1>
        <Messages>
            <Setting>
                <Data>
                    <Label>Setting_2</Label>
                    <Value>
                        <Measure>
                            <Data>Area_2</Data>
                            <Bound>
                                <Value>
                                    <Speed>2000</Speed>
                                </Value>
                                <Condition>
                                    <Data>0</Data>
                                </Condition>
                            </Bound>
                            <Bound>
                                <Value>
                                    <Distance>10000</Distance>
                                </Value>
                                <Condition>
                                    <Data>12000</Data>
                                </Condition>
                            </Bound>
                        </Measure>
                    </Value>
                </Data>
                <Data>
                    <Label>Setting_3</Label>
                    <Value>
                        <Default_value_if_undefined>
                            <Value>
                                <Value_on_condition>
                                    <Case>
                                        <Value>
                                            <Measure>
                                                <Speed>25</Speed>
                                                <Bound>
                                                    <Value>
                                                        <Time>2000</Time>
                                                    </Value>
                                                    <Condition>
                                                        <Speed>0</Speed>
                                                    </Condition>
                                                </Bound>
                                                <Bound>
                                                    <Value>
                                                        <Distance>10000</Distance>
                                                    </Value>
                                                    <Condition>
                                                        <Speed>12000</Speed>
                                                    </Condition>
                                                </Bound>
                                            </Measure>
                                        </Value>
                                        <Condition>
                                            <Data>Area_0</Data>
                                        </Condition>
                                    </Case>
                                </Value_on_condition>
                            </Value>
                            <Default_value>
                                <Integer>2000</Integer>
                            </Default_value>
                        </Default_value_if_undefined>
                    </Value>
                </Data>
            </Setting>
        </Messages>
    </Sub_Function_1>
</File>

Code is as follows:

for k in doc2.xpath('//Measure//Data[following-sibling::Bound]'):
    val1 = k.xpath('following-sibling::Bound[1]/Value/*')[0]
    val2 = k.xpath('following-sibling::Bound[2]/Value/*')[0]
    if val1.tag !=val2.tag:
        print("val:",val1.tag,"--",val1.sourceline,val2.tag,"--",val2.sourceline)

for k in doc2.xpath('//File[.//Measure//Data[following-sibling::Bound]]'):
    val1 = k.xpath('.//Measure//Data[following-sibling::Bound[1]/Value/*[1]]')
    val2 = k.xpath('.//Measure//Data[following-sibling::Bound[2]/Value/*[1]]')
    if val1 != val2:
        print("val:",val1, "--", val1.sourceline, val2, "--", val2.sourceline)

Output i am getting :

val: Speed -- 13 Distance -- 21

Output i am expecting:

val: Speed -- 13 Distance -- 21
val: Time -- 42 Distance -- 50

I was printing lines when both or has different attribute as shown in snapshot

for some reason it's not printing for all element such as if its inside "<Value_on_condition>" in 'Setting_3' in xml So if its a long xml its not able to find measure and compare both Val1 and Val2

enter image description here

CodePudding user response:

The structure of the xml in this question is different from the one in your other question. To reflect that, change your for loop to this:

for k in doc2.xpath('//Value//Measure'):
    val1 = k.xpath('.//Bound[1]//Value/*')[0]
    val2 = k.xpath('.//Bound[2]//Value/*')[0]
    if val1.tag !=val2.tag:
        print(val1.tag,"--",val1.sourceline,val2.tag,"--",val2.sourceline)
   
    cond1 = k.xpath('.//Bound[1]//Condition/*')[0]
    cond2 = k.xpath('.//Bound[2]//Condition/*')[0]
    if cond1.tag !=cond2.tag:
        print(cond1.tag,"--",cond1.sourceline,val2.tag,"--",cond2.sourceline)

The output I get is

Speed -- 12 Distance -- 20
Time -- 41 Distance -- 49
  • Related