Re: [xsl] Using a parameter in a select within a recursive template

Subject: Re: [xsl] Using a parameter in a select within a recursive template
From: "M. David Peterson" <m.david@xxxxxxxxxx>
Date: Sun, 19 Sep 2004 00:31:07 -0700
Hello again Helen :)

Yeah, this stylesheet has several things keeping it from working. First, your xsl:param elements need to either not use the select attribute and just set the value within the opening and closing tags or use single quotes inside the select attribute to tell the processor to use the contents as an absolute value. The way its setup right now you are telling the processor to set there value to the value of the elements named 141 and 19.

The second problem is that you are telling the processor to output the literal string value of the concatenation of the string "FIELD_" and the value of $number. The way around this is to instead do a test using the local-name() function to compare to your concated string value and output the value of the element that matches...

I would try something like the following. Note: I excluded the fo elements to simplify -- just add it back as needed. I also lowered the number values of the number and cols params to simplify the input and output for testing.

<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl=""; version="1.0">

  <xsl:template match="/">
    <xsl:call-template name="adddata">
      <xsl:with-param name="number" select="'5'"/>
      <xsl:with-param name="cols" select="'0'"/>

  <xsl:template name="adddata">
    <xsl:param name="number"/>
    <xsl:param name="cols"/>

      <xsl:when test="$number >= $cols">
            <xsl:value-of select="//*[local-name() = concat('FIELD_',$number)]"/>
        <xsl:call-template name="adddata">
          <xsl:with-param name="number" select="$number - 1"/>
          <xsl:with-param name="cols" select="$cols" />
        <xsl:value-of select="$cols"/>


Which when you process this XML:

<?xml version="1.0" encoding="UTF-8"?>
  <FIELD_1>test of field 1</FIELD_1>
  <FIELD_2>test of field 2</FIELD_2>
  <FIELD_3>test of field 3</FIELD_3>
  <FIELD_4>test of field 4</FIELD_4>
  <FIELD_5>test of field 5</FIELD_5>

Looks like this:

test of field 5test of field 4test of field 3test of field 2test of field 10

Hope this helps!

Best regards,


Sean & Helen wrote:
Hi everyone,
I am attempting to use a parameter within a recursive template to select a
different node whenever the template loops.

My datafile looks something like this:


I would like to loop through and use the incremented parameter to select the
contents of the node. So far I have been trying to use this template:

<xsl:template name="adddata">
     <xsl:param name="number" select="141" />
     <xsl:param name="cols" select="19" />

     <xsl:when test="$number &lt;= $cols">
        <xsl:value-of select="cols"/>
        <xsl:call-template name="adddata">
         <xsl:with-param name="number" select="$number - 1"/>
         <xsl:with-param name="cols" select="$cols" />
           <fo:table-cell >
               <fo:block><xsl:value-of select="concat('FIELD_',$number)"

However, the output gives me:
FIELD_1 FIELD_2 FIELD_3 etc....
rather than the data contained within these nodes. Can someone shed some
light for me?


Current Thread