0

I have the following xml record within a larger xml file:

<Employee>
<id>999</id>
<fname>Tim</fname>
<lname>Boskin</lname>
</Employee>

I am attempting to get the fname and lname attributes via lxml and xpath in python. The following statement is not returning anything:

fname = root.xpath('.//Employee[@id="999"]/fname')

Every example I have found and attempted has yielded no results, what would be the proper syntax?

1 Answer 1

1

@id selects value of the attribute named id.

And this is why it goes wrong. Try this:

fname = root.xpath('//Employee[id/text()="999"]/fname')

Because there is no attribute named id within the Employee element, instead it is a child element of the Employee element. For more details on XPath axes read this.

Sign up to request clarification or add additional context in comments.

1 Comment

Ah, appreciate it. I was misunderstanding the xpath syntax.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.