You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@camel.apache.org by German O <ge...@gmail.com> on 2012/10/22 19:45:29 UTC

Choice with xpath

Hi, I have a situation where I have to do a choice with an xpath, and
depending on the content of an xml tag, I need to do different things.
Xml:

  *user*


other

   *admin*


Ie:
<c:route>
			<c:from uri="file://{{handshakeFolder}}" />
			<c:choice>
				<c:when>
					<xpath>//a/b/text() start-with "user"</xpath>
					<to uri="direct:user-handshake" />
				</c:when>
				<c:when>
					<xpath>//a/b/text() start-with "admin"</xpath>
					<to uri="direct:admin-handshake" />
				</c:when>
			</c:choice>
		</c:route>


I couldn't find the way to do this...

Thanks.



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

Re: Choice with xpath

Posted by Willem jiang <wi...@gmail.com>.
I don't think Camel xpath[1] can provide the function that you want.
You can use a bean method [2] with @Xpath annotation to do that kind of job.
[1]http://camel.apache.org/xpath.html
[2]http://camel.apache.org/bean-language.html


-- 
Willem Jiang

Red Hat, Inc.
FuseSource is now part of Red Hat
Web: http://www.fusesource.com | http://www.redhat.com
Blog: http://willemjiang.blogspot.com (http://willemjiang.blogspot.com/) (English)
          http://jnn.javaeye.com (http://jnn.javaeye.com/) (Chinese)
Twitter: willemjiang 
Weibo: willemjiang





On Tuesday, October 23, 2012 at 1:45 AM, German O wrote:

> Hi, I have a situation where I have to do a choice with an xpath, and
> depending on the content of an xml tag, I need to do different things.
> Xml:
> 
> *user*
> 
> 
> other
> 
> *admin*
> 
> 
> Ie:
> <c:route>
> <c:from uri="file://{{handshakeFolder}}" />
> <c:choice>
> <c:when>
> <xpath>//a/b/text() start-with "user"</xpath>
> <to uri="direct:user-handshake" />
> </c:when>
> <c:when>
> <xpath>//a/b/text() start-with "admin"</xpath>
> <to uri="direct:admin-handshake" />
> </c:when>
> </c:choice>
> </c:route>
> 
> 
> I couldn't find the way to do this...
> 
> Thanks.
> 
> 
> 
> --
> View this message in context: http://camel.465427.n5.nabble.com/Choice-with-xpath-tp5721410.html
> Sent from the Camel - Users mailing list archive at Nabble.com (http://Nabble.com).




Re: Choice with xpath

Posted by Christian Müller <ch...@gmail.com>.
I suggest adding the camel-saxon dependency and use:
<xpath saxon="true">...</xpath>

Best,

Christian
-----------------

Software Integration Specialist

Apache Member
V.P. Apache Camel | Apache Camel PMC Member | Apache Camel committer
Apache Incubator PMC Member

https://www.linkedin.com/pub/christian-mueller/11/551/642

On Thu, Aug 6, 2015 at 10:22 PM, anish.somadevan <ne...@gmail.com> wrote:

> Am having a similar issue and i get this error message,
>
> java.lang.ClassCastException: org.apache.xml.dtm.ref.DTMManagerDefault
> cannot be cast to org.apache.xml.dtm.DTMManager:
> java.lang.ClassCastException: org.apache.xml.dtm.ref.DTMManagerDefault
> cannot be cast to org.apache.xml.dtm.DTMManager
>         at org.apache.xml.dtm.DTMManager.newInstance(DTMManager.java:137)
> [xalan.jar:]
>         at org.apache.xpath.XPathContext.<init>(XPathContext.java:102)
> [xalan.jar:]
>         at org.apache.xpath.XPathContext.<init>(XPathContext.java:349)
> [xalan.jar:]
>         at
>
> org.apache.xpath.jaxp.XPathExpressionImpl.eval(XPathExpressionImpl.java:113)
>         at
> org.apache.xpath.jaxp.XPathExpressionImpl.eval(XPathExpressionImpl.java:99)
>
> This is how am trying to retrieve the value,
>
> <xpath>/CamelData/Provider/text()</xpath>
>
> This is how my XML looks,
>
> <CamelData>
> <Provider>ProviderValue</Provider>
> </CamelData>
>
> Can you please let me know how to resolve this?
>
> Thanks,
> Anish
>
>
>
> --
> View this message in context:
> http://camel.465427.n5.nabble.com/Choice-with-xpath-tp5721410p5770396.html
> Sent from the Camel - Users mailing list archive at Nabble.com.
>

Re: Choice with xpath

Posted by "anish.somadevan" <ne...@gmail.com>.
Am having a similar issue and i get this error message, 

java.lang.ClassCastException: org.apache.xml.dtm.ref.DTMManagerDefault
cannot be cast to org.apache.xml.dtm.DTMManager:
java.lang.ClassCastException: org.apache.xml.dtm.ref.DTMManagerDefault
cannot be cast to org.apache.xml.dtm.DTMManager 
        at org.apache.xml.dtm.DTMManager.newInstance(DTMManager.java:137)
[xalan.jar:] 
        at org.apache.xpath.XPathContext.<init>(XPathContext.java:102)
[xalan.jar:] 
        at org.apache.xpath.XPathContext.<init>(XPathContext.java:349)
[xalan.jar:] 
        at
org.apache.xpath.jaxp.XPathExpressionImpl.eval(XPathExpressionImpl.java:113) 
        at
org.apache.xpath.jaxp.XPathExpressionImpl.eval(XPathExpressionImpl.java:99) 

This is how am trying to retrieve the value, 

<xpath>/CamelData/Provider/text()</xpath>

This is how my XML looks,

<CamelData>
<Provider>ProviderValue</Provider>
</CamelData>

Can you please let me know how to resolve this? 

Thanks,
Anish



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

Re: Choice with xpath

Posted by German O <ge...@gmail.com>.
Thanks all!... I've solved the issue, I took Raul Kripalani version to solve
the problem.


Thanks



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

Re: Choice with xpath

Posted by Raul Kripalani <ra...@evosent.com>.
Hi German,

This is more of an XPath question rather than a Camel-related one, but here
goes the answer.

Do you have multiple occurrences of element 'a'? The double slash traverses
the entire input and returns all elements with name 'a' regardless of where
in the document they are.

Anyway, try these expressions, which are available on XPath 1.0:

   - starts-with(/a/b/text(), 'user')
   - starts-with(/a/b/text(), 'admin')

These will work if your input documents look like this, with no namespaces:

<a>
  <b>user</b>
</a>

Thanks,

*Raúl Kripalani*
Apache Camel Committer
Enterprise Architect, Program Manager, Open Source Integration specialist
http://about.me/raulkripalani | http://www.linkedin.com/in/raulkripalani
http://blog.raulkr.net | twitter: @raulvk <http://twitter.com/raulvk>

On Mon, Oct 22, 2012 at 6:45 PM, German O <ge...@gmail.com> wrote:

> Hi, I have a situation where I have to do a choice with an xpath, and
> depending on the content of an xml tag, I need to do different things.
> Xml:
>
>   *user*
>
>
> other
>
>    *admin*
>
>
> Ie:
> <c:route>
>                         <c:from uri="file://{{handshakeFolder}}" />
>                         <c:choice>
>                                 <c:when>
>                                         <xpath>//a/b/text() start-with
> "user"</xpath>
>                                         <to uri="direct:user-handshake" />
>                                 </c:when>
>                                 <c:when>
>                                         <xpath>//a/b/text() start-with
> "admin"</xpath>
>                                         <to uri="direct:admin-handshake" />
>                                 </c:when>
>                         </c:choice>
>                 </c:route>
>
>
> I couldn't find the way to do this...
>
> Thanks.
>
>
>
> --
> View this message in context:
> http://camel.465427.n5.nabble.com/Choice-with-xpath-tp5721410.html
> Sent from the Camel - Users mailing list archive at Nabble.com.
>

Re: Choice with xpath

Posted by Claus Ibsen <cl...@gmail.com>.
The JDK offers xpath 1.0. And it has a number of functions you can use.
XPath 2.0 is offered by Saxon, so you can use camel-saxon. And XPath
2.0 has more functions.

I am not sure if there is a starts with function in XPath. Or maybe
there is some reg ex like functions?

An alternative is just to grab the xpath text and store as a header.
And then use simple / mvel / groovy etc in the choice which would have
a starts with function.



On Mon, Oct 22, 2012 at 7:45 PM, German O <ge...@gmail.com> wrote:
> Hi, I have a situation where I have to do a choice with an xpath, and
> depending on the content of an xml tag, I need to do different things.
> Xml:
>
>   *user*
>
>
> other
>
>    *admin*
>
>
> Ie:
> <c:route>
>                         <c:from uri="file://{{handshakeFolder}}" />
>                         <c:choice>
>                                 <c:when>
>                                         <xpath>//a/b/text() start-with "user"</xpath>
>                                         <to uri="direct:user-handshake" />
>                                 </c:when>
>                                 <c:when>
>                                         <xpath>//a/b/text() start-with "admin"</xpath>
>                                         <to uri="direct:admin-handshake" />
>                                 </c:when>
>                         </c:choice>
>                 </c:route>
>
>
> I couldn't find the way to do this...
>
> Thanks.
>
>
>
> --
> View this message in context: http://camel.465427.n5.nabble.com/Choice-with-xpath-tp5721410.html
> Sent from the Camel - Users mailing list archive at Nabble.com.



-- 
Claus Ibsen
-----------------
Red Hat, Inc.
FuseSource is now part of Red Hat
Email: cibsen@redhat.com
Web: http://fusesource.com
Twitter: davsclaus
Blog: http://davsclaus.com
Author of Camel in Action: http://www.manning.com/ibsen