2011-03-24 81 views
0

我需要從字符串爲從字符串變量

目錄root1 /根-2/root3

我希望它產生這樣的

<node name="root1"> 
    <node name="root2"> 
     <node name="root3"/> 
    </node> 
</node> 

我想這個節點創建節點,例如創建節點樣式表

<xsl:template match="/"> 
    <xsl:variable name="root" select="'root1/root2/root3'"/> 
    <xsl:call-template name="createNodes"> 
    <xsl:with-param name="root" select="$root"/> 
    </xsl:call-template> 
</xsl:template> 

<xsl:template name="createNodes"> 
    <xsl:param name="root"/> 
    <xsl:param name="name"/> 
    <xsl:variable name="rootPath" select="tokenize($root,'/') "/> 
    <xsl:for-each select="$rootPath"> 
     <xsl:element name="node"> 
     <xsl:attribute name="name"> 
       <xsl:value-of select="."/> 
     </xsl:attribute> 
      <xsl:call-template name="createNodes"> 
       <xsl:with-param name="caption" select="$rootPath"/> 
      </xsl:call-template> 
     </xsl:element> 
     </xsl:for-each> 
</xsl:template> 

這個問題我得到這個輸出

<node name="root1"> 
<node name="root2"> 
<node name="root3"> 

需要你的幫助:d

回答

0

基本上而不是循環的每個項目,你需要打破串移除的第一個項目,當您去,這樣你會得到一個嵌套的結果。

這應該工作:

<xsl:template name="createNodes"> 
    <xsl:param name="root"/> 
    <xsl:if test="$root"> 
    <node name="{substring-before(concat($root,'/'),'/')}"> 
     <xsl:call-template name="createNodes"> 
     <xsl:with-param name="root" select="substring-after($root,'/')"/> 
     </xsl:call-template> 
    </node> 
    </xsl:if> 
</xsl:template> 
+0

它工作得很好,非常感謝 – Fattalo 2011-03-24 12:21:22

+0

重構:文字結果元素和屬性值模板,normalizated參數的順序來處理單項目,脫衣只用在一次聲明變量相同的上下文範圍。 **請注意這是XSLT 1.0解決方案** – 2011-03-24 15:40:48