You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@camel.apache.org by Alexey <re...@gmail.com> on 2012/08/28 14:00:49 UTC

xpath for header

hello, i'm trying to use xpath expression for the header message in the
route.

my route:

...
<route id="delayedDecline"  errorHandlerRef="DELAYERRORE">
    <from uri="activemq:queue:RESTRUCT.IDENTBEFORE?concurrentConsumers=1"/>
    <bean ref="headerExtender" />
    <filter>
        
        <xpath>/history/p[@id = 'RESTSYBRIGE' and @state != 'NN']</xpath> 
        <to uri="activemq:queue:RESTRUCT.DECLINE"/>
    </filter>
</route>
...

bean "headerExtender" put to message header this xml:

<?xml version='1.0' encoding='UTF-8' standalone='yes'?>
<history>
    <p datO='20120822132650' datI='20120822132650' datC='20120822132647'
data='' lgSt='FINISH' state='AN' order='0' num='3329' id='RESTSYBRIGE' />
    <p datO='20120822135229' datI='20120822132650' datC='20120822135229'
data='' lgSt='FINISH' state='AN' order='0' num='3330' id='IDENTEDITPHYS' />
</history>

but this don't work :(

Please help! How to correctly write this expression?





--
View this message in context: http://camel.465427.n5.nabble.com/xpath-for-header-tp5718190.html
Sent from the Camel Development mailing list archive at Nabble.com.

Re: xpath for header

Posted by Charles Moulliard <ch...@gmail.com>.
I think that you should consider to use some tool to help you :

-
http://stackoverflow.com/questions/688323/is-there-an-online-tester-for-xpath-selectors
- http://www.huttar.net/dimitre/XPV/TopXML-XPV.html

On Tue, Aug 28, 2012 at 2:00 PM, Alexey <re...@gmail.com> wrote:

> hello, i'm trying to use xpath expression for the header message in the
> route.
>
> my route:
>
> ...
> <route id="delayedDecline"  errorHandlerRef="DELAYERRORE">
>     <from uri="activemq:queue:RESTRUCT.IDENTBEFORE?concurrentConsumers=1"/>
>     <bean ref="headerExtender" />
>     <filter>
>
>         <xpath>/history/p[@id = 'RESTSYBRIGE' and @state != 'NN']</xpath>
>         <to uri="activemq:queue:RESTRUCT.DECLINE"/>
>     </filter>
> </route>
> ...
>
> bean "headerExtender" put to message header this xml:
>
> <?xml version='1.0' encoding='UTF-8' standalone='yes'?>
> <history>
>     <p datO='20120822132650' datI='20120822132650' datC='20120822132647'
> data='' lgSt='FINISH' state='AN' order='0' num='3329' id='RESTSYBRIGE' />
>     <p datO='20120822135229' datI='20120822132650' datC='20120822135229'
> data='' lgSt='FINISH' state='AN' order='0' num='3330' id='IDENTEDITPHYS' />
> </history>
>
> but this don't work :(
>
> Please help! How to correctly write this expression?
>
>
>
>
>
> --
> View this message in context:
> http://camel.465427.n5.nabble.com/xpath-for-header-tp5718190.html
> Sent from the Camel Development mailing list archive at Nabble.com.
>



-- 
Charles Moulliard
Apache Committer / Sr. Pr. Consultant at FuseSource.com
Twitter : @cmoulliard
Blog : http://cmoulliard.blogspot.com

Re: xpath for header

Posted by Alexey <re...@gmail.com>.
Claus Ibsen-2, cool! Thanks!



--
View this message in context: http://camel.465427.n5.nabble.com/xpath-for-header-tp5718190p5718393.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: xpath for header

Posted by Claus Ibsen <cl...@gmail.com>.
Hi

Well Camel usually have a easier way than wring 20 lines of code.

There is a static evaluate/matches method on XPathBuilder you can pass
in your xpath + data, eg

boolean matches = XPathBuilder.xpath("/foo/bar/@xyz").matches(context,
"<foo><bar xyz='cheese'/></foo>")




On Thu, Aug 30, 2012 at 12:04 PM, Alexey <re...@gmail.com> wrote:
> Claus Ibsen-2, thank you very much!
>
> For the time begin, I did that follows:
>
> ---route---
> ...
> <filter>
>     <method ref="histActAggregator" method="xpathExecute(${headers.history},
> '/history/p[@id = &quot;RESTSYBRIGE&quot; and @state != &quot;NN&quot;]')"
> />
>     <to uri="activemq:queue:RESTRUCT.DECLINE"/>
> </filter>
> ...
>
> ---bean 'histActAggregator' ---
> ...
>     public boolean xpathExecute(String xml, String xpathStr) {
>         try {
>             XPathFactory factory = XPathFactory.newInstance();
>             XPath xpath = factory.newXPath();
>             XPathExpression expr = xpath.compile(xpathStr);
>             InputSource inputSource = new InputSource(new
> StringReader(xml));
>             Boolean result = (Boolean) expr.evaluate(inputSource,
> XPathConstants.BOOLEAN);
>
>             return result;
>         } catch (XPathExpressionException ex) {
>             logger.log(Level.SEVERE, "XPathExpressionException; In xml: " +
> xml + "; xpath expression: ", ex);
>             throw new RuntimeException(ex);
>         }
>     }
> ...
>
> May be the solution isn't very elegantly, but it works :)
>
> thanks
>
>
>
> --
> View this message in context: http://camel.465427.n5.nabble.com/xpath-for-header-tp5718190p5718365.html
> Sent from the Camel - Users mailing list archive at Nabble.com.



-- 
Claus Ibsen
-----------------
FuseSource
Email: cibsen@fusesource.com
Web: http://fusesource.com
Twitter: davsclaus, fusenews
Blog: http://davsclaus.com
Author of Camel in Action: http://www.manning.com/ibsen

Re: xpath for header

Posted by Alexey <re...@gmail.com>.
Claus Ibsen-2, thank you very much!

For the time begin, I did that follows:

---route---
...
<filter>
    <method ref="histActAggregator" method="xpathExecute(${headers.history},
'/history/p[@id = &quot;RESTSYBRIGE&quot; and @state != &quot;NN&quot;]')"
/>
    <to uri="activemq:queue:RESTRUCT.DECLINE"/>
</filter>
...

---bean 'histActAggregator' ---
...
    public boolean xpathExecute(String xml, String xpathStr) {
        try {
            XPathFactory factory = XPathFactory.newInstance();
            XPath xpath = factory.newXPath();
            XPathExpression expr = xpath.compile(xpathStr);
            InputSource inputSource = new InputSource(new
StringReader(xml));
            Boolean result = (Boolean) expr.evaluate(inputSource,
XPathConstants.BOOLEAN);

            return result;
        } catch (XPathExpressionException ex) {
            logger.log(Level.SEVERE, "XPathExpressionException; In xml: " +
xml + "; xpath expression: ", ex);
            throw new RuntimeException(ex);
        }
    }
...

May be the solution isn't very elegantly, but it works :)

thanks



--
View this message in context: http://camel.465427.n5.nabble.com/xpath-for-header-tp5718190p5718365.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: xpath for header

Posted by Claus Ibsen <cl...@gmail.com>.
On Wed, Aug 29, 2012 at 6:17 PM, Claus Ibsen <cl...@gmail.com> wrote:
> On Wed, Aug 29, 2012 at 2:10 PM, Alexey <re...@gmail.com> wrote:
>> Sorry, I moved the discussion to another topic.
>> You don't quite understand me. My xpath expression is correct. I tested this
>> xpath expr. with my xml using online xpath selector, it's work normal. The
>> problem is that xpath must select from my header, but not in the body.
>>
>> For example that expression work incorrect:
>> <xpath>in:header('history')/history/p[@id = 'RESTSYBRIGE' and @state !=
>> 'NN']</xpath>
>>
>> message not move to queue RESTRUCT.DECLINE
>>
>> How to make so that the XPath searching in the header?
>>
>
> xpath works on message body only.
>

I logged a JIRA to improve this in the future
https://issues.apache.org/jira/browse/CAMEL-5550


>
>>
>>
>> --
>> View this message in context: http://camel.465427.n5.nabble.com/xpath-for-header-tp5718190p5718251.html
>> Sent from the Camel - Users mailing list archive at Nabble.com.
>
>
>
> --
> Claus Ibsen
> -----------------
> FuseSource
> Email: cibsen@fusesource.com
> Web: http://fusesource.com
> Twitter: davsclaus, fusenews
> Blog: http://davsclaus.com
> Author of Camel in Action: http://www.manning.com/ibsen



-- 
Claus Ibsen
-----------------
FuseSource
Email: cibsen@fusesource.com
Web: http://fusesource.com
Twitter: davsclaus, fusenews
Blog: http://davsclaus.com
Author of Camel in Action: http://www.manning.com/ibsen

Re: xpath for header

Posted by Claus Ibsen <cl...@gmail.com>.
On Wed, Aug 29, 2012 at 2:10 PM, Alexey <re...@gmail.com> wrote:
> Sorry, I moved the discussion to another topic.
> You don't quite understand me. My xpath expression is correct. I tested this
> xpath expr. with my xml using online xpath selector, it's work normal. The
> problem is that xpath must select from my header, but not in the body.
>
> For example that expression work incorrect:
> <xpath>in:header('history')/history/p[@id = 'RESTSYBRIGE' and @state !=
> 'NN']</xpath>
>
> message not move to queue RESTRUCT.DECLINE
>
> How to make so that the XPath searching in the header?
>

xpath works on message body only.


>
>
> --
> View this message in context: http://camel.465427.n5.nabble.com/xpath-for-header-tp5718190p5718251.html
> Sent from the Camel - Users mailing list archive at Nabble.com.



-- 
Claus Ibsen
-----------------
FuseSource
Email: cibsen@fusesource.com
Web: http://fusesource.com
Twitter: davsclaus, fusenews
Blog: http://davsclaus.com
Author of Camel in Action: http://www.manning.com/ibsen

Re: xpath for header

Posted by Alexey <re...@gmail.com>.
Sorry, I moved the discussion to another topic. 
You don't quite understand me. My xpath expression is correct. I tested this
xpath expr. with my xml using online xpath selector, it's work normal. The
problem is that xpath must select from my header, but not in the body.

For example that expression work incorrect:
<xpath>in:header('history')/history/p[@id = 'RESTSYBRIGE' and @state !=
'NN']</xpath> 

message not move to queue RESTRUCT.DECLINE

How to make so that the XPath searching in the header?



--
View this message in context: http://camel.465427.n5.nabble.com/xpath-for-header-tp5718190p5718251.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: xpath for header

Posted by AlanFoster <al...@alanfoster.me>.
Hi Alexey,

It is best to ask camel questions to the user's mailing list, this mailing
list is for camel development.

To answer your question, it most likely isn't working as expected because
your xpath isn't a predicate

If this isn't the problem and you're getting a stack trace, please post that
-On the users mailing list of course

Hope this helps
Alan



--
View this message in context: http://camel.465427.n5.nabble.com/xpath-for-header-tp5718190p5718212.html
Sent from the Camel Development mailing list archive at Nabble.com.