0

I'm not able to get the xpath expression to work. I'm using Camel 2.15.1. Any help would be greatly appreciated.

I've tried the following

<xpath>name(//*[1])='PPR_PC2'</xpath>
<xpath>name("//*[local-name()='PPR_PC2')</xpath>
<xpath>//PPR_PC2</xpath>

My xml file

<?xml version="1.0" encoding="UTF-8"?>
<PPR_PC2 xmlns="urn:hl7-org:v2xml">
   <MSH>
      <MSH.1>|</MSH.1>
      ...
      ...
    </MSH>
    ...
    ...
</PPR_PC2>

My Camel route

<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>

This is the error I'm getting

[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/>, ...) 

3 Answers 3

1

The error message

A sequence of more than one item is not allowed as the first argument of name()

suggests that when you wrote

name(//*[1])

(which selects every element that is the first child of its parent)

you probably meant

name((//*)[1])

(which selects the first element in the document)

though that would give you exactly the same as

name(/*)
Sign up to request clarification or add additional context in comments.

Comments

0

Declare the namespace and use it. Note the xmlns below.

<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>

You can pick any prefix you like, but you have to pick a prefix in order to be able to declare and use a namespace. I chose hl7 for the example.

Declare the namespace higher up in your XML file to be able to use it in multiple places.

Also read how to use xpath in camel-context.xml to check if a particular node is exist or not

Comments

0

Try using :

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

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.