Xslt 从字符串变量创建节点
我需要从字符串创建节点,例如 root1/root2/root3 我希望它生成这样的节点Xslt 从字符串变量创建节点,xslt,Xslt,我需要从字符串创建节点,例如 root1/root2/root3 我希望它生成这样的节点 <node name="root1"> <node name="root2"> <node name="root3"/> </node> </node> 我试过这个样式表 <xsl:template match="/"> <xsl:variable name="root" sele
<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基本上,您不需要在每个项目上循环,而是需要分解字符串,在执行过程中删除第一个项目,这样您将得到嵌套结果 这应该起作用:
<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>
refactured:Literal结果元素和属性值模板,规范化参数以按顺序处理单例项,strip变量声明仅在同一上下文范围内使用一次。请注意,这是XSLT1.0解决方案