0

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

4

1 回答 1

1

那是因为您的“有效 xpath 查询”无效。concat()是一个接受参数的函数,这就是为什么你的第一个 xpath:concat('PREFIX:',/returns/one/node)被正确使用并工作的原因。

当您像在此处那样将其放置在 xpath 的末尾时:/returns/many/nodes/concat('PREFIX:',text())它正在尝试查找名为“concat”的节点类型,该节点类型不存在,因此您收到了错误。

expr.evaluate()将评估您concat('PREFIX:',/returns/one/node)作为参数,但由于它期望返回类型为节点列表,并concat()返回一个字符串类型,这也不起作用,因此您有几个选择:

你必须给你的 xpath /returns/many/nodesevaluate()然后String[]用“PREFIX:”设置你的索引 0,然后再用你的 XPath 结果填充它。

或者

您可以评估concat('PREFIX:',/returns/one/node)xpath 并将预期类型更改为字符串,但是您需要处理一个长字符串。

于 2013-03-06T18:37:00.937 回答