[xml] XPath to select Element by attribute value

I have following XML.

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

I want to select Employee element with id="4".

I am using below XPath expression which is not returning anything.

//Employee/[@id='4']/text()

I checked it at http://chris.photobooks.com/xml/default.htm and it says invalid xpath, not sure where is the issue.

This question is related to xml xpath

The answer is


As a follow on, you could select "all nodes with a particular attribute" like this:

//*[@id='4']

Try doing this :

/Employees/Employee[@id=4]/*/text()