Attosecond and femtosecond beam shaping for nonlinear X-ray spectroscopy with X-ray free-electron laser