Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old March 24th, 2011, 07:57 AM
Registered User
 
Join Date: Mar 2011
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default Create nodes from string varaible

Hi

I need to create nodes from string , example for that

root1/root2/root3

i want it to generate nodes like that

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

I tried this style sheet

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

The problem i get this output

<node name="root1">
<node name="root2">
<node name="root3">
 
Old March 24th, 2011, 08:17 AM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

Don't use for-each, simply use recursion:
Code:
<xsl:stylesheet
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:xsd="http://www.w3.org/2001/XMLSchema"
  xmlns:mf="http://example.com/mf"
  exclude-result-prefixes="mf xsd"
  version="2.0">
  
  <xsl:output indent="yes"/>
  
  <xsl:function name="mf:create-elements" as="element()*">
    <xsl:param name="element-names" as="xsd:string*"/>
    <xsl:if test="$element-names[1]">
      <node name="{$element-names[1]}">
        <xsl:sequence select="mf:create-elements($element-names[position() gt 1])"/>
      </node>
    </xsl:if>
  </xsl:function>
  
  <xsl:template name="main">
    <xsl:sequence select="mf:create-elements(tokenize('root1/root2/root3', '/'))"/>
  </xsl:template>

</xsl:stylesheet>
I used a function as it is more elegant in my view but feel free to use a template.
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog
 
Old March 31st, 2011, 07:29 AM
Registered User
 
Join Date: Nov 2007
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to asfak
Default

Quote:
Originally Posted by Ahly_Bayern View Post
Hi

I need to create nodes from string , example for that

root1/root2/root3

i want it to generate nodes like that

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

I tried this style sheet

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

The problem i get this output

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

My simple solution


Code:
<xsl:template match="/">
        <xsl:variable name="text">
            <xsl:text>root1/root2/root3</xsl:text>
        </xsl:variable>
        <xsl:call-template name="node-creator">
            <xsl:with-param name="text" select="$text"/>
        </xsl:call-template>
    </xsl:template>
    
    <xsl:template name="node-creator">
        <xsl:param name="text"/>
        <xsl:variable name="preSlash" select="substring-before($text,'/')"/>
        <xsl:variable name="afterSlash" select="substring-after($text,'/')"/>
        <xsl:choose>
            <xsl:when test="$preSlash != ''">
                <node name="{$preSlash}">
                    <xsl:if test="$afterSlash != ''">
                        <xsl:call-template name="node-creator">
                            <xsl:with-param name="text" select="$afterSlash"/>
                        </xsl:call-template>
                    </xsl:if>
                </node>
            </xsl:when>
            <xsl:otherwise>
                <xsl:if test="$text != ''">
                    <node name="{$text}"/>
                </xsl:if>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>





Similar Threads
Thread Thread Starter Forum Replies Last Post
SSO and Session varaible access sanjivbshinde ASP.NET 3.5 Professionals 3 December 3rd, 2009 04:42 PM
how to create string with quots dishant XML 7 October 1st, 2009 07:05 PM
Javascript varaible jabeen Javascript 1 October 25th, 2007 06:57 AM
create one string from various records paul20091968 Access VBA 1 January 4th, 2007 04:18 PM
How to assign xsl param with java script varaible Sanjay.Verma XSLT 1 November 21st, 2006 04:34 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.