You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@camel.apache.org by toomanyedwards <to...@gmail.com> on 2013/10/31 19:06:41 UTC

How can I avoid setting namespace prefixes in xpath expressions in camel?

Hi all,
  I am using xpath to set camel header values.  Given a document that looks
something like this:

<?xml version="1.0" encoding="UTF-8"?>
<book xmlns="http://mydomain.com">
	<chapter>some text</chapter>
</book>


I can do something like this fine:

.setHeader(MY_HEADER_NAME).
.xpath("/nsprefix:book/nsprefix:chapter/text()",String.class, new
Namespaces("nsprefix","http://mydomain.com"))


But this pollutes my xpath expression with all the prefixes.  What I want to
be able to do is something like this:

.setHeader(MY_HEADER_NAME).xpath("book/chapter/text()",String.class, new
Namespaces("","http://mydomain.com"))


But this doesn't work.  I would have hoped that specifying an empty string
for the prefix as I did above would inform camel that elements without a
prefix have an implied namespace of "http://mydomain.com",but this doesn't
work.

Is there a way in camel to specify the implied namespace for elements that
don't have a prefix in my xpath?  If not, is there any technical reason this
couldn't be implemented?

Thanks!

-edward




--
View this message in context: http://camel.465427.n5.nabble.com/How-can-I-avoid-setting-namespace-prefixes-in-xpath-expressions-in-camel-tp5742461.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: How can I avoid setting namespace prefixes in xpath expressions in camel?

Posted by toomanyedwards <to...@gmail.com>.
Thanks, Claus.  Looks like the javax.xml.xpath.XPathFactory.setFeature()
functionality could potentially be leveraged at some point to do this.  

If camel exposed access to this standard method and a route was configured
to use the saxon parser then it looks like we could leverage the saxon
feature:
http://www.saxonica.com/documentation/javadoc/net/sf/saxon/lib/FeatureKeys.html#XQUERY_DEFAULT_ELEMENT_NAMESPACE  


In general, exposing setFeature through camel seems like an appropriate
thing to do since it's on the generic interface and not tied to any specific
implementation.  Just a thought.  

Do you think something like that make sense to you or would that be
inappropriate in camel?

-edward



--
View this message in context: http://camel.465427.n5.nabble.com/How-can-I-avoid-setting-namespace-prefixes-in-xpath-expressions-in-camel-tp5742461p5742524.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: How can I avoid setting namespace prefixes in xpath expressions in camel?

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

You cannot do that, as its not supported by the xpath expression api
that comes with java. Many before you have tried. You can
google/search the web about this.

Though you can use local-name() in the xpath expression, then you can
avoid the namespace prefix binding. But it clutters  the xpath stil.



On Fri, Nov 1, 2013 at 4:29 PM, toomanyedwards <to...@gmail.com> wrote:
> I thought I made it clear in my original question, but I guess not.  What I'm
> trying to is *not* have to specify in my xpath.
>
>   /nsprefix:book/nsprefix:chapter is extremely cluttered and redundant.
>
> It be much better if we could just specify:
>
>  /book/chapter and tell the processor what the default namespace should be
> for elements that don't have a prefix.
>
> This is effectively what you can do in an xml document like this:
>
> <?xml version="1.0" encoding="UTF-8"?>
> <book xmlns="http://mydomain.com">
>         <chapter>some text</chapter>
> </book>
>
>
> It'd be great to be able to do it an xpath as well.  Thanks for your reply,
> but no where in the camel docs does it show how to this.
>
> Please point to where if you think it does.
> http://camel.apache.org/xpath.html does describe how to do this.
>
> -edward
>
>
>
> --
> View this message in context: http://camel.465427.n5.nabble.com/How-can-I-avoid-setting-namespace-prefixes-in-xpath-expressions-in-camel-tp5742461p5742509.html
> Sent from the Camel - Users mailing list archive at Nabble.com.



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

Re: How can I avoid setting namespace prefixes in xpath expressions in camel?

Posted by toomanyedwards <to...@gmail.com>.
I thought I made it clear in my original question, but I guess not.  What I'm
trying to is *not* have to specify in my xpath.

  /nsprefix:book/nsprefix:chapter is extremely cluttered and redundant.

It be much better if we could just specify:

 /book/chapter and tell the processor what the default namespace should be
for elements that don't have a prefix.

This is effectively what you can do in an xml document like this:

<?xml version="1.0" encoding="UTF-8"?> 
<book xmlns="http://mydomain.com">
        <chapter>some text</chapter>
</book>


It'd be great to be able to do it an xpath as well.  Thanks for your reply,
but no where in the camel docs does it show how to this.  

Please point to where if you think it does. 
http://camel.apache.org/xpath.html does describe how to do this.

-edward



--
View this message in context: http://camel.465427.n5.nabble.com/How-can-I-avoid-setting-namespace-prefixes-in-xpath-expressions-in-camel-tp5742461p5742509.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: How can I avoid setting namespace prefixes in xpath expressions in camel?

Posted by alapaka <al...@rocketmail.com>.
well eddie - in this case RTFM!

here is what you are looking for in the Java DSL:

http://camel.apache.org/xpath.html



--
View this message in context: http://camel.465427.n5.nabble.com/How-can-I-avoid-setting-namespace-prefixes-in-xpath-expressions-in-camel-tp5742461p5742468.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: How can I avoid setting namespace prefixes in xpath expressions in camel?

Posted by alapaka <al...@rocketmail.com>.
you can set the namespace prefixes at route level and just use the prefix
anywhere in that route.

off the top of my head I don't know the java DSL for it, but in xml you do:

<route id="myroute" xmlns:nsprefix="http://mydomain.com">
...
<setHeader headerName="my_header">
	<xpath
resultType="java.lang.String">/nsprefix:book/nsprefix:chapter</xpath>
</setHeader>
...
</route>


and of course you can specify multiple namespaces



--
View this message in context: http://camel.465427.n5.nabble.com/How-can-I-avoid-setting-namespace-prefixes-in-xpath-expressions-in-camel-tp5742461p5742467.html
Sent from the Camel - Users mailing list archive at Nabble.com.