修剪 Camel 路由中使用的 xpath 表达式

发布于 2024-11-16 15:42:44 字数 1231 浏览 1 评论 0原文

这可能是骆驼问题或 xpath 问题:)。非camel人员请注意:camel使用javax.xml.xpath来解析他们的xml。

在骆驼路线中,我试图从 xml 主体中提取一个元素用作我的文件名:

Namespaces ns = getNamespaces();

String fileIDxPath = (see notes);

from("direct:testOutToFile")
    .setHeader(Exchange.FILE_NAME, ns.xpath(fileIDxPath))
    .to("file:/tmp/testing/generatedXML");

但我似乎无法获得一个 xpath 来为我提供节点的修剪值 。这是我尝试过的 xpath:

A) //rm:TradeMsg/rm:Submitter/rm:partyTradeIdentifier/fpml:tradeId/
B) //rm:TradeMsg/rm:Submitter/rm:partyTradeIdentifier/fpml:tradeId/text()
C) //rm:TradeMsg/rm:Submitter/rm:partyTradeIdentifier/fpml:tradeId/text()[normalize-space()]
D) normalize-space(//rm:TradeMsg/rm:Submitter/rm:partyTradeIdentifier/fpml:tradeId/text())
  • A) 返回整个节点,而不仅仅是值
  • B) 返回值,但包含大量空格
  • C) 还返回包含所有空格的值
  • D) 抛出 XPathExpressionException

问题对于 xpath 专家来说 - 我是否在 xpath 中遗漏了一些可以修剪此值的内容?

骆驼专家的问题 - 也许我可以在评估后修剪这个值?我知道我可以通过另一个步骤来传递它,但我认为这将是一个足够常见的用例,可以在 ns.xpath 调用本身中得到支持。

我还注意到 ns.xpath 将标头设置为“com.sun.org.apache.xml.internal.dtm.ref.DTMNodeList”的实例,而不是 String,这将是预期的类型。

谢谢大家,
罗伊

This might be either a camel or an xpath question :). Note to non-camel folks: camel uses javax.xml.xpath to resolve their xml.

In a camel route, I'm trying to pull an element out of the xml body to use as my filename:

Namespaces ns = getNamespaces();

String fileIDxPath = (see notes);

from("direct:testOutToFile")
    .setHeader(Exchange.FILE_NAME, ns.xpath(fileIDxPath))
    .to("file:/tmp/testing/generatedXML");

But I can't seem to get an xpath that gives me the trimmed value of the node. Here's the xpath that I've tried:

A) //rm:TradeMsg/rm:Submitter/rm:partyTradeIdentifier/fpml:tradeId/
B) //rm:TradeMsg/rm:Submitter/rm:partyTradeIdentifier/fpml:tradeId/text()
C) //rm:TradeMsg/rm:Submitter/rm:partyTradeIdentifier/fpml:tradeId/text()[normalize-space()]
D) normalize-space(//rm:TradeMsg/rm:Submitter/rm:partyTradeIdentifier/fpml:tradeId/text())
  • A) Returns the whole node, not just the value
  • B) Returns the value, but with loads of whitespace
  • C) Also returns the value with all of the whitespace
  • D) Throws XPathExpressionException

Question for xpath gurus - am I missing something in my xpath that would trim this value?

Question for camel gurus - maybe I can trim this value after it's evaluated? I know I could pass it through another step, but I'd think that this would be a common enough use case to be supported in the ns.xpath call itself.

I also noticed that the ns.xpath sets the header to an instance of 'com.sun.org.apache.xml.internal.dtm.ref.DTMNodeList' instead String, which would be the expected type.

Thanks all,
Roy

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

瑕疵 2024-11-23 15:42:44

试试这个...它在 2.8-SNAPSHOT 中工作正常

.setHeader(Exchange.FILE_NAME, 
           XPathBuilder.xpath("normalize-space(//rm:TradeMsg/rm:Submitter/rm:partyTradeIdentifier/fpml:tradeId/text()", String.class))

try this...it works fine in 2.8-SNAPSHOT

.setHeader(Exchange.FILE_NAME, 
           XPathBuilder.xpath("normalize-space(//rm:TradeMsg/rm:Submitter/rm:partyTradeIdentifier/fpml:tradeId/text()", String.class))
染年凉城似染瑾 2024-11-23 15:42:44

你有没有尝试过:
标准化空间(//rm:TradeMsg/rm:提交者/rm:partyTradeIdentifier/fpml:tradeId)

Have you tried:
normalize-space(//rm:TradeMsg/rm:Submitter/rm:partyTradeIdentifier/fpml:tradeId)
?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文