[xsl] xsl:variable inside xsl:for-each

Subject: [xsl] xsl:variable inside xsl:for-each
From: "a. benedict balbuena" <abcbalbuena@xxxxxxxxx>
Date: Tue, 11 Apr 2006 17:32:11 +0800
Hello!

I'm a noob in XSLT and currently using XSLT 1.0. I just learned XSLT
this week and am trying to use it in some of my projects.

I cant seem to find an answer on the net on my problem, maybe some of
you guys can help.

I have an input xml file:

    <ec name="X" value="4"/>
    <ec name="Y" value="7"/>
    <ec name="Z" value="0.1"/>

     <t>
        <p>
            <ec name="sax" value="$X"/>
            <ec name="say" value="$Y"/>
            <ec name="saz" value="$Z"/>
            <t>
                <sxa position="$sax"/>
            </t>
            <t>
                <sya position="$say"/>
            </t>
            <t>
                 <ffA position="$saz"/>
             </t>
         </p>
    </t>


----

I want to output the following in html:

<table border="1">
<tr>
<td>X</td>
<td>4</td>
</tr>

<tr>
<td>Y</td>
<td>7</td>
</tr>
<tr>
<td>Z</td>
<td>7</td>
</tr>
</table>


-----

theoretically the xsl file should do this:

<xsl:for-each select="ec">
<!-- MAKE A VARIABLE NAMED X, Y, and Z -->
</xsl:for-each>

<xsl:for-each select="t/p/ec">
<!-- MAKE A VARIABLE NAMED sax, say, and saz -->
</xsl:for-each>

<table border="1">
     <xsl:for-each select="t/p/t">
         <xsl:choose>
	     <xsl:when test="child::sxa">
                 <tr>
                     <td> <xsl:text> X </xsl:text> </td>
                     <td> <xsl:value-of select="sxa/@position" /> </td>
                 </tr>
             </xsl:when>
	     <xsl:when test="child::sya">
                 <tr>
                     <td> <xsl:text> Y </xsl:text> </td>
                     <td> <xsl:value-of select="sya/@position" /> </td>
                 </tr>
             </xsl:when>

	     <xsl:when test="child::ffa">
                 <tr>
                     <td> <xsl:text> Z </xsl:text> </td>
                     <td> <xsl:value-of select="ffa/@position" /> </td>
                 </tr>
             </xsl:when>
         </xsl:choose>
     </xsl:for-each>
</table>

Im stuck inside the first for-each loops. Inserting the following
inside the for-each loops amounts to nothing since the variable name
should be a string, but i'm really referencing to the name attribute
of the <ec> tag.

<xsl:variable name="@name>
     <xsl:value-of select="@value"/>
<xsl:variable>



Any suggestion will be most welcome! Thanks!

Current Thread