1 Reply Latest reply on Dec 6, 2015 12:24 PM by Ashish Mishra

    Camel Loop Xpath Expression

    Tomasz Zalewski Newbie

      Has anyone tried setting up XPath expression for the loop counter?

      The following camel route does not seem to work for me and I am assuming that the expression has to return a number for the loop counter:

       

      <?xml version="1.0" encoding="ASCII"?>

      <routes xmlns="http://camel.apache.org/schema/spring">

        <route>

          <from uri="switchyard://TrnsformationService"/>

          <log message="TrnsformationService - message received: ${body}" loggingLevel="TRACE"/>

          <loop>

            <xpath>count(//*[local-name()='Site'])</xpath>

            <to uri="xslt:SitesToLocations.xsl"/>

            <log message="TrnsformationService - message trasformed: ${body}" loggingLevel="TRACE"/>

          </loop>

        </route>

      </routes>


      The incoming message is quite simple:

       

      <?xml version="1.0" encoding="UTF-8"?>

      <Sites xmlns="urn://abc.com/site">

      <Site>

        <code>123</code>

        <name>Cool-Site 1</name>

        <description>This is the cool site #1.</description>

      </Site>

      <Site xmlns="urn://nrel.gov/site">

        <code>1234</code>

        <name>Cool-Site 2</name>

        <description>This is the cool site #2.</description>

      </Site>

      </Sites>


      When run, the  exception shown below gets thrown from the junit. The cause is indicated as:

      Caused by: com.sun.org.apache.xpath.internal.XPathException: Can not convert #NUMBER to a NodeList!

      I would expect that the route needs a number to set up the loop counter.

      If I just select the nodes and remove the count() function it does not work either as number format exception is thrown:

      Caused by: org.apache.camel.RuntimeCamelException: java.lang.NumberFormatException: For input string: "<Site ...


      This project is using 2.0.0.Final of switchyard and 2.14.4 of camel-core (pom.xml is attached).


      org.switchyard.test.InvocationFaultException

        at org.switchyard.test.Invoker.sendInOut(Invoker.java:325)

        at com.example.switchyard.TrnsformationServiceTest.testTransformIt(TrnsformationServiceTest.java:43)

        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)

        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)

        at java.lang.reflect.Method.invoke(Method.java:606)

        at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)

        at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)

        at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)

        at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)

        at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)

        at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)

        at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)

        at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)

        at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)

        at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)

        at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)

        at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)

        at org.junit.runners.ParentRunner.run(ParentRunner.java:309)

        at org.switchyard.test.SwitchYardRunner.run(SwitchYardRunner.java:85)

        at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)

        at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)

        at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)

        at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)

        at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)

        at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)

      Caused by: org.switchyard.HandlerException: org.apache.camel.builder.xml.InvalidXPathExpression: Invalid xpath: count(//*[local-name()='Site']). Reason: javax.xml.xpath.XPathExpressionException

        at org.switchyard.component.camel.switchyard.SwitchYardConsumer.invokeCamelProcessor(SwitchYardConsumer.java:184)

        at org.switchyard.component.camel.switchyard.SwitchYardConsumer.handleMessage(SwitchYardConsumer.java:96)

        at org.switchyard.handlers.ProviderHandler$1.run(ProviderHandler.java:60)

        at org.switchyard.handlers.ProviderHandler$1.run(ProviderHandler.java:58)

        at java.security.AccessController.doPrivileged(Native Method)

        at javax.security.auth.Subject.doAsPrivileged(Subject.java:536)

        at org.switchyard.security.provider.DefaultSecurityProvider.runAs(DefaultSecurityProvider.java:169)

        at org.switchyard.handlers.ProviderHandler.handleMessage(ProviderHandler.java:65)

        at org.switchyard.bus.camel.processors.HandlerProcessor.process(HandlerProcessor.java:61)

        at org.apache.camel.processor.DelegateSyncProcessor.process(DelegateSyncProcessor.java:63)

        at org.apache.camel.management.InstrumentationProcessor.process(InstrumentationProcessor.java:72)

        at org.apache.camel.processor.DelegateAsyncProcessor.process(DelegateAsyncProcessor.java:91)

        at org.apache.camel.processor.DelegateAsyncProcessor.process(DelegateAsyncProcessor.java:91)

        at org.switchyard.bus.camel.audit.FaultProcessor.process(FaultProcessor.java:45)

        at org.apache.camel.processor.DelegateAsyncProcessor.process(DelegateAsyncProcessor.java:91)

        at org.apache.camel.processor.CamelInternalProcessor.process(CamelInternalProcessor.java:191)

        at org.apache.camel.processor.Pipeline.process(Pipeline.java:118)

        at org.apache.camel.processor.Pipeline.process(Pipeline.java:80)

        at org.apache.camel.processor.TryProcessor.process(TryProcessor.java:111)

        at org.apache.camel.processor.TryProcessor.process(TryProcessor.java:82)

        at org.apache.camel.management.InstrumentationProcessor.process(InstrumentationProcessor.java:72)

        at org.apache.camel.processor.DelegateAsyncProcessor.process(DelegateAsyncProcessor.java:91)

        at org.apache.camel.processor.DelegateAsyncProcessor.process(DelegateAsyncProcessor.java:91)

        at org.switchyard.bus.camel.audit.FaultProcessor.process(FaultProcessor.java:45)

        at org.apache.camel.processor.DelegateAsyncProcessor.process(DelegateAsyncProcessor.java:91)

        at org.apache.camel.processor.CamelInternalProcessor.process(CamelInternalProcessor.java:191)

        at org.apache.camel.processor.CamelInternalProcessor.process(CamelInternalProcessor.java:191)

        at org.apache.camel.component.direct.DirectProducer.process(DirectProducer.java:51)

        at org.apache.camel.processor.CamelInternalProcessor.process(CamelInternalProcessor.java:191)

        at org.apache.camel.processor.UnitOfWorkProducer.process(UnitOfWorkProducer.java:74)

        at org.apache.camel.impl.ProducerCache$2.doInProducer(ProducerCache.java:375)

        at org.apache.camel.impl.ProducerCache$2.doInProducer(ProducerCache.java:343)

        at org.apache.camel.impl.ProducerCache.doInProducer(ProducerCache.java:233)

        at org.apache.camel.impl.ProducerCache.sendExchange(ProducerCache.java:343)

        at org.apache.camel.impl.ProducerCache.send(ProducerCache.java:168)

        at org.apache.camel.impl.DefaultProducerTemplate.send(DefaultProducerTemplate.java:119)

        at org.apache.camel.impl.DefaultProducerTemplate.send(DefaultProducerTemplate.java:105)

        at org.switchyard.bus.camel.ExchangeDispatcher.dispatch(ExchangeDispatcher.java:87)

        at org.switchyard.bus.camel.CamelExchange.sendInternal(CamelExchange.java:238)

        at org.switchyard.bus.camel.CamelExchange.send(CamelExchange.java:175)

        at org.switchyard.test.Invoker.sendInOut(Invoker.java:320)

        ... 25 more

      Caused by: org.apache.camel.builder.xml.InvalidXPathExpression: Invalid xpath: count(//*[local-name()='Site']). Reason: javax.xml.xpath.XPathExpressionException

        at org.apache.camel.builder.xml.XPathBuilder.doInEvaluateAs(XPathBuilder.java:910)

        at org.apache.camel.builder.xml.XPathBuilder.evaluateAs(XPathBuilder.java:774)

        at org.apache.camel.builder.xml.XPathBuilder.evaluate(XPathBuilder.java:744)

        at org.apache.camel.builder.xml.XPathBuilder.evaluate(XPathBuilder.java:167)

        at org.apache.camel.processor.LoopProcessor.process(LoopProcessor.java:54)

        at org.apache.camel.management.InstrumentationProcessor.process(InstrumentationProcessor.java:72)

        at org.apache.camel.processor.RedeliveryErrorHandler.process(RedeliveryErrorHandler.java:398)

        at org.apache.camel.processor.CamelInternalProcessor.process(CamelInternalProcessor.java:191)

        at org.apache.camel.processor.Pipeline.process(Pipeline.java:118)

        at org.apache.camel.processor.Pipeline.process(Pipeline.java:80)

        at org.apache.camel.processor.CamelInternalProcessor.process(CamelInternalProcessor.java:191)

        at org.apache.camel.util.AsyncProcessorHelper.process(AsyncProcessorHelper.java:105)

        at org.apache.camel.processor.DelegateAsyncProcessor.process(DelegateAsyncProcessor.java:87)

        at org.switchyard.component.camel.switchyard.SwitchYardConsumer.invokeCamelProcessor(SwitchYardConsumer.java:182)

        ... 65 more

      Caused by: javax.xml.xpath.XPathExpressionException

        at com.sun.org.apache.xpath.internal.jaxp.XPathExpressionImpl.evaluate(XPathExpressionImpl.java:200)

        at org.apache.camel.builder.xml.XPathBuilder.doInEvaluateAs(XPathBuilder.java:892)

        ... 78 more

      Caused by: com.sun.org.apache.xpath.internal.XPathException: Can not convert #NUMBER to a NodeList!

        at com.sun.org.apache.xpath.internal.objects.XObject.error(XObject.java:711)

        at com.sun.org.apache.xpath.internal.objects.XObject.nodelist(XObject.java:457)

        at com.sun.org.apache.xpath.internal.jaxp.XPathExpressionImpl.getResultAsType(XPathExpressionImpl.java:360)

        at com.sun.org.apache.xpath.internal.jaxp.XPathExpressionImpl.eval(XPathExpressionImpl.java:106)

        at com.sun.org.apache.xpath.internal.jaxp.XPathExpressionImpl.evaluate(XPathExpressionImpl.java:187)

        ... 79 more

       

      Any insight is much appreciated

      /Tomasz