I have this code, i want to group animals with the same tags to the one group ex. tags<dog><dog> to <dogs><dog/><dog/></dogs> etc. But in my code, I have no idea why is the output without animals.
OUTPUT:
<root>
<zoo>
<some_tag/><some_diff/>
</zoo>
<zoo>
<b/><o/>
</zoo>
</root>
CODE:
xml = '`<root>
<zoo>
<some_tag/><some_diff/>
<dog/><dog/>
<cat/><cat/><cat/>
</zoo>
<zoo>
<b/><o/>
<dog/><dog/>
<cat/><cat/><cat/><cat/>
</zoo>
</root>`'
from lxml import etree as et
root = et.fromstring(xml)
node = root.findall('./zoo')
j = False
k = False
for zoo in node:
for animal in zoo:
if 'dog' in animal.tag:
if not j:
dogs = et.SubElement(zoo,'dogs')
dogs.append(animal)
j = True
if 'cat' in animal.tag:
if not k:
cats = et.SubElement(zoo,'cats')
cats.append(animal)
k = True
k = False
j= False