I am traversing an XML Document and extracting various bits of information via XPath expressions (using Saxon 9 in Java). While going through the nodes under one element, I need to reference a node from another element. In the following example, for each /root/books/book I want to get a reference to the /root/authors/author/name that has the same id as the author element under book.
I'm thinking of something like /root/authors/author[id=XXX/author]/name where XXX refers to the "current node" that I am evaluating. It's the XXX bit that I can't figure out. Any help?
<root>
<authors>
<author>
<id>001</id>
<name>Bob</name>
</author>
<author>
<id>002</id>
<name>Jane</name>
</author>
</authors>
<books>
<book>
<name>My Book</name>
<author>001</author>
</book>
</books>
</root>
Here's the code snippet. First I'm getting a nodelist, then I'm looping through those nods and evaluating a set of expressions for each node.
NodeList nl = (NodeList) xpath.evaluate(loopExpr, doc, XPathConstants.NODESET);
for (int i = 0; i < nl.getLength(); i++) {
Node currentNode = nl.item(i);
for (String field : fields) {
XPathExpression xe = expr.get(field);
String value = xe.evaluate(currentNode);
imp.add(value);
}
}
let $book := . return /root/authors/author[id = $book/author]/name
. I am not sure how to enable that version using the Jaxp API. – Martin Honnen