我无法让xpath表达式起作用。我正在使用Camel 2.15.1。任何帮助将不胜感激。
我尝试了以下
<xpath>name(//*[1])='PPR_PC2'</xpath>
<xpath>name("//*[local-name()='PPR_PC2')</xpath>
<xpath>//PPR_PC2</xpath>
我的xml文件
<?xml version="1.0" encoding="UTF-8"?>
<PPR_PC2 xmlns="urn:hl7-org:v2xml">
<MSH>
<MSH.1>|</MSH.1>
...
...
</MSH>
...
...
</PPR_PC2>
我的骆驼路线
<route id="_route_1">
<from uri="activemq:queue:myQIN"/>
<doTry>
<choice>
<when>
// This path works without having namespace
<xpath>name(//*[1])='PPR_PC2'</xpath>
<to uri="xslt:transform/stylesheet.xsl"/>
<to uri="..."/>
</when>
<otherwise> ... </otherwise>
</choice>
<doCatch> ... </doCatch>
</doTry>
</route>
这是我得到的错误
[thread #1 - JmsConsumer[myQIN]] EndpointMessageListener
WARN Execution of JMS message listener failed.
Caused by: [org.apache.camel.builder.xml.InvalidXPathExpression - Invalid xpath: name(//*[1])='PPR_PC2'.
Reason: javax.xml.xpath.XPathExpressionException: net.sf.saxon.trans.XPathException:
A sequence of more than one item is not allowed as the first argument of name() (<PPR_PC2/>, <MSH/>, ...) ]
org.apache.camel.builder.xml.InvalidXPathExpression:
Invalid xpath: name(//*[1])='PPR_PC2'.
Reason: javax.xml.xpath.XPathExpressionException: net.sf.saxon.trans.XPathException:
A sequence of more than one item is not allowed as the first argument of name() (<PPR_PC2/>, <MSH/>, ...)
答案 0 :(得分:1)
错误消息
不允许包含多个项目的序列作为name()的第一个参数
表示你写的时候
name(//*[1])
(选择其父元素的第一个子元素)
你可能意味着name((//*)[1])
(选择文档中的第一个元素)
虽然这会给你完全相同的
name(/*)
答案 1 :(得分:0)
声明命名空间并使用它。请注意下面的xmlns
。
<route id="_route_1" xmlns:hl7="urn:hl7-org:v2xml">
<from uri="activemq:queue:myQIN"/>
<doTry>
<choice>
<when>
<xpath>/hl7:PPR_PC2</xpath>
<to uri="xslt:transform/stylesheet.xsl"/>
<to uri="..."/>
</when>
<otherwise> ... </otherwise>
</choice>
<doCatch> ... </doCatch>
</doTry>
</route>
您可以选择任何您喜欢的前缀,但您必须选择一个前缀才能声明和使用命名空间。我选择hl7
作为示例。
在XML文件中将命名空间声明为更高,以便能够在多个位置使用它。
另请阅读how to use xpath in camel-context.xml to check if a particular node is exist or not
答案 2 :(得分:0)
尝试使用:
Namespaces ns = new Namespaces("cus","http://...");
ns.xpath("//MSH.1/text()", java.lang.String.class)