これは私がそれを行う方法です。兄弟の 1 つとまったく同じ属性を持っている場合でも、要素ごとに完全に一意の XPath を取得できるように、位置も含めていることに注意してください。
XML 入力
<Element0 AttributeA="A">
<Element1 AttributeB="1" AttributeC="C" AttributeD="D">
<Element2>nodeValue</Element2>
</Element1>
<Element1 AttributeB="2" AttributeC="C" AttributeD="D">
<Element2>nodeValue</Element2>
<Element3 AttributeE="E">
<Element4 AttributeF="F">nodeValue</Element4>
</Element3>
</Element1>
</Element0>
XSLT1.0
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:strip-space elements="*"/>
<xsl:template match="text()"/>
<xsl:template match="*">
<xsl:for-each select="ancestor-or-self::*">
<xsl:value-of select="concat('/',local-name())"/>
<xsl:value-of select="concat('[',count(preceding-sibling::*[local-name()=local-name(current())])+1,']')"/>
<xsl:if test="@*">
<xsl:text>[</xsl:text>
<xsl:apply-templates select="@*"/>
<xsl:text>]</xsl:text>
</xsl:if>
</xsl:for-each>
<xsl:text>
</xsl:text>
<xsl:apply-templates select="node()"/>
</xsl:template>
<xsl:template match="@*">
<xsl:if test="position() != 1">
<xsl:text> and </xsl:text>
</xsl:if>
<xsl:value-of select="concat('@',local-name(),'="',.,'"')"/>
</xsl:template>
</xsl:stylesheet>
出力
/Element0[1][@AttributeA="A"]
/Element0[1][@AttributeA="A"]/Element1[1][@AttributeB="1" and @AttributeC="C" and @AttributeD="D"]
/Element0[1][@AttributeA="A"]/Element1[1][@AttributeB="1" and @AttributeC="C" and @AttributeD="D"]/Element2[1]
/Element0[1][@AttributeA="A"]/Element1[2][@AttributeB="2" and @AttributeC="C" and @AttributeD="D"]
/Element0[1][@AttributeA="A"]/Element1[2][@AttributeB="2" and @AttributeC="C" and @AttributeD="D"]/Element2[1]
/Element0[1][@AttributeA="A"]/Element1[2][@AttributeB="2" and @AttributeC="C" and @AttributeD="D"]/Element3[1][@AttributeE="E"]
/Element0[1][@AttributeA="A"]/Element1[2][@AttributeB="2" and @AttributeC="C" and @AttributeD="D"]/Element3[1][@AttributeE="E"]/Element4[1][@AttributeF="F"]