這裏的(的可疑值)樣式表打印到每個元素和屬性在文檔中的路徑:
<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>
當施加到該輸入:
<people>
<person>
<name first="betty" last="jones" />
</person>
<person>
<name first="alan" last="smith" />
</person>
<singleElement />
</people>
產地:
/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
[列出XML文件中的每個節點。](http://stackoverflow.com/questions/4051987/list-every-node-in-an-xml-file)或[HTML XPa th - 如何獲取基於另一個元素的值的元素的XPath?](http://stackoverflow.com/questions/5227330/html-xpath-how-to-get-the-xpath-of-an- element-based-on-the-value-of-ele-ele) – 2011-04-17 20:42:08