骆驼路线xpath过滤器

时间:2016-12-30 14:49:03

标签: java xpath apache-camel

我在骆驼路线上遇到了xpath过滤器,我找不到解决方案......

这是我的xml消息

<env:Envelope xmlns:env="http://www.w3.org/2003/05/soap-envelope"
xmlns:ct="http://com/fr/test">
<env:Header>
    <receptionTime xmlns="http://com/fr/test">2016-02-12T14:40:56+0100
    </receptionTime>
    <correlationId xmlns="http://com/fr/test">944689bc-9605-4292-929a-3b2e8a5eddd7
    </correlationId>
    <equipmentType xmlns="http://com/fr/test">WCA</equipmentType>
    <messageType xmlns="http://com/fr/test">Reservation</messageType>
</env:Header>
<env:Body>
<content xmlns="http://com/fr/test"></content>
</env:Body>

我的路线像这样:

 from("activemq-ext-in:queue:" + queueName)
    .id("amq-transfert-downward-route")
    .filter().xpath("/env:Envelope/env:Header/equipmentType/text()='WCA'")
    //.when(body().contains("WCA"))
    .to("activemq-ext-out:topic:" + topicName);

并且我的消息从未到达我的主题:(如果我取消注释when子句,并评论过滤器,我的消息是正确发送,但我想使用过滤器

有人可以帮我找到好的xpath表达式吗?

可能我可以使用.choice()。when(xpath ...)。to(...)?

我只需要一个正确的解决方案:)

非常感谢!

1 个答案:

答案 0 :(得分:1)

看起来像名称空间前缀问题。您正在使用“env:”限定Envelope,只要该前缀映射到正确的命名空间就很好。但是,您在equipmentTest上缺少合适的限定符,该限定符位于不同的名称空间中。

请参阅Camel docs有关如何配置命名空间的信息。

Namespaces ns = new Namespaces("ct", "http://com/fr/test")
    .add("env", "http://www.w3.org/2003/05/soap-envelope");

from("activemq-ext-in:queue:" + queueName)
   .id("amq-transfert-downward-route")
   .filter(
      ns.xpath("/env:Envelope/env:Header/ct:equipmentType/text()='WCA'")
   ).to("activemq-ext-out:topic:" + topicName);

更新:更改为使用OP示例而不是Camel文档中的示例。我没有对此进行过测试,但它看起来是正确的。