You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@camel.apache.org by at...@free.fr on 2011/09/12 14:21:45 UTC

XML Namespace and Xpath injection

Hello,
I'm trying to inject a value in bean trough an xpath injection as following :


	public void audit(@XPath("/Envelope/Body/getToken/username/text()") String id, @Body String body) {
		logger.info("Id :"+id );
		logger.info("Body = "+body);
	}

Unfortunately, all the elements are fully qualified by a namespace.

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tok="http://mynamespace">
   <soapenv:Header/>
   <soapenv:Body>
      <tok:getToken>
         <!--Optional:-->
         <tok:username>mylogin</tok:username>
      </tok:getToken>
   </soapenv:Body>
</soapenv:Envelope>

I tried to lookup the element value by this xpath query /Envelope/Body/getToken/username/text() or /soapenv:Envelope/soapenv:Body/tok:getToken/tok:username/text(), but both doesn't work.

I guess the problem is that the namespace context is missing.
How to specify the namespace context ? In the xml file ? In the java class by an annotation ?

Thanks in advance for your help.

Regards
Alexandre Touret


Re: [ camel ] XML Namespace and Xpath injection

Posted by Marcin Cylke <mc...@touk.pl>.
On 09/12/2011 02:21 PM, atouret.nospam@free.fr wrote:
> Hello,
> I'm trying to inject a value in bean trough an xpath injection as following :
>
>
> 	public void audit(@XPath("/Envelope/Body/getToken/username/text()") String id, @Body String body) {
> 		logger.info("Id :"+id );
> 		logger.info("Body = "+body);
> 	}
>
> Unfortunately, all the elements are fully qualified by a namespace.

You can pass namespaces to @XPath annotation, or create your own 
annotation, with namespaces included:

@Retention(RetentionPolicy.RUNTIME)
@Target( { ElementType.FIELD, ElementType.METHOD, ElementType.PARAMETER })
@LanguageAnnotation(language = "xpath", factory = 
XpathExpressionAnnotationFactoryImproved.class)
public @interface StXpath {
	String value();

	// You can add the namespaces as the default value of the annotation
	NamespacePrefix[] namespaces() default { @NamespacePrefix(prefix = 
"st", uri = "http://example.pl/service/schemas") };

	Class<?> resultType() default String.class;
}



Re: XML Namespace and Xpath injection

Posted by Claus Ibsen <cl...@gmail.com>.
The @XPath annotation has an attribute to set namespaces.

There is an example in Camel in Action book p118


On Mon, Sep 12, 2011 at 2:21 PM,  <at...@free.fr> wrote:
> Hello,
> I'm trying to inject a value in bean trough an xpath injection as following :
>
>
>        public void audit(@XPath("/Envelope/Body/getToken/username/text()") String id, @Body String body) {
>                logger.info("Id :"+id );
>                logger.info("Body = "+body);
>        }
>
> Unfortunately, all the elements are fully qualified by a namespace.
>
> <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tok="http://mynamespace">
>   <soapenv:Header/>
>   <soapenv:Body>
>      <tok:getToken>
>         <!--Optional:-->
>         <tok:username>mylogin</tok:username>
>      </tok:getToken>
>   </soapenv:Body>
> </soapenv:Envelope>
>
> I tried to lookup the element value by this xpath query /Envelope/Body/getToken/username/text() or /soapenv:Envelope/soapenv:Body/tok:getToken/tok:username/text(), but both doesn't work.
>
> I guess the problem is that the namespace context is missing.
> How to specify the namespace context ? In the xml file ? In the java class by an annotation ?
>
> Thanks in advance for your help.
>
> Regards
> Alexandre Touret
>
>



-- 
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/