annndrey
annndrey

Reputation: 1788

How to find a tag with some value in python and lxml

I have an xml file with the following structure:

<main_tag>
   <first>
     <tag1>val1</tag1>
     <conf>
       <tag2>val2</tag2>
       <tag3>val3</tag3>
       <tag4>val4</tag4>
     </conf>
   </first>
   <second>
     <tag1>val2</tag1>
     <conf>
       <tag2>val6</tag2>
       <tag3>val7</tag3>
       <tag4>val8</tag4>
     </conf>
   </second>
</main_tag>

I have to change the value of tag2. Possible values are stored in a dict:

{tag2values:[newvalue1, newvalue2]}

If value of tag1 is val1, then we change tag2 value to newvalue1. If tag1 value is val2, then we change tag2 value to newvalue2.

So the question is: is there a way to find an element in lxml matching its parent's value? Or to find an element by it's parent neighbour value?

Upvotes: 2

Views: 1444

Answers (1)

Martijn Pieters
Martijn Pieters

Reputation: 1121148

The .xpath method let's you find tags by XPath 1.0 expressions:

>>> from lxml import etree
>>> from cStringIO import StringIO
>>> tag2values = ['newvalue1', 'newvalue2']
>>> example = StringIO("""\
... <main_tag>
...    <first>
...      <tag1>val1</tag1>
...      <conf>
...        <tag2>val2</tag2>
...        <tag3>val3</tag3>
...        <tag4>val4</tag4>
...      </conf>
...    </first>
...    <second>
...      <tag1>val2</tag1>
...      <conf>
...        <tag2>val6</tag2>
...        <tag3>val7</tag3>
...        <tag4>val8</tag4>
...      </conf>
...    </second>
... </main_tag>
... """)
>>> tree = etree.parse(example)
>>> value1selector = '*/conf/tag2[../../tag1/text() = "val1"]'
>>> value2selector = '*/conf/tag2[../../tag1/text() = "val2"]'
>>> for elem in tree.xpath(value1selector):
...     elem.text = tag2values[0]
... 
>>> for elem in tree.xpath(value2selector):
...     elem.text = tag2values[1]
... 
>>> print(etree.tostring(tree, pretty_print=True))
<main_tag>
   <first>
     <tag1>val1</tag1>
     <conf>
       <tag2>newvalue1</tag2>
       <tag3>val3</tag3>
       <tag4>val4</tag4>
     </conf>
   </first>
   <second>
     <tag1>val2</tag1>
     <conf>
       <tag2>newvalue2</tag2>
       <tag3>val7</tag3>
       <tag4>val8</tag4>
     </conf>
   </second>
</main_tag>

In the above example, the XPath expression in value1selector gives you all tag2 elements that are children of conf, with a sibling tag1 tag with text val1, as ElementTree Element instances, thus making it trivial to replace their text content.

Upvotes: 5

Related Questions