What I think you should do is first try to get the preceding sibling part of the formula right. When done correctly, you should have basically a row counter in the milestone id field, so I'd think from your data source:
count(../preceding-sibling::d:msdbMilestone1/@MilestoneDetaiID1) + 1
After you have that working, and basically have a row counter, then you can work on getting the text into it, and finally, if you need it to be conditional, add that last....