xpath - 在 Camel 中混合 xpath 和简单表达式

标签 xpath apache-camel

我只是想知道是否可以将 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/

相关文章:

python - Scrapy - 发送新请求/使用回调

html - 通用 xpath 查询

java - 如何获取队列的连接错误?

java - 如何使用 Apache Camel 从 xml 中提取数据并将其保存到数据库

java - 使用正则表达式、Java 修改 XPath 字符串

.net - XPath 获取 ID 最高的元素

python - 在selenium中运行Xpath的text()

xpath - Camel Apache : xpath to extract some value out of received XML

java - Apache Camel : Problem POSTing request body

java - 如何使用 apache Camel 从目录中删除文件