I'm working on a blog post that includes a mention of relative XPath, and rather than just continue adding info to that post (thus possibly creating the longest blog entry ever in the history of the world), I thought I'd include this tip on its own.
When I am unsure of the XPath I need for an expression, I leverage InfoPath, and you can too. Here's how:
-
Find the field or group you need to get the relative path from:

-
Add a rule to it - I usually add a conditional formatting rule, but since we are going to delete the rule when we are done, it really doesn't matter what type of rule you use.
-
Create a condition - for the first drop down, select the field you need the relative path to:

-
Change the first drop down to "The expression":

-
VoilĂ ! You have a relative XPath!

Now you can just delete your rule. For more complex expressions, you can use the third drop down, and select "Use a formula" - and check the Edit Xpath box in the formula editor. Get in the habit at looking at the actual XPath InfoPath is using, and you'll be surprised how much XPath knowledge you pick up.