In all cases of infants or persons of unsound mind where no legal representative or guardian appears in their behalf at the hearing, it shall be the duty of the court to appoint a guardian ad litem who shall represent their interest for all purposes.