只是为了好玩,没有扩展功能。
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="text()"/>
<xsl:template match="*|@*">
<xsl:param name="pPath"/>
<xsl:param name="pNames" select="'
'"/>
<xsl:variable name="vPath"
select="concat($pPath,'/',
substring('@',
1 div (count(.|../@*) =
count(../@*))),
name())"/>
<xsl:variable name="vNames">
<xsl:if test="not(contains($pNames,
concat('
',$vPath,'
')))">
<xsl:value-of select="concat($vPath,'
')"/>
</xsl:if>
<xsl:apply-templates select="*[1]|@*">
<xsl:with-param name="pPath" select="$vPath"/>
<xsl:with-param name="pNames" select="$pNames"/>
</xsl:apply-templates>
</xsl:variable>
<xsl:value-of select="$vNames"/>
<xsl:apply-templates select="following-sibling::*[1]">
<xsl:with-param name="pPath" select="$pPath"/>
<xsl:with-param name="pNames" select="concat($pNames,$vNames)"/>
</xsl:apply-templates>
</xsl:template>
</xsl:stylesheet>
输出:
/root
/root/@name
/root/child
/root/child/@id
/root/child/grandchild
编辑:XSLT/XPath 2.0 的更好示例。这个 XPath 2.0 行:
string-join(
distinct-values(
(//*|//@*)
/string-join(
(ancestor::node()/name(),
if (self::attribute())
then concat('@',name())
else name()),
'/')),
'
')