If you mean by not knowing the previous node that you want to check all nodes that may have a selected
attribute I think you just have to change your XPath expression to:
count(/process_data/formData/xdp/datasets/data/Data//*[@selected='true'])
This assumes that selected
is actually a string attribute.