我只是想知道是否可以将 XPath 表达式与 Camel simple
混合使用表达。
案子:
在我的 configure()
我的路线方法我有代码:
Namespaces ns = new Namespaces("sys1", "urn:com:example:namespace:system/1");
directFrom("from")
.setExchangePattern(ExchangePattern.InOut)
.to("{{request.endpoint}}")
.choice()
.when(header(STATUS_CODE).isEqualTo(200))
.choice()
.when(xpath("count(//sys1:UsageType[@code='0003'])>0").namespaces(ns))
.setHeader(STATUS_CODE,constant(404))
.setHeader(STATUS_MESSAGE,simple("Not found"))
.setBody(constant("Not found"))
.endChoice()
.otherwise()
.to("xslt:xslt/response.xsl?transformerFactoryClass=net.sf.saxon.TransformerFactoryImpl")
.endChoice()
.end()
.endChoice()
.end();
我希望 XPath 表达式中的值是可配置的:在 xpath 表达式中是否可以有这样的东西:
.when(xpath("count(//sys1:UsageType[@code='${properties:filter.value}'])>0").namespaces(ns)))
我使用与
simple
相同的语法表达。我发现的唯一方法是将(使用蓝图)整个 XPath(或只是值)注入(inject)变量中。
最佳答案
来自 Camel documentation :
// Following properties are given:
// foo=Camel
// bar=Kong
from("direct:in").choice()
// $type is a variable for the header with key type
// here we use the properties function to lookup foo from the properties files
// which at runtime will be evaluted to 'Camel'
.when().xpath("$type = function:properties('foo')")
.to("mock:camel")
// here we use the simple language to evaluate the expression
// which at runtime will be evaluated to 'Donkey Kong'
.when().xpath("//name = function:simple('Donkey ${properties:bar}')")
.to("mock:donkey")
.otherwise()
.to("mock:other")
.end();
因此,这样的事情应该有效:
.when(xpath("count(//sys1:UsageType[@code=function:simple('${properties:filter.value}')])>0").namespaces(ns))
关于xpath - 在 Camel 中混合 xpath 和简单表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25115230/