The following XPath 2.0 expression :
//file/concat(string-join(ancestor::*[parent::*]
/concat(name(.), '/'),
''),
@name, '
'
)
when evaluating the provided XML document :
<xml>
<home>
<mysite>
<www>
<images>
<file name="logo.gif"/>
</images>
<file name="index.html"/>
<file name="about_us.html"/>
</www>
</mysite>
</home>
</xml>
creates the desired, correct result :
home/mysite/www/images/logo.gif
home/mysite/www/index.html
home/mysite/www/about_us.html
If you cannot use XPath 2.0, it is not possible to get the desired result with only XPath 1.0 expression .
, XPath (, XSLT, #, php,...).
XSLT 1.0:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:template match="file">
<xsl:for-each select="ancestor::*[parent::*]">
<xsl:value-of select="concat(name(),'/')"/>
</xsl:for-each>
<xsl:value-of select="concat(@name, '
')"/>
</xsl:template>
<xsl:template match="text()"/>
</xsl:stylesheet>
, XML-, :
home/mysite/www/images/logo.gif
home/mysite/www/index.html
home/mysite/www/about_us.html