Home > Mobile >  How to check format of specific element given in one XML to another XML using python
How to check format of specific element given in one XML to another XML using python

Time:10-25

My XML1 is as follows where I have put all the equal operator syntax:

<Operator>
    <Equal>
        <Data>Object</Data>
        <Data>1</Data>
    </Equal>
    <Equal>
        <Integer>Non_Object</Integer>
        <Integer>2</Integer>
    </Equal>
</Operator>

My XML2 is as follows where I have to check syntax of equal operator that is if any label having equal operator is having two Data or two Integer or(as shown XML1) then it will give the name of the label

XML2:

<File>
    <Sub_Function_1>
        <Messages>
            <Data>
                <Label>Alarm</Label>
                <Value>
                    <Equal>
                        <Data>Yes</Data>
                        <Integer>80</Integer>
                    </Equal>
                </Value>
            </Data>
        </Messages>
    </Sub_Function_1>
    <Sub_Function_2>
        <Services_1>
            <Data>
                <Label>Hotel</Label>
                <Value>
                    <Equal>
                        <Data>Yes</Data>
                        <Data>2</Data>
                    </Equal>
                </Value>
            </Data>
        </Services_1>
        <Services_2>
            <Data>
                <Label>Food</Label>
                <Value>
                    <Equal>
                        <Integer>Yes</Integer>
                        <Integer>2</Integer>
                    </Equal>
                </Value>
            </Data>
        </Services_2>
    </Sub_Function_2>
</File>

so in XML2 Label 'Hotel' and 'Food' matches the syntax of equal operator present in XML1

so how to print these two labels.

I started my code only to search all the equal operators first but I am not getting how to match the syntax of equal operator given XML1 in XML2

from lxml import etree

doc = etree.parse('C:/Syntax/Sample.xml')
doc2 = etree.parse('C:/Syntax/Project.xml')
for op in doc.findall('.//Equal'):
    for eq in doc2.findall('.//File'):
        match = eq.findall('.//Equal')
                for ch in match :
            ch = match.find('Data')
            ch = match.find('Data')

I just want to find the labels having the Syntax in XML1.

Always grateful for any kind of help.

CodePudding user response:

If I understand your question, and the structure of your xml files, correctly this should work:

#identify your values in the first file
values = [e.xpath('.//*[2]')[0].text for e in doc.xpath('.//Equal')]

#find corresponding values in the Sub_Function_2 element children in the 2nd file
for service in doc2.xpath('.//Sub_Function_2//Data[Label]'):
    value = service.xpath('.//Equal//*[2]/text()')[0]
    if value in values:
        #get the value in the corresponding Label
        print(service.xpath('.//Label/text()')[0])

Output, based on your sample xml, should be:

Hotel
Food
  • Related