See attached - save the file locally, then right click the saved file and select Design to open in design mode.
I used an expression box for this in my sample instead of a field - I'd think given the structure you describe, the XPath would be:
count(../../preceding-sibling::my:RepeatingSection/my:OptionalSection) + 1