2011-06-07 61 views
0

這是一個XSLT代碼:返回已鏈接的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="$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()"/> 

時appliedon這個XML:

<ItemSet> 
<Item>1 
<iteml1>1.1</iteml1> 
<iteml1>1.2</iteml1> 
</Item> 
<Item>2 
    <iteml1>2.1 
    <iteml2>2.1.1</iteml2> 
    </iteml1> 
</Item> 
</ItemSet> 

結果是:

1 
1: 1.1 
1: 1.2 
2 
2: 2.1 
2: 2.1: 2.1.1 

我應該添加什麼,所以我可以有一個李NK上樹爲例的每elemet:

<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> 

等等

+0

你還在尋找答案? – 2011-06-10 13:19:24

+0

你想要輸出一個** simil **定位HTML標記,它根本不是HTML。這是想要的還是錯字?如果我沒有錯,請修改。 – 2011-06-10 13:21:42

+0

仍然需要一個答案是的,我知道不是HTML但它的原因是我不能在示例中添加一個鏈接,因此如果你有答案,與我們共享 – 2011-06-13 08:29:03

回答

1
<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])"/> 
       <a href="{$vValue}"> 
         <xsl:value-of select="$vValue"/> 
       </a> 
       <br/> 
       <xsl:apply-templates select="*"> 
         <xsl:with-param name="pPath" select="concat($pPath, $vValue, ': ')"/> 
       </xsl:apply-templates> 
     </xsl:template> 

     <xsl:template match="text()"/> 
</xsl:stylesheet> 

或嘗試這一個

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
     <xsl:output omit-xml-declaration="yes" method="html"/> 
     <xsl:template match="ItemSet/Item"> 
     <xsl:variable name="vValue" select="normalize-space(text()[1])"/> 
       <a href="{$vValue}"> 
         <xsl:value-of select="$vValue"/> 
       </a> 
       <br/> 
       <xsl:apply-templates select="*" /> 
     </xsl:template> 

     <xsl:template match="iteml1 | iteml2"> 
       <xsl:variable name="vValue" select="normalize-space(text()[1])"/> 
       <xsl:variable name="vValueparent" select="normalize-space(../text()[1])"/> 

       <a href="{$vValueparent}"> 
         <xsl:value-of select="$vValueparent"/> 
       </a> : 
       <a href="{$vValue}"> 
         <xsl:value-of select="$vValue"/> 
       </a><br/> 
       <xsl:apply-templates select="*"/> 
     </xsl:template>   
</xsl:stylesheet>