How to use a substring of a string in a JXPath expression

271 views Asked by At

I have a list of java objects that contain flight numbers and gate numbers:

public class Flight {
  public String flightNumber;
  public String gateNumber;
}

For the purposes of this question, my list contains objects with the following flight numbers and gate numbers:

"AA123"  "10"
"BB789"  "11"
"BB124"  "10"
"AA456"  "12"

I know that if I want to select all objects at gate 10, I can use the following JXPath expression (which works):

.[gateNumber = "10"]

What I need to do is select all objects at gate 10 with a flight number that starts with "AA".

I've tried the following, but none of them work:

.[gateNumber = "10" and flightNumber.startsWith("AA")]
.[gateNumber = "10" and flightNumber.substring(0, 2) = "AA"]
.[gateNumber = "10" and substring(flightNumber, 0, 2) = "AA"]
.[gateNumber = "10" and substring($flightNumber, 0, 2) = "AA"]

What should my JXPath expression be? Is it even possible without writing a custom extension?

1

There are 1 answers

0
Jason On BEST ANSWER

I could not find a way using native JXPath, so in the end I had to develop an extension that returned true if the contents of the first string parameter started with the contents of the second string parameter:

.[gateNumber = "10" and utils:startsWith(flightNumber, "AA")]

Edit

I haven't tried it, but apparently JXPath supports the starts-with() function from XPath.