Click here to Skip to main content
15,898,035 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Can any one tell how to dynamically change the style on td tag.
I have a code in XSLT file which should get the style and assign to td tag.

I have below code in XSLT, where I need style for td for $QuestionIndex+1 value in the code. Problem is how to dynamically generate td style.
XML
<xsl:when test ="../../@IsQID='True'">
                  <tr>
                    <td valign="top" width="1%" noWrap="yes" >
                        <xsl:if test="@QuestionType!='INS' and @QuestionType!='MAT'">
                        <xsl:if test="../../@IsQUnderline='True'">__</xsl:if>
                        <xsl:if test="../../@IsQNumber='True'">
                            <xsl:value-of select="$QuestionIndex+1" />.<!--plus 1 to include current questions-->
                        </xsl:if>
                      </xsl:if>
                    </td>
                    <td align="left" width="99%" class="tdNumCls">
                      <xsl:value-of select ="ID"/>
                    </td>
                  </tr>
                  <tr>
                    <td valign="top" width="1%" noWrap="yes" >
                    </td>
                    <td align="left" width="99%" class="tdNumCls">
                      <xsl:if test="../../@IsQStem='True'">
                        <xsl:value-of select="Stem/text()" disable-output-escaping="yes"/>
                      </xsl:if>
                    </td>
                  </tr>
                </xsl:when>




Nooo. I need style like Font-size:11 pt; Font-weight:Bold; Font-style:Arial. In below line of code @QuestionIndexFont actually is XMLDocument Attribute. Where I'm assigning Font style with Font-size:11 pt; Font-weight:Bold; Font-style:Arial

I tried with Font style like this shown below. But, not able to assign font to the questionIndex value
XML
<font style="{@QuestionIndexFont}">
                                <xsl:value-of select="$QuestionIndex+1" />.<!--plus 1 to include current questions-->
                            </font>

But, same code works with
XML
<font style="Font-size:11pt; Font-weight:bold">
                                <xsl:value-of select="$QuestionIndex+1" />.<!--plus 1 to include current questions-->
                            </font>

QuestionIndexFont changes. How should i assign font style ?
Posted
Updated 4-Nov-10 15:30pm
v3
Comments
AspDotNetDev 4-Nov-10 21:31pm    
I deleted another of your fake answers and incorporated it into your question. Please learn to edit your question rather than adding more fake answers. Also, I edited my answer in a further attempt to answer your question.

1 solution

So you want to set the style attribute on a TD and you want to set it to something based on a numeric variable named $QuestionIndex? I'll assume this is the TD you want to apply that to:
XML
<td valign="top" width="1%" noWrap="yes" >
</td>

I'll assume that you want to set the font color to "Red" or "Green" depending on the value of $QuestionIndex:
XML
<td valign="top" width="1%" noWrap="yes">
	<xsl:attribute name="style">
		<xsl:choose>
			<xsl:when test="$QuestionIndex = 1">
				<xsl:value-of select="'color: Red;'" />
			</xsl:when>
			<xsl:otherwise>
				<xsl:value-of select="'color: Green;'" />
			</xsl:otherwise>
		</xsl:choose>
	</xsl:attribute>
</td>

EDIT: Your description is still confusing, but let me try again based on what I now think you want. So @QuestionIndexFont will have the style information (font size, font weight) stored as a string, and you want to apply that to the style attribute of a TD element which contains the value stored in $QuestionIndex + 1. So @QuestionIndexFont might contain "Font-size: 11pt; Font-weight: bold;" and $QuestionIndex might be equal to the number 3 (note: not the string "3"). When all is said and done, you want the HTML to look like this:
HTML
<td style="Font-size: 11pt; Font-weight: bold;">
    4
</td>

To do that, you'd use this XSLT:
XML
<td>
    <xsl:attribute name="style">
        <!-- Note: if this isn't the right way to select an attribute, let me know. -->
        <xsl:value-of select="@QuestionIndexFont" />
    </xsl:attribute>
    <xsl:value-of select="$QuestionIndex + 1" />
</td>

If that's not correct, then please try to explain what it is you really want. And don't post it as another answer that I will have to delete... post it as an update to your question then comment on this answer to let me know you made that update.

EDIT: Just a tip to help you debug. If you want to see what the XML input is in your output, you just do this:
XML
<xsl:copy-of select="." />

IIRC, that should give you a copy of the entire XML structure as it exist at that point in the code. You can use that to see if you're getting the expected data.
 
Share this answer
 
v4
Comments
ssssyyy 4-Nov-10 22:59pm    
Yes, exactly your right.

I tried the way you suggested. But, @QuestionIndexFont is not taking the Font style that was assigned through the XML attribute document.
AspDotNetDev 4-Nov-10 23:33pm    
Sounds like your XML input may be incorrect. I updated my answer to show you a technique you can use to debug your XML.
ssssyyy 5-Nov-10 9:58am    
When i debugged to see what's getting out of @QuestionIndexFont to the XSLT and value seems to be looking and fine and @QuestionIndexFont looks like below

FONT-SIZE: 8pt ; FONT-FAMILY:Arial; FONT-WEIGHT:Bold;

Is it any thing wrong. but, still the way you suggested not working .
ssssyyy 5-Nov-10 10:10am    
When I look at the html source, it looks like last td which has 1. has style empty.


<table><tr><td align="left"></td><td>

</td><td align="left"></td><td>Font-size: 8pt; Font-weight: Bold;</td></tr></table></td></tr><tr><td><table><tr><td><div><table width="100%" cellspacing="0" cellpadding="0"><tr><td valign="top" width="1%" noWrap="yes" style="">1.</td><td align="left" width="99%" class="tdNumCls">
AspDotNetDev 5-Nov-10 21:04pm    
Hmmm, not sure why it's not working then. Good luck.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900