XPath namespaces in Spring DSL?

classic Classic list List threaded Threaded
11 messages Options
Reply | Threaded
Open this post in threaded view
|

XPath namespaces in Spring DSL?

Andrè
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?
Reply | Threaded
Open this post in threaded view
|

Re: XPath namespaces in Spring DSL?

Ashwin Karpe
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

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
Red Hat
http://www.redhat.com

Blog:http://opensourceknowledge.blogspot.com
---------------------------------------------------------
Reply | Threaded
Open this post in threaded view
|

Re: XPath namespaces in Spring DSL?

Claus Ibsen-2
In reply to this post by Andrè
The Camel @XPath also supports specifying namespaces.


On Wed, Jul 6, 2011 at 3:51 PM, Andrè <[hidden email]> 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>[hidden email]</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: [hidden email]
Web: http://fusesource.com
Twitter: davsclaus, fusenews
Blog: http://davsclaus.blogspot.com/
Author of Camel in Action: http://www.manning.com/ibsen/
Reply | Threaded
Open this post in threaded view
|

Re: XPath namespaces in Spring DSL?

Andreas A.
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.
Reply | Threaded
Open this post in threaded view
|

Re: XPath namespaces in Spring DSL?

Claus Ibsen-2
On Thu, Jun 14, 2012 at 1:21 PM, Andreas A. <[hidden email]> 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: [hidden email]
Web: http://fusesource.com
Twitter: davsclaus, fusenews
Blog: http://davsclaus.com
Author of Camel in Action: http://www.manning.com/ibsen
Reply | Threaded
Open this post in threaded view
|

Re: XPath namespaces in Spring DSL?

Andreas A.
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

Reply | Threaded
Open this post in threaded view
|

Re: XPath namespaces in Spring DSL?

Claus Ibsen-2
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. <[hidden email]> 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: [hidden email]
Web: http://fusesource.com
Twitter: davsclaus, fusenews
Blog: http://davsclaus.com
Author of Camel in Action: http://www.manning.com/ibsen
Reply | Threaded
Open this post in threaded view
|

Re: XPath namespaces in Spring DSL?

Andreas A.
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.
Reply | Threaded
Open this post in threaded view
|

Re: XPath namespaces in Spring DSL?

Claus Ibsen-2
On Fri, Jun 15, 2012 at 1:58 PM, Andreas A. <[hidden email]> 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: [hidden email]
Web: http://fusesource.com
Twitter: davsclaus, fusenews
Blog: http://davsclaus.com
Author of Camel in Action: http://www.manning.com/ibsen
Reply | Threaded
Open this post in threaded view
|

Re: XPath namespaces in Spring DSL?

kmoens
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
Reply | Threaded
Open this post in threaded view
|

Re: XPath namespaces in Spring DSL?

Claus Ibsen-2
On Thu, Oct 18, 2012 at 10:46 AM, kmoens <[hidden email]> 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: [hidden email]
Web: http://fusesource.com
Twitter: davsclaus
Blog: http://davsclaus.com
Author of Camel in Action: http://www.manning.com/ibsen