You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@camel.apache.org by Andrè <mo...@web.de> on 2011/07/06 15:51:58 UTC

XPath namespaces in Spring DSL?

hey, 
another problem i´m facing is ...
 
<to uri="bean:isAuthZBlocked?method=checkRequest" /> 
gets a xml as string

the method head looks like this:

public void checkRequest(
		
@XPath("/Request/Subject/Attribute[@AttributeId='urn:oasis:names:tc:xacml:1.0:subject:subject-id']/AttributeValue/text()")
String ref)

but the xml is like that:
<Request xmlns="urn:oasis:names:tc:xacml:2.0:context:schema:os"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation=" urn:oasis:names:tc:xacml:2.0:context:schema:os
http://docs.oasis-open.org/xacml/access_control-xacml-2.0-context-schema-os.xsd">
	<Subject>
		<Attribute AttributeId="urn:oasis:names:tc:xacml:1.0:subject:subject-id"
			DataType="urn:oasis:names:tc:xacml:1.0:data-type:rfc822Name">
			<AttributeValue>seth@users.example.com</AttributeValue>
		</Attribute>
.....
which means, that i have to declare the namespace which is defined in
request

but aflter reading this http://camel.apache.org/xpath.html
i dont know how to do it in Spring DSL? 


--
View this message in context: http://camel.465427.n5.nabble.com/XPath-namespaces-in-Spring-DSL-tp4556826p4556826.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: XPath namespaces in Spring DSL?

Posted by Ashwin Karpe <ak...@fusesource.com>.
Hi,

Please check out the example below. 

https://svn.apache.org/viewvc/camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/processor/xpathFilterWithNamespace.xml?view=co&content-type=text%2Fplain
https://svn.apache.org/viewvc/camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/processor/xpathFilterWithNamespace.xml?view=co&content-type=text%2Fplain 

Primarily see how the namepace "xmlns:foo" has been defined in <beans/> and
used with XPath.

Hope this helps.

Cheers,

Ashwin...

-----
---------------------------------------------------------
Ashwin Karpe
Apache Camel Committer & Sr Principal Consultant
FUSESource (a Progress Software Corporation subsidiary)
http://fusesource.com 

Blog: http://opensourceknowledge.blogspot.com 
---------------------------------------------------------
--
View this message in context: http://camel.465427.n5.nabble.com/XPath-namespaces-in-Spring-DSL-tp4556826p4556975.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: XPath namespaces in Spring DSL?

Posted by Claus Ibsen <cl...@gmail.com>.
On Thu, Oct 18, 2012 at 10:46 AM, kmoens <kr...@yahoo.com> wrote:
> Hi,
>
> had the same issue with a document having a default namespace defined, sth
> like
> <doc xmlns="urn:abc"><tag1><tag2>hello</tag2><tag3>world</tag3></tag1></doc>
>
> To get the value 'hello' from the above document, I tried in my bean
> public String getGreeting(@Xpath("doc/tag1/tag2/text()") String greeting) {
>  return greeting;
> }
> which returned an empty String.
>
> The solution is to 'mimic' if there was a prefix e.g. "p",  in this case the
> Xpath looks like:
> @Xpath(value="p:doc/p:tag1/p:tag2/text()", namespaces=
> @NamespacePrefix(prefix="p", uri="urn:abc"))
>
> which returned "hello".
>
> A bit cumbersome, but it worked ;)
>

Yeah unfortunately the XPath API for xpath expressions don't allow us
to hook into it, or some way "mimic" this default namespace for us. So
the only option is to assign a bogus namespace prefix. Or maybe have
luck with that /local() stuff.


> Hope this helps,
> Kris
>
>
>
>
> --
> View this message in context: http://camel.465427.n5.nabble.com/XPath-namespaces-in-Spring-DSL-tp4556826p5721221.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

Re: XPath namespaces in Spring DSL?

Posted by kmoens <kr...@yahoo.com>.
Hi,

had the same issue with a document having a default namespace defined, sth
like
<doc xmlns="urn:abc"><tag1><tag2>hello</tag2><tag3>world</tag3></tag1></doc>

To get the value 'hello' from the above document, I tried in my bean
public String getGreeting(@Xpath("doc/tag1/tag2/text()") String greeting) {
 return greeting; 
}
which returned an empty String.

The solution is to 'mimic' if there was a prefix e.g. "p",  in this case the
Xpath looks like:
@Xpath(value="p:doc/p:tag1/p:tag2/text()", namespaces=
@NamespacePrefix(prefix="p", uri="urn:abc")) 

which returned "hello".

A bit cumbersome, but it worked ;)

Hope this helps,
Kris




--
View this message in context: http://camel.465427.n5.nabble.com/XPath-namespaces-in-Spring-DSL-tp4556826p5721221.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: XPath namespaces in Spring DSL?

Posted by Claus Ibsen <cl...@gmail.com>.
On Fri, Jun 15, 2012 at 1:58 PM, Andreas A. <an...@gmail.com> wrote:

> Yeah I found and applied the example from the book.
>
> This works as long as there is actually a prefix in the xml. When there's
> only a default namespace the XPath-qeury returns nothing. It doesn't throw
> an exception like it normally would if the path was incorrect.
>
>
There is something about local() in the xpath expression you can use I
think.
To kinda refer to a local namepsace or something like that.

People on this forum have posted and talked about this in the past.

And I guess you can possible google something about that.


> --
> View this message in context:
> http://camel.465427.n5.nabble.com/XPath-namespaces-in-Spring-DSL-tp4556826p5714537.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 namespaces in Spring DSL?

Posted by "Andreas A." <an...@gmail.com>.
Yeah I found and applied the example from the book.

This works as long as there is actually a prefix in the xml. When there's
only a default namespace the XPath-qeury returns nothing. It doesn't throw
an exception like it normally would if the path was incorrect.

--
View this message in context: http://camel.465427.n5.nabble.com/XPath-namespaces-in-Spring-DSL-tp4556826p5714537.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: XPath namespaces in Spring DSL?

Posted by Claus Ibsen <cl...@gmail.com>.
The book has an example and the source code is here
http://code.google.com/p/camelinaction/source/browse/trunk/chapter4/bean/src/main/java/camelinaction/XmlOrderNamespaceService.java

On Thu, Jun 14, 2012 at 3:35 PM, Andreas A. <an...@gmail.com> wrote:

> There is no example of this on the website as far as I can see, but thanks
> for the pointer to the section in the book. I still think there is a
> problem
> when using a <routeContext>. I haven't tried newer versions though.
>
> Also - does Ashwins example produce the same result as this (only in Spring
> DSL)?
>
> https://svn.apache.org/repos/asf/camel/trunk/camel-core/src/test/java/org/apache/camel/builder/xml/XPathNamespaceTest.java
>
>
>
> --
> View this message in context:
> http://camel.465427.n5.nabble.com/XPath-namespaces-in-Spring-DSL-tp4556826p5714484.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 namespaces in Spring DSL?

Posted by "Andreas A." <an...@gmail.com>.
There is no example of this on the website as far as I can see, but thanks
for the pointer to the section in the book. I still think there is a problem
when using a <routeContext>. I haven't tried newer versions though.

Also - does Ashwins example produce the same result as this (only in Spring
DSL)?
https://svn.apache.org/repos/asf/camel/trunk/camel-core/src/test/java/org/apache/camel/builder/xml/XPathNamespaceTest.java



--
View this message in context: http://camel.465427.n5.nabble.com/XPath-namespaces-in-Spring-DSL-tp4556826p5714484.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: XPath namespaces in Spring DSL?

Posted by Claus Ibsen <cl...@gmail.com>.
On Thu, Jun 14, 2012 at 1:21 PM, Andreas A. <an...@gmail.com> wrote:

> Claus can you provide an example of the @Xpath-annotation where a namespace
> is specified?
>
>
Camel in Action book, chapter 4, section 4.5.4.

And the Camel docs as well, see
http://camel.apache.org/xpath.html

eg a tip is to just type @xpath in the search box on the Camel front page,
and read the links.




> I have this issue https://issues.apache.org/jira/browse/CAMEL-3155 and I
> believe that Ashwin's and your answer in this thread should solve the
> problem.
>
> I'm using Camel 2.4.0 w. Spring DSL and tried Ashwin's solution however the
> path is not recognized. I think it might be a problem because I'm using a
> CamelContext in one file and importing a RouteContext in another. I've
> tried
> defining the namespace in the beans-tag in each and both at the same time.
>
>
org.apache.camel.builder.xml.InvalidXPathExpression: Invalid xpath:
> /myns:myroot/myelement/@Attrib. Reason:
> javax.xml.xpath.XPathExpressionException.
>
> --
> View this message in context:
> http://camel.465427.n5.nabble.com/XPath-namespaces-in-Spring-DSL-tp4556826p5714479.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 namespaces in Spring DSL?

Posted by "Andreas A." <an...@gmail.com>.
Claus can you provide an example of the @Xpath-annotation where a namespace
is specified?

I have this issue https://issues.apache.org/jira/browse/CAMEL-3155 and I
believe that Ashwin's and your answer in this thread should solve the
problem.

I'm using Camel 2.4.0 w. Spring DSL and tried Ashwin's solution however the
path is not recognized. I think it might be a problem because I'm using a
CamelContext in one file and importing a RouteContext in another. I've tried
defining the namespace in the beans-tag in each and both at the same time.

org.apache.camel.builder.xml.InvalidXPathExpression: Invalid xpath:
/myns:myroot/myelement/@Attrib. Reason:
javax.xml.xpath.XPathExpressionException.

--
View this message in context: http://camel.465427.n5.nabble.com/XPath-namespaces-in-Spring-DSL-tp4556826p5714479.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: XPath namespaces in Spring DSL?

Posted by Claus Ibsen <cl...@gmail.com>.
The Camel @XPath also supports specifying namespaces.


On Wed, Jul 6, 2011 at 3:51 PM, Andrè <mo...@web.de> wrote:
> hey,
> another problem i´m facing is ...
>
> <to uri="bean:isAuthZBlocked?method=checkRequest" />
> gets a xml as string
>
> the method head looks like this:
>
> public void checkRequest(
>
> @XPath("/Request/Subject/Attribute[@AttributeId='urn:oasis:names:tc:xacml:1.0:subject:subject-id']/AttributeValue/text()")
> String ref)
>
> but the xml is like that:
> <Request xmlns="urn:oasis:names:tc:xacml:2.0:context:schema:os"
>        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
>        xsi:schemaLocation=" urn:oasis:names:tc:xacml:2.0:context:schema:os
> http://docs.oasis-open.org/xacml/access_control-xacml-2.0-context-schema-os.xsd">
>        <Subject>
>                <Attribute AttributeId="urn:oasis:names:tc:xacml:1.0:subject:subject-id"
>                        DataType="urn:oasis:names:tc:xacml:1.0:data-type:rfc822Name">
>                        <AttributeValue>seth@users.example.com</AttributeValue>
>                </Attribute>
> .....
> which means, that i have to declare the namespace which is defined in
> request
>
> but aflter reading this http://camel.apache.org/xpath.html
> i dont know how to do it in Spring DSL?
>
>
> --
> View this message in context: http://camel.465427.n5.nabble.com/XPath-namespaces-in-Spring-DSL-tp4556826p4556826.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.blogspot.com/
Author of Camel in Action: http://www.manning.com/ibsen/