I think the following correction only matters in unusual cases where different prefixes are used for the same namespaces, or different namespaces for the same prefix, among sibling elements in a document. However there is nothing theoretically wrong with such input, and it could be common in certain kinds of generated XML.
Anyway, the following answer fixes that case (copied-and-modified from @Kirill's answer):
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" indent="no" />
<xsl:template match="*[not(*)]">
<xsl:for-each select="ancestor-or-self::*">
<xsl:value-of select="concat('/', name())"/>
<!-- Suggestions on how to refactor the repetition of long XPath
expression parts are welcome. -->
<xsl:if test="count(../*[local-name() = local-name(current())
and namespace-uri(.) = namespace-uri(current())]) > 1">
<xsl:value-of select="concat('[', count(
preceding-sibling::*[local-name() = local-name(current())
and namespace-uri(.) = namespace-uri(current())]) + 1, ']')"/>
</xsl:if>
</xsl:for-each>
<xsl:text>
</xsl:text>
<xsl:apply-templates select="*"/>
</xsl:template>
<xsl:template match="*">
<xsl:apply-templates select="*"/>
</xsl:template>
</xsl:stylesheet>
It also addresses the problem in other answers where elements that are first in a series of siblings lack a position predicate.
E.g. for the input
<root>
<item1>value1</item1>
<subitem>
<a:item xmlns:a="uri">value2</a:item>
<b:item xmlns:b="uri">value3</b:item>
</subitem>
</root>
this answer produces
/root/item1
/root/subitem/a:item[1]
/root/subitem/b:item[2]
which is correct.
However, like all XPath expressions, these will only work if the environment using them specifies correct bindings for the namespace prefixes used. In theory there can be more pathological documents for which the above answer generates XPath expressions that can never work (in XPath 1.0 at least) regardless of the prefix bindings. E.g. this input:
<root>
<item1>value1</item1>
<a:subitem xmlns:a="differentURI">
<a:item xmlns:a="uri">value2</a:item>
<b:item xmlns:b="uri">value3</b:item>
</a:subitem>
</root>
produces the output
/root/item1
/root/a:subitem/a:item[1]
/root/a:subitem/b:item[2]
But the second XPath expression here can never work, since the prefix a
refers to two different namespaces in the same expression.