XPath to select Element by attribute value
As a follow on, you could select "all nodes with a particular attribute" like this:
//*[@id='4']
You need to remove the /
before the [
. Predicates (the parts in [..]
) shouldn't have slashes immediately before them - they go directly after the node selector they are associated with.
Also, to select the Employee element itself, you should leave off the /text()
at the end. Otherwise you'd just be selecting the whitespace text values immediately under the Employee element.
//Employee[@id = '4']
One more thing to note: //
can be very slow because it searches the entire document for matching nodes. If the structure of the documents you're working with is going to be consistent, you are probably best off using a more explicit path, for example:
/Employees/Employee[@id = '4']