Re: [xsl] Problem with xslt recursion

Subject: Re: [xsl] Problem with xslt recursion
From: Roopesh Kumar <roopeshkumar_in@xxxxxxxxx>
Date: Tue, 10 Feb 2004 11:07:06 -0800 (PST)
That was very informative...
to address your doubt about why it is not enuf for me
to just use <xsl:copy-of select="Type/text()"/>

instead of recursion i have to further explain what i
am trying to do..

Here is the complete xml

<?xml version="1.0"?>


<Element name = "El1">
<Element name = "El2">
<Element name = "El3">


Now the aim of the xsl is that given an element as a
parameter it has to build a string dynamically.

Suppose i give the parameter as El1..then the output
string should be R1_R2
If the parameter is El2 then the output string should
be R1..
if the parameter is el3 then the string should be
that is the string is dependant on the number of
values in the type tag for that element..note that the
order needs to be maintained..

Here is the xsl for doing the same...

<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
<xsl:template match="/">
<xsl:call-template name="InsertElement">
<xsl:template name="InsertElement">
 <xsl:param name="ElementName"/>
 <xsl:for-each select="/Root/Elements/Element">
 <xsl:if test="@name=$ElementName">
 <xsl:variable name="Count"
 <xsl:variable name="finalName">
<xsl:call-template name="buildCompositeName">
<xsl:with-param name="Name"></xsl:with-param>
<xsl:with-param name="Index"
select="1"/><xsl:with-param name="Count"
<xsl:value-of select="$finalName"/>

<xsl:template name="buildCompositeName">
<xsl:param name="Name"/>
<xsl:param name="Index"/>
<xsl:param name="Count"/>
<xsl:when test="$Index &lt; $Count">
<xsl:variable name="Temp1"><xsl:value-of

<!-- At this point I need the dynamic xpath which i
was talking about (which helps me access the
appropriate value based on whether Temp1 is A or B-->
<!--<xsl:variable name="Temp2"><xsl:value-of
<xsl:call-template name="buildCompositeName">
<xsl:with-param name="Name"
<xsl:with-param name="Index" select="$Index+1"/>
<xsl:with-param name="Count" select="$Count"/>
	<xsl:value-of select="$Name"/>

The problem of dynamic xpath comes at the point where
the value of a Type is to be mapped to be used to
geneate the xpath..that is..if i am looking at El1
then the first type is in the first recursion i
 need to acces the value for the xpath /Root/A
in the second recursion i need to access the value for
the xpath /Root/B
During each call i need to concatenate the value so
obtained e.i R1 in the first case and R2 in the second
case ..
At the end of the recursion which occurs when the
index becomes equal to the count..i will have the
dynamically generated variable length string..

I hope this gives u a better picture of what my
problem is..

The question now is how can i access the values R1 and
R2 the place where the xsl code is

--- David Carlisle <davidc@xxxxxxxxx> wrote:
> > [number($Index)]
> you would have been better to have kept Index as a
> number rather than
> forcing it back to a number.
> ie don't do this:
> <xsl:with-param name="Index"><xsl:value-of
> select="$Index+1"/></xsl:with-param>
> do this
> <xsl:with-param name="Index" select="$Index+1"/>
> Never use xsl:variable with content instead of a
> select attribute
> unless you intend to do that. It is quite expensive.
> You are starting with a number then generating a
> text node with the
> string value of that number, placing that in a root
> node, making a result
> tree fragment of that, and then converting the whole
> thing back to a
> number with number().
>   The purpose is solved but i want to know if it is
> the
>   right thing to do....
> Impossible to say given your description, but it
> seems unlikely that
> you need recursion at all. As I said in my reply
> your stated aim of
> concatenating the text can be done with a single
> line:
> <xsl:copy-of select="Type/text()"/>
> <xsl:variable name="fullpath"
> select="concat('/a/b/',$path)"/>
> This is a FAQ Xpaths are not strings, they are part
> of the syntax of
> the language. In C or Java or most other programming
> languages, if you
> had  a variable x with value 2 and the string "x+2" 
> it wouldn't be
> trivial to get the value 4: you'd have to write a
> parser to parse the
> string and access the referenced  constructs. It is
> the same in XPath,
> if you have a string that looks like an XPath then
> this is no use to you
> unless you have an Xpath parser that is callable at
> run time. Some
> systems do have such an extension, eg saxon:evaluate
> but it is not a
> standard part of the language.
> In simple cases such as your first
> <xsl:variable name="path">c</xsl:variable>
> (which would be better as
>   <xsl:variable name="path" select="'c'"/>
> )
> <!-- Now i am trying to access the dynamic path
> a/b/c
> you can do
> /a/b/*[name()=$path]
> This really is a FAQ see the faq that is linked from
> the home page of
> this list.
> David
> -- 
> This e-mail has been scanned for all viruses by Star
> Internet. The
> service is powered by MessageLabs. For more
> information on a proactive
> anti-virus service working around the clock, around
> the globe, visit:
>  XSL-List info and archive: 

Software Engineer 
Verizon Data Services India 


One manages uncertainty; one does not solve it.  
Eliminating uncertainty eliminates opportunity.  
Chaos is the engine of evolution.

Do you Yahoo!?
Yahoo! Finance: Get your refund fast by filing online.

 XSL-List info and archive:

Current Thread