2016-09-29 68 views
0

我无法使xpath表达式正常工作。我正在使用骆驼2.15.1。任何帮助将不胜感激。骆驼使用xpath检索xml标签名称

我已经试过以下

<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/>, ...) 
错误3210

回答

1

该错误消息

多个项目的序列是不允许的名称()

的第一个参数暗示当你写下你的时候

name(//*[1]) 

(其中选择每一个元素是其父母的第一个孩子)

你可能是指

name((//*)[1]) 

(其中选择文档中的第一个元素)

尽管这会给你完全一样

name(/*) 
+0

谢谢迈克尔,那工作很好。 – user1998820

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

0

尝试:

Namespaces ns = new Namespaces("cus","http://..."); 

ns.xpath("//MSH.1/text()", java.lang.String.class)