Have a look at the JXPath User's Guide. you can create an extension function that would take the query as a parameter.
public static NodeSet getCustomers(String query){
List<Customer> l=getCustomersFromMyDatabase(query);
BasicNodeSet bns=new BasicNodeSet();
putCustomersIntoNodeSet(bns,l);
return bns;
}
Your xpath would then look like
getCustomers('id=123')
You can have also a first parameter of type ExpressionContext, that can give you the context object if you need it, etc.