2011-03-24 106 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