1

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?

Community
  • 1
  • 1
  • Does this work - `xpath("//{http://www.fake.com/Message/1}Payload/*")` – Anand S Kumar Jul 15 '15 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. – LarsH Jul 15 '15 at 20:19

1 Answers1

4

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.

ptha
  • 836
  • 1
  • 8
  • 22
  • 1
    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. – Dennis Crissman II Jul 15 '15 at 17:10