Binyamin Even
Binyamin Even

Reputation: 3382

select ancestor node by attribute with Xpath in Python

My XML is conceptually in this structure:

<?xml version="1.0" encoding="UTF-8"?>
<Employees>
 <Employee>
    <age>40</age>
    <name lang="en">Tom</name>
    <gender>Male</gender>
    <role>Manager</role>
 </Employee>
 <Employee>
    <age>25</age>
    <name lang="fr">Meghna</name>
    <gender>Female</gender>
    <role>Manager</role>
 </Employee>
</Employees> 

now, what I want is to select all the employees who have name with lang="en", and write them to a new XML file.

So in this example I want just the first employee:

<Employee>
    <age>40</age>
    <name lang="en">Tom</name>
    <gender>Male</gender>
    <role>Manager</role>
</Employee>

I tried to do something like that

//Employee[//name[@lang='en']]

(I am deliberately using // before name, since name doesn't have to be a direct child of Employee)

I don't know how to write it to a file, and for some reason - my query doesn't return anything.

Upvotes: 1

Views: 300

Answers (1)

har07
har07

Reputation: 89285

According to the XML posted, you should be able to use the following XPath expression :

//Employee[name/@lang='en']

The XPath return Employee element where the corresponding child name has attribute lang equals "en". Or in case name may not be direct child of Employee, you can use the following expression instead :

//Employee[.//name/@lang='en']

Notice the . before // which indicate that the expression inside the predicate ([]) is relative to current Employee context.

Upvotes: 2

Related Questions