2

I am having some trouble using xpath to extract the "Payload" values below using apache-camel. I use the below xpath in my route for both of the example xml, the first example xml returns SomeElement and SomeOtherElement as expected, but the second xml seems unable to parse the xml at all.

xpath("//Payload/*")

This example xml parses just fine.

<Message>
  <Payload>
    <SomeElement />
    <SomeOtherElement />
  </Payload>
</Message>

This example xml does not parse.

<Message xmlns="http://www.fake.com/Message/1">
  <Payload>
    <SomeElement />
    <SomeOtherElement />
  </Payload>
</Message>

I found a similar question about xml and xpath, but it deals with C# and is not a camel solution.

Any idea how to solve this using apache-camel?

2
  • Does this work - xpath("//{http://www.fake.com/Message/1}Payload/*") Commented Jul 15, 2015 at 15:57
  • What makes you think the second XML doesn't parse? Maybe you mean that your XPath expression selected nothing? Those are two entirely different things. Commented Jul 15, 2015 at 20:19

1 Answer 1

5

Your 2nd example xml, specifies a default namespace: xmlns="http://www.fake.com/Message/1" and so your xpath expression will not match, as it specifies no namespace.

See http://camel.apache.org/xpath.html#XPath-Namespaces on how to specify a namespace.

You would need something like

Namespaces ns = new Namespaces("fk", "http://www.fake.com/Message/1");

xpath("//fk:Payload/*", ns)

I'm not familiar with Apache-Camel, this was just a result of some quick googling.

An alternative maybe to just change your xPath to something like

xpath("//*[local-name()='Payload']/*)

Good luck.

Sign up to request clarification or add additional context in comments.

1 Comment

xpath("//*[local-name()='Payload']/*) This seems to have done the trick. Unfortunately it means wrapping all my xpath expressions in this, but I am in a better state than I was before.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.