Xslt 带根的XSL递归链接树
这是一个XSLT代码:Xslt 带根的XSL递归链接树,xslt,xslt-1.0,Xslt,Xslt 1.0,这是一个XSLT代码: <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output omit-xml-declaration="yes"/> <xsl:template match="*[parent::*]"> <xsl:param name="pPath"/> <xsl:value-of select="$
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes"/>
<xsl:template match="*[parent::*]">
<xsl:param name="pPath"/>
<xsl:value-of select="$pPath"/>
<xsl:variable name="vValue" select="normalize-space(text()[1])"/>
<xsl:value-of select="$vValue"/>
<br/>
<xsl:apply-templates select="*">
<xsl:with-param name="pPath" select="concat($pPath, $vValue, ': ')"/>
</xsl:apply-templates>
</xsl:template>
<xsl:template match="text()"/>
我应该添加什么,以便在该树的每个元素上都有链接,例如:
<a href="1">1</a>
<a href="1">1</a> : <a href="1/1.1">1.1</a>
<a href="1">1</a> : <a href="1/1.2">1.2</a>
<a href="2">2</a>
<a href="2">2</a> : <a href="2/2.1">2.1</a>
<a href="2">2</a> : <a href="2/2.1">2.1</a> : <a href="2/2.1/2.1.1">2.1.1</a>
:
:
:
: :
等等。这样做不需要处理递归性:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes"/>
<xsl:template match="*[parent::*]">
<xsl:variable name="curr-id" select="generate-id()" />
<xsl:for-each select="ancestor-or-self::*[parent::*]">
<xsl:variable name="curr-sub-id" select="generate-id()" />
<a>
<xsl:attribute name="href">
<xsl:for-each select="ancestor-or-self::*[parent::*]">
<xsl:value-of select="normalize-space(text()[1])"/>
<xsl:if test="generate-id() != $curr-sub-id">
<xsl:text>/</xsl:text>
</xsl:if>
</xsl:for-each>
</xsl:attribute>
<xsl:value-of select="normalize-space(text()[1])"/>
</a>
<xsl:if test="generate-id() != $curr-id">
<xsl:text>:</xsl:text>
</xsl:if>
</xsl:for-each>
<br/>
<xsl:apply-templates select="*" />
</xsl:template>
</xsl:stylesheet>
:
这样做不需要处理递归性:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes"/>
<xsl:template match="*[parent::*]">
<xsl:variable name="curr-id" select="generate-id()" />
<xsl:for-each select="ancestor-or-self::*[parent::*]">
<xsl:variable name="curr-sub-id" select="generate-id()" />
<a>
<xsl:attribute name="href">
<xsl:for-each select="ancestor-or-self::*[parent::*]">
<xsl:value-of select="normalize-space(text()[1])"/>
<xsl:if test="generate-id() != $curr-sub-id">
<xsl:text>/</xsl:text>
</xsl:if>
</xsl:for-each>
</xsl:attribute>
<xsl:value-of select="normalize-space(text()[1])"/>
</a>
<xsl:if test="generate-id() != $curr-id">
<xsl:text>:</xsl:text>
</xsl:if>
</xsl:for-each>
<br/>
<xsl:apply-templates select="*" />
</xsl:template>
</xsl:stylesheet>
:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes"/>
<xsl:template match="*[parent::*]">
<xsl:variable name="curr-id" select="generate-id()" />
<xsl:for-each select="ancestor-or-self::*[parent::*]">
<xsl:variable name="curr-sub-id" select="generate-id()" />
<a>
<xsl:attribute name="href">
<xsl:for-each select="ancestor-or-self::*[parent::*]">
<xsl:value-of select="normalize-space(text()[1])"/>
<xsl:if test="generate-id() != $curr-sub-id">
<xsl:text>/</xsl:text>
</xsl:if>
</xsl:for-each>
</xsl:attribute>
<xsl:value-of select="normalize-space(text()[1])"/>
</a>
<xsl:if test="generate-id() != $curr-id">
<xsl:text>:</xsl:text>
</xsl:if>
</xsl:for-each>
<br/>
<xsl:apply-templates select="*" />
</xsl:template>
</xsl:stylesheet>