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...
thanks..
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"?>

<Root>
<A>R1</A>
<B>R2</B>
<C>R3</C>

<Elements>
<Element name = "El1">
  <R>
   <Type>A</Type>
   <Type>B</Type>
  </R>
</Element>
<Element name = "El2">
  <R>
   <Type>A</Type>
  </R>
</Element>
<Element name = "El3">
  <R>
   <Type>A</Type>
   <Type>B</Type>
   <Type>C</Type>
  </R>
</Element>

</Elements>
</Root>

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
R1_R2_R3
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"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>
<xsl:template match="/">
<html>
<body>
<xsl:call-template name="InsertElement">
<xsl:with-param
name="ElementName">El1</xsl:with-param>
</xsl:call-template>
</body>
</html>
</xsl:template>
<xsl:template name="InsertElement">
 <xsl:param name="ElementName"/>
 <xsl:for-each select="/Root/Elements/Element">
 <xsl:if test="@name=$ElementName">
 <xsl:variable name="Count"
select="count(descendant::R/Type)"></xsl:variable>
 <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"
select="$Count+1"/>
</xsl:call-template>
</xsl:variable>
<p>
<xsl:value-of select="$finalName"/>
</p>
</xsl:if>
</xsl:for-each>
</xsl:template>

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

<!-- 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
select="/Root/{$Temp1}"/></xsl:variable>-->
				
<xsl:call-template name="buildCompositeName">
<xsl:with-param name="Name"
select="concat($Name,'_',$Temp2)"/>
<xsl:with-param name="Index" select="$Index+1"/>
<xsl:with-param name="Count" select="$Count"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
	<xsl:value-of select="$Name"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>

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 A..now 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 dynamically...at the place where the xsl code is
commented



--- 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
> 
> 
> 
> -- 
> http://www.dcarlisle.demon.co.uk/matthew
> 
>
________________________________________________________________________
> 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:
> http://www.star.net.uk
>
________________________________________________________________________
> 
>  XSL-List info and archive: 
> http://www.mulberrytech.com/xsl/xsl-list
> 


=====
Luv
Roopesh
Software Engineer 
Verizon Data Services India 
[98842-40222]

__________________________________________________________________

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.
http://taxes.yahoo.com/filing.html

 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Current Thread