You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@camel.apache.org by Nurali Techie <nu...@yahoo.com> on 2013/10/31 15:02:55 UTC

Namespace list - blueprint - xpath

Hi Friends,

I want to execute xpath in my code.  It means; I have xpath expression, I have exchange object with In Message.

Using 'XPathExpression' and 'XPathBuilder' classes; I succeed to do so.  Below is the code snippet.

        XPathExpression xpathExprObj = new XPathExpression("/userResponse/User/id");
        xpathExprObj.setResultType(String.class);

        Object result = xpathExprObj.evaluate(exchange, Object.class);
        
But, if I have xpath with namespace (i.e xpath = /ns0:userResponse/User/id).. above code throwing exception .. saying - Prefix must resolve to a namespace: ns0

Part of Exception:

org.apache.camel.builder.xml.InvalidXPathExpression: Invalid xpath: /ns0:userResponse/User/id. Reason: javax.xml.xpath.XPathExpressionException
at org.apache.camel.builder.xml.XPathBuilder.evaluateAs(XPathBuilder.java:767) ~[bundlefile:2.11.2-sap-02]
at org.apache.camel.builder.xml.XPathBuilder.evaluate(XPathBuilder.java:748) ~[bundlefile:2.11.2-sap-02]
at org.apache.camel.builder.xml.XPathBuilder.evaluate(XPathBuilder.java:168) ~[bundlefile:2.11.2-sap-02]
at *** My project code calling from here ..
..........
-------
Caused by: com.sun.org.apache.xpath.internal.domapi.XPathStylesheetDOM3Exception: Prefix must resolve to a namespace: ns0
at com.sun.org.apache.xpath.internal.compiler.XPathParser.errorForDOM3(XPathParser.java:653) ~[na:1.6.0_33]
at com.sun.org.apache.xpath.internal.compiler.Lexer.mapNSTokens(Lexer.java:638) ~[na:1.6.0_33]
at com.sun.org.apache.xpath.internal.compiler.Lexer.tokenize(Lexer.java:265) ~[na:1.6.0_33]
at com.sun.org.apache.xpath.internal.compiler.Lexer.tokenize(Lexer.java:96) ~[na:1.6.0_33]
at com.sun.org.apache.xpath.internal.compiler.XPathParser.initXPath(XPathParser.java:110) ~[na:1.6.0_33]
at com.sun.org.apache.xpath.internal.XPath.<init>(XPath.java:176) ~[na:1.6.0_33]
at com.sun.org.apache.xpath.internal.XPath.<init>(XPath.java:264) ~[na:1.6.0_33]
at com.sun.org.apache.xpath.internal.jaxp.XPathImpl.compile(XPathImpl.java:385) ~[na:1.6.0_33]

Upon digging; I found that .. I need to set namespaces to XPathExpression object .. So, I need list of all namespace.
I am using blueprint beans.xml .. so I need list of all namespaces defined with blueprint tag in beans.xml.

I have camel exchange and camel endpoint object with me.
I want programmatic solution.

Thanks,
Nurali 

Re: Namespace list - blueprint - xpath

Posted by Aki Yoshida <el...@gmail.com>.
I don't know what you are trying to do actually after reading your response.

If what you want to do is to pick up the node
/ns0:userResponse/User/id where prefix ns0 is mapped to a certain
namespace uri which you know, say urn:whichiknow you can just set it
in the map using an arbitrary prefix like ns9=>urn:whichiknow and look
up the node /ns9:userResponse/User/id.

regards, aki

2013/11/1 Nurali Techie <nu...@yahoo.com>:
> Hi All,
>
> I have got the solution .. :)
>
> I have used xpath expression to get all namespace.  Here is the part of
> code:
>
>         String searchAllNamespacesXpathExpr = "//namespace::*";    // xpath
> expr to search all namespace
>         XPathExpression nsXpath = new
> XPathExpression(searchAllNamespacesXpathExpr);
>         nsXpath.setResultType(NodeList.class);
>         NodeList nsNodeList = (NodeList) nsXpath.evaluate(exchange,
> Object.class);  // this NodeList contains all namespaces
>
> From NodeList object, I prepare namespace Map which I set to XpathExpression
> object.
>
>         XPathExpression xpathExprObj = new
> XPathExpression("/ns0:userResponse/User/id)");
>         xpathExprObj.setNamespaces(namespaceMap);     // set namespace Map
> which was prepared from NodeList
>         xpathExprObj.setResultType(String.class);
>         Object result = xpathExprObj.evaluate(exchange, Object.class);
>
> Now, xpath with namespace (i.e /ns0:userResponse/User/id) will be evaluated
> correctly.
>
> Thanks Aki and All for you help.
>
> Let me know; in case you see any flaw in above way.
>
> Cheers,
> Nurali
>
>
> On Friday, November 1, 2013 2:50 PM, Nurali Techie <nu...@yahoo.com>
> wrote:
> Thanks Aki for your thoughts ..
>
> Yes, I should call setNamespaces() .. but I need namespace Map which I can
> pass to ..
> So, my question remain same .. is there any API or service .. which I can
> use to get Namespace Map ..
> And I hv Camel Exchange and Camel Endpoint object with me ..
>
> Parsing blueprint beans.xml and to extract Namespace from <blueprint> tag;
> looks hack and dirty way ..
> Also, I don't know how can I get beans.xml InputStream to parse for ..
>
> I also debugged and found that .. for standard <xpath> tag having namespace
> xpath in beans.xml .. the blueprint container do magic to pass all namespace
> while creating xpath expression object.  It means; namespace list is with
> blueprint container .. but I don't know how I can reached to there when I
> only have Camel Exchange and Endpoint object with me.
>
> Thanks,
> Nurali
>
>
>
> On Thursday, October 31, 2013 9:24 PM, Aki Yoshida <el...@gmail.com>
> wrote:
>
> for the first part of the question, maybe you forgot the set the
> namespace context (the prefix->nsuri map) in the expression?
>
> setNamespaces(Map<String,String> namespaces)
>
> for the second part, you probably need to parse the file that will
> scan all the namespaces declared and you will see them in the
> corresponding namespace declaration handler.
>
>
> 2013/10/31 Nurali Techie <nu...@yahoo.com>:
>> Hi Friends,
>>
>> I want to execute xpath in my code.  It means; I have xpath expression, I
>> have exchange object with In Message.
>>
>> Using 'XPathExpression' and 'XPathBuilder' classes; I succeed to do so.
>> Below is the code snippet.
>>
>>         XPathExpression xpathExprObj = new
>> XPathExpression("/userResponse/User/id");
>>         xpathExprObj.setResultType(String.class);
>>
>>         Object result = xpathExprObj.evaluate(exchange, Object.class);
>>
>> But, if I have xpath with namespace (i.e xpath =
>> /ns0:userResponse/User/id).. above code throwing exception .. saying -
>> Prefix must resolve to a namespace: ns0
>>
>> Part of Exception:
>>
>> org.apache.camel.builder.xml.InvalidXPathExpression: Invalid xpath:
>> /ns0:userResponse/User/id. Reason: javax.xml.xpath.XPathExpressionException
>> at
>> org.apache.camel.builder.xml.XPathBuilder.evaluateAs(XPathBuilder.java:767)
>> ~[bundlefile:2.11.2-sap-02]
>> at
>> org.apache.camel.builder.xml.XPathBuilder.evaluate(XPathBuilder.java:748)
>> ~[bundlefile:2.11.2-sap-02]
>> at
>> org.apache.camel.builder.xml.XPathBuilder.evaluate(XPathBuilder.java:168)
>> ~[bundlefile:2.11.2-sap-02]
>> at *** My project code calling from here ..
>> ..........
>> -------
>> Caused by:
>> com.sun.org.apache.xpath.internal.domapi.XPathStylesheetDOM3Exception:
>> Prefix must resolve to a namespace: ns0
>> at
>> com.sun.org.apache.xpath.internal.compiler.XPathParser.errorForDOM3(XPathParser.java:653)
>> ~[na:1.6.0_33]
>> at
>> com.sun.org.apache.xpath.internal.compiler.Lexer.mapNSTokens(Lexer.java:638)
>> ~[na:1.6.0_33]
>> at
>> com.sun.org.apache.xpath.internal.compiler.Lexer.tokenize(Lexer.java:265)
>> ~[na:1.6.0_33]
>> at
>> com.sun.org.apache.xpath.internal.compiler.Lexer.tokenize(Lexer.java:96)
>> ~[na:1.6.0_33]
>> at
>> com.sun.org.apache.xpath.internal.compiler.XPathParser.initXPath(XPathParser.java:110)
>> ~[na:1.6.0_33]
>> at com.sun.org.apache.xpath.internal.XPath.<init>(XPath.java:176)
>> ~[na:1.6.0_33]
>> at com.sun.org.apache.xpath.internal.XPath.<init>(XPath.java:264)
>> ~[na:1.6.0_33]
>> at
>> com.sun.org.apache.xpath.internal.jaxp.XPathImpl.compile(XPathImpl.java:385)
>> ~[na:1.6.0_33]
>>
>> Upon digging; I found that .. I need to set namespaces to XPathExpression
>> object .. So, I need list of all namespace.
>> I am using blueprint beans.xml .. so I need list of all namespaces defined
>> with blueprint tag in beans.xml.
>>
>> I have camel exchange and camel endpoint object with me.
>> I want programmatic solution.
>>
>> Thanks,
>> Nurali
>
>

Re: Namespace list - blueprint - xpath

Posted by Nurali Techie <nu...@yahoo.com>.
Hi All,

I have got the solution .. :)

I have used xpath expression to get all namespace.  Here is the part of code:

        String searchAllNamespacesXpathExpr = "//namespace::*";    // xpath expr to search all namespace
        XPathExpression nsXpath = new XPathExpression(searchAllNamespacesXpathExpr);
        nsXpath.setResultType(NodeList.class);
        NodeList nsNodeList = (NodeList) nsXpath.evaluate(exchange, Object.class);  // this NodeList contains all namespaces

From NodeList object, I prepare namespace Map which I set to XpathExpression object.

        XPathExpression xpathExprObj = new XPathExpression("/ns0:userResponse/User/id)");
        xpathExprObj.setNamespaces(namespaceMap);     // set namespace Map which was prepared from NodeList
        xpathExprObj.setResultType(String.class);
        Object result = xpathExprObj.evaluate(exchange, Object.class);


Now, xpath with namespace (i.e /ns0:userResponse/User/id) will be evaluated correctly.

Thanks Aki and All for you help.

Let me know; in case you see any flaw in above way.

Cheers,
Nurali



On Friday, November 1, 2013 2:50 PM, Nurali Techie <nu...@yahoo.com> wrote:
 
Thanks Aki for your thoughts ..

Yes, I should call setNamespaces() .. but I need namespace Map which I can pass to ..
So, my question remain same .. is there any API or service .. which I can use to get Namespace Map ..
And I hv Camel Exchange and Camel Endpoint object with me ..

Parsing blueprint beans.xml and to extract Namespace from <blueprint> tag; looks hack and dirty way ..
Also, I don't know how can I get beans.xml InputStream to parse for .. 

I also debugged and found that .. for standard <xpath> tag having namespace xpath in beans.xml .. the blueprint container do magic to pass all namespace while creating xpath expression object.  It means; namespace list is with blueprint container .. but I don't know how I can reached to there when I only have Camel Exchange and Endpoint object with me.

Thanks,
Nurali




On Thursday, October 31, 2013 9:24 PM, Aki Yoshida <el...@gmail.com> wrote:

for the first part of the question, maybe you forgot the set the
namespace context (the prefix->nsuri map) in the expression?

setNamespaces(Map<String,String> namespaces)

for the second part, you probably need to parse the file that will
scan all the namespaces declared and you will see them in the
corresponding namespace declaration handler.


2013/10/31 Nurali Techie <nu...@yahoo.com>:
> Hi Friends,
>
> I want to execute xpath in my code.  It means; I have xpath expression, I have exchange object with In Message.
>
> Using 'XPathExpression' and 'XPathBuilder' classes; I succeed to do so.  Below is the code snippet.
>
>         XPathExpression xpathExprObj = new XPathExpression("/userResponse/User/id");
>         xpathExprObj.setResultType(String.class);
>
>         Object result = xpathExprObj.evaluate(exchange, Object.class);
>
> But, if I have xpath with namespace (i.e xpath = /ns0:userResponse/User/id).. above code throwing exception .. saying - Prefix must resolve to a namespace: ns0
>
> Part of Exception:
>
> org.apache.camel.builder.xml.InvalidXPathExpression: Invalid xpath: /ns0:userResponse/User/id. Reason: javax.xml.xpath.XPathExpressionException
> at org.apache.camel.builder.xml.XPathBuilder.evaluateAs(XPathBuilder.java:767) ~[bundlefile:2.11.2-sap-02]
> at org.apache.camel.builder.xml.XPathBuilder.evaluate(XPathBuilder.java:748) ~[bundlefile:2.11.2-sap-02]
> at org.apache.camel.builder.xml.XPathBuilder.evaluate(XPathBuilder.java:168) ~[bundlefile:2.11.2-sap-02]
> at *** My project code calling from here ..
> ..........
> -------
> Caused by: com.sun.org.apache.xpath.internal.domapi.XPathStylesheetDOM3Exception: Prefix must resolve to a namespace: ns0
> at com.sun.org.apache.xpath.internal.compiler.XPathParser.errorForDOM3(XPathParser.java:653) ~[na:1.6.0_33]
> at com.sun.org.apache.xpath.internal.compiler.Lexer.mapNSTokens(Lexer.java:638) ~[na:1.6.0_33]
> at com.sun.org.apache.xpath.internal.compiler.Lexer.tokenize(Lexer.java:265) ~[na:1.6.0_33]
> at com.sun.org.apache.xpath.internal.compiler.Lexer.tokenize(Lexer.java:96) ~[na:1.6.0_33]
> at com.sun.org.apache.xpath.internal.compiler.XPathParser.initXPath(XPathParser.java:110) ~[na:1.6.0_33]
> at com.sun.org.apache.xpath.internal.XPath.<init>(XPath.java:176) ~[na:1.6.0_33]
> at com.sun.org.apache.xpath.internal.XPath.<init>(XPath.java:264) ~[na:1.6.0_33]
> at com.sun.org.apache.xpath.internal.jaxp.XPathImpl.compile(XPathImpl.java:385) ~[na:1.6.0_33]
>
> Upon digging; I found that .. I need to set namespaces to XPathExpression object .. So, I need list of all namespace.
> I am using blueprint beans.xml .. so I need list of all namespaces defined with blueprint tag in beans.xml.
>
> I have camel exchange and camel endpoint object with me.
> I want programmatic solution.
>
> Thanks,
> Nurali

Re: Namespace list - blueprint - xpath

Posted by Nurali Techie <nu...@yahoo.com>.
Thanks Aki for your thoughts ..

Yes, I should call setNamespaces() .. but I need namespace Map which I can pass to ..
So, my question remain same .. is there any API or service .. which I can use to get Namespace Map ..
And I hv Camel Exchange and Camel Endpoint object with me ..

Parsing blueprint beans.xml and to extract Namespace from <blueprint> tag; looks hack and dirty way ..
Also, I don't know how can I get beans.xml InputStream to parse for .. 

I also debugged and found that .. for standard <xpath> tag having namespace xpath in beans.xml .. the blueprint container do magic to pass all namespace while creating xpath expression object.  It means; namespace list is with blueprint container .. but I don't know how I can reached to there when I only have Camel Exchange and Endpoint object with me.

Thanks,
Nurali



On Thursday, October 31, 2013 9:24 PM, Aki Yoshida <el...@gmail.com> wrote:
 
for the first part of the question, maybe you forgot the set the
namespace context (the prefix->nsuri map) in the expression?

setNamespaces(Map<String,String> namespaces)

for the second part, you probably need to parse the file that will
scan all the namespaces declared and you will see them in the
corresponding namespace declaration handler.


2013/10/31 Nurali Techie <nu...@yahoo.com>:
> Hi Friends,
>
> I want to execute xpath in my code.  It means; I have xpath expression, I have exchange object with In Message.
>
> Using 'XPathExpression' and 'XPathBuilder' classes; I succeed to do so.  Below is the code snippet.
>
>         XPathExpression xpathExprObj = new XPathExpression("/userResponse/User/id");
>         xpathExprObj.setResultType(String.class);
>
>         Object result = xpathExprObj.evaluate(exchange, Object.class);
>
> But, if I have xpath with namespace (i.e xpath = /ns0:userResponse/User/id).. above code throwing exception .. saying - Prefix must resolve to a namespace: ns0
>
> Part of Exception:
>
> org.apache.camel.builder.xml.InvalidXPathExpression: Invalid xpath: /ns0:userResponse/User/id. Reason: javax.xml.xpath.XPathExpressionException
> at org.apache.camel.builder.xml.XPathBuilder.evaluateAs(XPathBuilder.java:767) ~[bundlefile:2.11.2-sap-02]
> at org.apache.camel.builder.xml.XPathBuilder.evaluate(XPathBuilder.java:748) ~[bundlefile:2.11.2-sap-02]
> at org.apache.camel.builder.xml.XPathBuilder.evaluate(XPathBuilder.java:168) ~[bundlefile:2.11.2-sap-02]
> at *** My project code calling from here ..
> ..........
> -------
> Caused by: com.sun.org.apache.xpath.internal.domapi.XPathStylesheetDOM3Exception: Prefix must resolve to a namespace: ns0
> at com.sun.org.apache.xpath.internal.compiler.XPathParser.errorForDOM3(XPathParser.java:653) ~[na:1.6.0_33]
> at com.sun.org.apache.xpath.internal.compiler.Lexer.mapNSTokens(Lexer.java:638) ~[na:1.6.0_33]
> at com.sun.org.apache.xpath.internal.compiler.Lexer.tokenize(Lexer.java:265) ~[na:1.6.0_33]
> at com.sun.org.apache.xpath.internal.compiler.Lexer.tokenize(Lexer.java:96) ~[na:1.6.0_33]
> at com.sun.org.apache.xpath.internal.compiler.XPathParser.initXPath(XPathParser.java:110) ~[na:1.6.0_33]
> at com.sun.org.apache.xpath.internal.XPath.<init>(XPath.java:176) ~[na:1.6.0_33]
> at com.sun.org.apache.xpath.internal.XPath.<init>(XPath.java:264) ~[na:1.6.0_33]
> at com.sun.org.apache.xpath.internal.jaxp.XPathImpl.compile(XPathImpl.java:385) ~[na:1.6.0_33]
>
> Upon digging; I found that .. I need to set namespaces to XPathExpression object .. So, I need list of all namespace.
> I am using blueprint beans.xml .. so I need list of all namespaces defined with blueprint tag in beans.xml.
>
> I have camel exchange and camel endpoint object with me.
> I want programmatic solution.
>
> Thanks,
> Nurali

Re: Namespace list - blueprint - xpath

Posted by Aki Yoshida <el...@gmail.com>.
for the first part of the question, maybe you forgot the set the
namespace context (the prefix->nsuri map) in the expression?

setNamespaces(Map<String,String> namespaces)

for the second part, you probably need to parse the file that will
scan all the namespaces declared and you will see them in the
corresponding namespace declaration handler.

2013/10/31 Nurali Techie <nu...@yahoo.com>:
> Hi Friends,
>
> I want to execute xpath in my code.  It means; I have xpath expression, I have exchange object with In Message.
>
> Using 'XPathExpression' and 'XPathBuilder' classes; I succeed to do so.  Below is the code snippet.
>
>         XPathExpression xpathExprObj = new XPathExpression("/userResponse/User/id");
>         xpathExprObj.setResultType(String.class);
>
>         Object result = xpathExprObj.evaluate(exchange, Object.class);
>
> But, if I have xpath with namespace (i.e xpath = /ns0:userResponse/User/id).. above code throwing exception .. saying - Prefix must resolve to a namespace: ns0
>
> Part of Exception:
>
> org.apache.camel.builder.xml.InvalidXPathExpression: Invalid xpath: /ns0:userResponse/User/id. Reason: javax.xml.xpath.XPathExpressionException
> at org.apache.camel.builder.xml.XPathBuilder.evaluateAs(XPathBuilder.java:767) ~[bundlefile:2.11.2-sap-02]
> at org.apache.camel.builder.xml.XPathBuilder.evaluate(XPathBuilder.java:748) ~[bundlefile:2.11.2-sap-02]
> at org.apache.camel.builder.xml.XPathBuilder.evaluate(XPathBuilder.java:168) ~[bundlefile:2.11.2-sap-02]
> at *** My project code calling from here ..
> ..........
> -------
> Caused by: com.sun.org.apache.xpath.internal.domapi.XPathStylesheetDOM3Exception: Prefix must resolve to a namespace: ns0
> at com.sun.org.apache.xpath.internal.compiler.XPathParser.errorForDOM3(XPathParser.java:653) ~[na:1.6.0_33]
> at com.sun.org.apache.xpath.internal.compiler.Lexer.mapNSTokens(Lexer.java:638) ~[na:1.6.0_33]
> at com.sun.org.apache.xpath.internal.compiler.Lexer.tokenize(Lexer.java:265) ~[na:1.6.0_33]
> at com.sun.org.apache.xpath.internal.compiler.Lexer.tokenize(Lexer.java:96) ~[na:1.6.0_33]
> at com.sun.org.apache.xpath.internal.compiler.XPathParser.initXPath(XPathParser.java:110) ~[na:1.6.0_33]
> at com.sun.org.apache.xpath.internal.XPath.<init>(XPath.java:176) ~[na:1.6.0_33]
> at com.sun.org.apache.xpath.internal.XPath.<init>(XPath.java:264) ~[na:1.6.0_33]
> at com.sun.org.apache.xpath.internal.jaxp.XPathImpl.compile(XPathImpl.java:385) ~[na:1.6.0_33]
>
> Upon digging; I found that .. I need to set namespaces to XPathExpression object .. So, I need list of all namespace.
> I am using blueprint beans.xml .. so I need list of all namespaces defined with blueprint tag in beans.xml.
>
> I have camel exchange and camel endpoint object with me.
> I want programmatic solution.
>
> Thanks,
> Nurali