Home > Enterprise >  How to print line number for specific condition in xml using python
How to print line number for specific condition in xml using python

Time:11-10

My xml is

<File>
    <Sub_Function_1>
        <Messages>
            <Setting>
                <Data>
                    <Label>Setting_1</Label>
                    <Value>
                        <Measure>
                            <Data>Area</Data>
                            <Bound>
                                <Value>
                                    <Data>2000</Data>
                                </Value>
                                <Condition>
                                    <Data>0</Data>
                                </Condition>
                            </Bound>
                            <Bound>
                                <Value>
                                    <Integer>10000</Integer>
                                </Value>
                                <Condition>
                                    <Integer>12000</Integer>
                                </Condition>
                            </Bound>
                        </Measure>
                    </Value>
                </Data>
                <Data>
                    <Label>Setting_2</Label>
                    <Value>
                        <Measure>
                            <Data>Area_2</Data>
                            <Bound>
                                <Value>
                                    <Integer>2000</Integer>
                                </Value>
                                <Condition>
                                    <Data>0</Data>
                                </Condition>
                            </Bound>
                            <Bound>
                                <Value>
                                    <Integer>10000</Integer>
                                </Value>
                                <Condition>
                                    <Data>12000</Data>
                                </Condition>
                            </Bound>
                        </Measure>
                    </Value>
                </Data>
                <Data>
                    <Label>Setting_3</Label>
                    <Value>
                        <Measure>
                            <Data>Area_3</Data>
                            <Bound>
                                <Value>
                                    <Speed>2000</Speed>
                                </Value>
                                <Condition>
                                    <Speed>0</Speed>
                                </Condition>
                            </Bound>
                            <Bound>
                                <Value>
                                    <Distance>10000</Distance>
                                </Value>
                                <Condition>
                                    <Speed>12000</Speed>
                                </Condition>
                            </Bound>
                        </Measure>
                    </Value>
                </Data>
            </Setting>
        </Messages>
    </Sub_Function_1>
</File>

Here I want to print line number if both Condition and Value of Bound have different elements. for ex here line 14(Data) and line 22(Integer) doesn't match , line 17(Data) and line 25(Integer) doesn't match , line 64(Speed) and line 72(Distance) doesn't match.

My code where I was trying to match elements of condition:

from lxml import etree
doc = etree.parse('C:/Python/Project.xml')
for eqs in doc.xpath('//File[.//Measure//*[2]/Value/*[1]]'):
 for vqs in doc.xpath('//File[.//Measure//*[3]/Value/*[1]]'):
  if eqs != vqs :
       for e in eqs:
        print("Measure", e.sourceline)

It's not printing any line no output lines expected:

line no. 12, 15, 60

So here value and conditions are not having same elements for which i want to print lines

enter image description here

Here Value and condition have same elements so I don't want to print lines enter image description here

Here Value don't have same element so i want to print line of value

enter image description here

CodePudding user response:

If I understand your conditions correctly, this should work:

for target in doc2.xpath('//Data//following-sibling::Bound[not(Value=Condition)]'):
    print(target.sourceline)

Output should be something like

10
18
34
42
58
66

EDIT:

Again, if I understand you correctly, this should get you at least close enough to where you want to get. Of course, you can modify it as you see fit:

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(val1.tag,"--",val1.sourceline,val2.tag,"--",val2.sourceline)
   
    cond1 = k.xpath('following-sibling::Bound[1]/Condition/*')[0]
    cond2 = k.xpath('following-sibling::Bound[2]/Condition/*')[0]
    if cond1.tag !=cond2.tag:
        print(cond1.tag,"--",cond1.sourceline,val2.tag,"--",cond2.sourceline)

Output in this case:

Data -- 12 Integer -- 20
Data -- 15 Integer -- 23
Speed -- 60 Distance -- 68
  • Related