Jump to content

Using variable in XSLT


nobitavn94

Recommended Posts

I'm new in XSLT.I want to use avariable to store Id as a parameter like this :<xsl:for-each select="NewDataSet/Table"> <tr> <td style="font-family: Verdana;font-size : 8pt;color:blue"> <xsl:variable name="id" select="Id" /> <xsl:value-of select="Id"/>. </td> <td> <a href="showCatalog.aspx?id='$name'" class="catalogLink" ><xsl:value-of select="Name"/> </a> </td> </tr> </xsl:for-each>but this code doesn't work properly.Can any one help me ?Thanks you very much !

Link to comment
Share on other sites

First of all, you adress a variable by the value of the name attribute, not the name attribute itself. So you should adress this variable with "$id", not "$name".Also, a local variable (one inside a template) is only available to it's siblings and descendants. Scince in this example the variable is a child of a <td>, it's available only to that <td> and further down.To make it accessable to other cells, simply move it at least one level above. In other words, create it inside a <tr> like this:

<xsl:for-each select="NewDataSet/Table"><tr><xsl:variable name="id" select="Id" /><td style="font-family: Verdana;font-size : 8pt;color:blue"><xsl:value-of select="Id"/>.</td><td> <a href="showCatalog.aspx?id='$id'" class="catalogLink" ><xsl:value-of select="Name"/> </a></td></tr> </xsl:for-each>

If that for-each generated more then one row, you may even create the variable as a sibling to the row like this:

<xsl:for-each select="NewDataSet/Table"><xsl:variable name="id" select="Id" /><tr><td style="font-family: Verdana;font-size : 8pt;color:blue"><xsl:value-of select="Id"/>.</td><td> <a href="showCatalog.aspx?id='$id'" class="catalogLink" ><xsl:value-of select="Name"/> </a></td></tr> </xsl:for-each>

Scince all of those are created for each NewDataSet/Table node, the result is the same.

Link to comment
Share on other sites

First of all, you adress a variable by the value of the name attribute, not the name attribute itself. So you should adress this variable with "$id", not "$name".Also, a local variable (one inside a template) is only available to it's siblings and descendants. Scince in this example the variable is a child of a <td>, it's available only to that <td> and further down.To make it accessable to other cells, simply move it at least one level above. In other words, create it inside a <tr> like this:
<xsl:for-each select="NewDataSet/Table"><tr><xsl:variable name="id" select="Id" /><td style="font-family: Verdana;font-size : 8pt;color:blue"><xsl:value-of select="Id"/>.</td><td> <a href="showCatalog.aspx?id='$id'" class="catalogLink" ><xsl:value-of select="Name"/> </a></td></tr> </xsl:for-each>

If that for-each generated more then one row, you may even create the variable as a sibling to the row like this:

<xsl:for-each select="NewDataSet/Table"><xsl:variable name="id" select="Id" /><tr><td style="font-family: Verdana;font-size : 8pt;color:blue"><xsl:value-of select="Id"/>.</td><td> <a href="showCatalog.aspx?id='$id'" class="catalogLink" ><xsl:value-of select="Name"/> </a></td></tr> </xsl:for-each>

Scince all of those are created for each NewDataSet/Table node, the result is the same.

I follow your code ,but it doesn't work. Then , the link each text inside td tag like this:"..../showCatalog?id='$id' " . I want $id store <xsl:value-of select="Id"/> value .I wonder if I made mistakes anywhere .Thanks !I follow your code ,but it doesn't work. Then , the link each text inside td tag like this:"..../showCatalog?id='$id' " . I want $id store <xsl:value-of select="Id"/> value .I wonder if I made mistakes anywhere .Thanks !
Link to comment
Share on other sites

I follow your code ,but it doesn't work. Then , the link each text inside td tag like this:"..../showCatalog?id='$id' " . I want $id store <xsl:value-of select="Id"/> value .I wonder if I made mistakes anywhere .Thanks !

I follow your code ,but it doesn't work. Then , the link each text inside td tag like this:"..../showCatalog?id='$id' " . I want $id store <xsl:value-of select="Id"/> value .I wonder if I made mistakes anywhere .Thanks !
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...