Using this XML example:
0
1
<
The expression needed to refer to that second B element should look something like this:
/*/B[id='1']
Or, if the target node is at an unknown position in the document, use:
//B[id='1']
Full Java example (assuming the XML is in a file called workbook.xml):
DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = domFactory.newDocumentBuilder();
Document doc = builder.parse("workbook.xml");
XPath xpath = XPathFactory.newInstance().newXPath();
XPathExpression expr = xpath.compile("//B[id='1']");
NodeList nodes = (NodeList) expr.evaluate(doc, XPathConstants.NODESET);
for (int i = 0; i < nodes.getLength(); i++) {
System.out.println("[" + nodes.item(i) + "]");
}