我如何在 Java 上用撒克逊语“for … order by”

How do I “for … order by” in Saxon on Java

这是 的后续。

我打电话:

XQueryCompiler compiler = processor.newXQueryCompiler();
// 21 of the following
xPath.declareNamespace(prefix, uri);
xPath.compile("for $n in '/def:System/def:Securities[def:AssetType != 50]' order by $n/'def:RiskLevel' return $n");
XQueryEvaluator selector = exe.load();
selector.setContextItem(xmlDocument);
// exception thrown on this:
selector.evaluate();

然后我得到: net.sf.saxon.s9api.SaxonApiException: '/'的第一个操作数要求的项类型是node();提供的值具有项目类型 xs:string

我猜 '/def:System/def:Securities[def:AssetType != 50]' 需要是一个节点 - 我该怎么做?而且,将其设为一个节点然后将其缩减为一个节点而不是返回与查询匹配的所有节点吗?

而且这个查询可以很复杂 "node/node[@attr = '5]/node/not[node = 'dave']/node/@atr"

更新: 查询 "for $n in /def:System/def:Securities[def:AssetType != 50] order by $n/'def:RiskLevel, def:SecurityDesc' return $n" 我得到同样的异常。

查询中的那些单引号是虚假的。您将 $n 绑定到字符串 '/def:System/def:Securities[def:AssetType != 50]',错误是说当 $n 是字符串时您不能使用 $n/xxx