Hi,
You can simply iterate on the ancestor or self axes and output the
XPath expressin matching the current element. Below you can find a
sample stylesheet that outputs the XPath expressions matching all the
document nodes.
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes"/>
<xsl:template match="/">
<xpaths>
<xsl:apply-templates/>
</xpaths>
</xsl:template>
<xsl:template match="*">
<xpath>
<xsl:for-each select="ancestor-or-self::*">
<xsl:value-of select="name()"/>
<xsl:variable name="count">
<xsl:number/>
</xsl:variable>
<xsl:text>[</xsl:text>
<xsl:value-of select="$count"/>
<xsl:text>]</xsl:text>
<xsl:if test="position()!=last()">/</xsl:if>
</xsl:for-each>
</xpath>
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="text()"/>
</xsl:stylesheet>
On something like
<?xml version="1.0" encoding="UTF-8"?>
<test>
<a> </a>
<b/>
<a>
<c> </c>
<x> </x>
</a>
</test>
it will give you
<?xml version="1.0" encoding="UTF-8"?>
<xpaths>
<xpath>test[1]</xpath>
<xpath>test[1]/a[1]</xpath>
<xpath>test[1]/b[1]</xpath>
<xpath>test[1]/a[2]</xpath>
<xpath>test[1]/a[2]/c[1]</xpath>
<xpath>test[1]/a[2]/x[1]</xpath>
</xpaths>
Best Regards,
George
---------------------------------------------------------------------
George Cristian Bina
<oXygen/> XML Editor, Schema Editor and XSLT Editor/Debugger
http://www.oxygenxml.com