Xpather is a project mainly written in Java, it's free.
DSL for generating xpath
Xpather is a simple DSL for generating xpath expressions, including specific support for creating xpath expression for xHtml
import static xpather.XpathExpression.*;
...
element("Library").toXpath();
Will return the xpath expression
/Library
import static xpather.XpathExpression.*;
...
element("Library").with(any(element("Book"))).toXpath();
Will return the xpath expression
/Library//Book
import static xpather.XpathExpression.*;
...
any(element("book")).with(element("price").equalTo("35.00")).toXpath();
Will return the xpath expression
/Library//Book
import static xpather.XpathExpression.*;
...
any(element("Book")).with(attribute("language")).toXpath();
Will return the xpath expression
//Book/@language
import static xpather.XpathExpression.*;
...
any(element("Book")).with(attribute("language").containing("english")).toXpath();
Will return the xpath expression
//Book[@language='english']
import static xpather.XpathExpression.*;
...
any(div().with(id("myId"))).toXpath();
Will return the xpath expression //div[@id='myId']
import static xpather.XpathExpression.*;
...
any(div().with(css("myClass"))).toXpath();
Will return the xpath expression
//div[@class='myClass']
import static xpather.XpathExpression.*;
...
any(div().with(css(containing("myClass")))).toXpath();
Will return the xpath expression
//div[contains(@class, 'myClass')]
import static xpather.XpathExpression.*;
...
any(div().with(ul().with(li().atIndex(2)))).toXpath();
Will return the xpath expression
//div/ul/li[2]