XML
<root>
<p>nodea text 1</p>
<p>nodea text 2</p>
<nodea>
</nodea>
<p>nodeb text 1</p>
<p>nodeb text 2</p>
<nodeb>
</nodeb>
</root>
I want to get the first preceding sibling p tag of nodea or nodeb if there is one. For example for the above xml the preceding siblings for respective node are
nodea preceding siblings
<p>nodea text 1</p>
<p>nodea text 2</p>
nodeb preceding siblings
<p>nodeb text 1</p>
<p>nodeb text 2</p>
i have tried the below xpath but it gives me the preceding p tag of nodea instead of nodeb.
nodeb = xml.find('nodeb')
nodeb.xpath('preceding-sibling::p[not(preceding-sibling::nodea)][1]')
If there is no preceding p tag before the node then it should return empty list. For example for the below xml there are no preceding sibling p tags for nodeb.
<root>
<p>nodea text 1</p>
<nodea>
</nodea>
<nodeb>
</nodeb>
</root>
It would be nice if someone can also explain why my xpath is not working and what should i keep in mind when writing xpath?