Here is a stylesheet (of dubious value) that prints the path to each element and attribute in the document:
<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="*"> <xsl:param name="pathToHere" select="''" /> <xsl:variable name="precSiblings" select="count(preceding-sibling::*[name()=name(current())])" /> <xsl:variable name="follSiblings" select="count(following-sibling::*[name()=name(current())])" /> <xsl:variable name="fullPath" select="concat($pathToHere, '/', name(), substring(concat('[', $precSiblings + 1, ']'), 1 div ($follSiblings or $precSiblings)))" /> <xsl:value-of select="concat($fullPath, '
')" /> <xsl:apply-templates select="@*|*"> <xsl:with-param name="pathToHere" select="$fullPath" /> </xsl:apply-templates> </xsl:template> <xsl:template match="@*"> <xsl:param name="pathToHere" select="''" /> <xsl:value-of select="concat($pathToHere, '/@', name(), '
')" /> </xsl:template> </xsl:stylesheet>
When applied to this input:
<people> <person> <name first="betty" last="jones" /> </person> <person> <name first="alan" last="smith" /> </person> <singleElement /> </people>
It produces:
/people /people/person[1] /people/person[1]/name /people/person[1]/name/@first /people/person[1]/name/@last /people/person[2] /people/person[2]/name /people/person[2]/name/@first /people/person[2]/name/@last /people/singleElement
source share