JD.
JD.

Reputation: 2487

Prefix each member of an xpathexpression nodelist

I would like to construct an xpath query such that xpathexpression.evaulate returns a list of values prefixed with an arbitrary string.

Achieving this with a single result is a simple concat('PREFIX:',/returns/one/node), although the valid xpath query /returns/many/nodes/concat('PREFIX:',text()) is not accepted by xpathexpression.evaulate.

Here is my function call:

NodeList resultNodes = (NodeList) expr.evaluate(doc, XPathConstants.NODESET); String collectionIDs[] = new String[resultNodes.getLength()];

This is the error I receive:

javax.xml.transform.TransformerException: Unknown nodetype: concat

Does anyone know of an evaluate-friendly alternative?

Thank you in advance

Upvotes: 0

Views: 810

Answers (1)

JWiley
JWiley

Reputation: 3219

That's because your "valid xpath query" isn't valid. concat() is a function which accepts parameters, which is why your first xpath: concat('PREFIX:',/returns/one/node) is being used correctly, and works.

When you place it at the end of your xpath like you did here: /returns/many/nodes/concat('PREFIX:',text()) it's attempting to find a node type called "concat," which doesn't exist, and thus the error you received.

expr.evaluate() will evaluate your concat('PREFIX:',/returns/one/node) as a parameter, but as it is expecting a return type of nodelist, and concat() returns a string type, that won't work either, so you have a couple choices:

You will have to give your xpath of /returns/many/nodes to evaluate(), and then set index 0 of your String[] with "PREFIX:" before filling it with your XPath results.

Or

You can evaluate the concat('PREFIX:',/returns/one/node) xpath and change the expected type to string, but then you have one long string to deal with.

Upvotes: 1

Related Questions