You may want to have a look at this answer:
https://stackoverflow.com/a/4747858/36305
It demonstrates a way, using XSLT, to build an XPath expression for selecting every node of a given XML document and even generating an XPath expression for comparing the values:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:variable name="vApos">'</xsl:variable>
<xsl:template match="*[@* or not(*)] ">
<xsl:if test="not(*)">
<xsl:apply-templates select="ancestor-or-self::*" mode="path"/>
<xsl:value-of select="concat('=',$vApos,.,$vApos)"/>
<xsl:text>
</xsl:text>
</xsl:if>
<xsl:apply-templates select="@*|*"/>
</xsl:template>
<xsl:template match="*" mode="path">
<xsl:value-of select="concat('/',name())"/>
<xsl:variable name="vnumPrecSiblings" select=
"count(preceding-sibling::*[name()=name(current())])"/>
<xsl:if test="$vnumPrecSiblings">
<xsl:value-of select="concat('[', $vnumPrecSiblings +1, ']')"/>
</xsl:if>
</xsl:template>
<xsl:template match="@*">
<xsl:apply-templates select="../ancestor-or-self::*" mode="path"/>
<xsl:value-of select="concat('[@',name(), '=',$vApos,.,$vApos,']')"/>
<xsl:text>
</xsl:text>
</xsl:template>
</xsl:stylesheet>
When applied on the provided input (wrapped in a top-element to become well-formed):
<t>
<abc>
<aaa>123</aaa>
</abc>
<abc>
<aaa>123</aaa>
</abc>
</t>
the result is a set of XPath expressions selecting each of the elements that have a single text child, and comparing the string value of the element and the child:
/t/abc/aaa='123'
/t/abc[2]/aaa='123'
This also generates expressions, that involve comparisons of attribute values, and whose Boolean value is true()
exactly when the attribute value comparison is true.
For example, if the provided XML document was this:
<t>
<abc x="1" y="2">
<aaa>123</aaa>
</abc>
<abc z="3">
<aaa>123</aaa>
</abc>
</t>
then the result of the transformation is:
/t/abc[@x='1']
/t/abc[@y='2']
/t/abc/aaa='123'
/t/abc[2][@z='3']
/t/abc[2]/aaa='123'