Render a table with alternating rows and columns different from each other - using xsl:apply-templates

by Euo 25. March 2009 14:12

Let say you have a list of items and you want to display them in a table-like format. What you would do is to loop over those items and render each item in a column. And again you have a requirements that:

there must only have two columns and the left column' color is different from the right; and

alternating rows must also have different color. 

 

Here is a sample code to accomplish that.

 

 <xsl:template match="*" mode="main">

        <xsl:variable name="dataItem" select='your list of items, let say 8'/>

        <table border='1' width='250px' cellspacing="5px"  >
            <xsl:apply-templates  mode='table' select='$dataItem/item[position() mod 2 = 1]'  />
        </table>

    </xsl:template>

    <xsl:template mode="table" match="item" >
        <xsl:variable name="itemSet" select=". | following-sibling::item[position() &lt; 2]"/>
        <xsl:apply-templates select="$itemSet[position() mod 2 = 1]" mode="row">
            <xsl:with-param name="row" select="position()"/>
        </xsl:apply-templates>
    </xsl:template>

    <xsl:template match="item" mode="row">
        <xsl:param name="row"/>
        <xsl:variable name="rowColor">
            <xsl:choose>
                <xsl:when test="($row mod 2)=0">
                    <xsl:value-of select="'background-color:maroon;'"/>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:value-of select="'background-color:black;'"/>
                </xsl:otherwise>
            </xsl:choose>
        </xsl:variable>

        <tr style="{$rowColor}" height="50px">
            <xsl:apply-templates mode="col" select=". |following-sibling::item[position() &lt; 2]">
                <xsl:with-param name="row" select="$row"/>
            </xsl:apply-templates>
        </tr>
    </xsl:template>

    <xsl:template match="item" mode="col">
        <xsl:param name="row"/>
        <xsl:variable name="color">
            <xsl:choose>
                <xsl:when test="(position() mod 2)=0">
                    <xsl:value-of select="'background-color:yellow;'"/>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:value-of select="'background-color:green;'"/>
                </xsl:otherwise>
            </xsl:choose>
        </xsl:variable>

        <td width="50%">
            <div  style="{$color}">
                row: <xsl:value-of select="$row" />
                <br/>
                col: <xsl:value-of select="position()"/>
            </div>
        </td>
    </xsl:template>

The code above will render the image below:

Tags: ,

Development

make .NET and PHP MD5 hashing compatible

by Euo 19. March 2009 13:29

I found an example here on how to hash a string using .NET in which the result is the same output of PHP hashing. But based on the comments, there will be problem if the string is in different encodings. 

Anyways, here is the code. 

 

using System;
using System.Collections.Generic;
using System.Text;
using System.Security.Cryptography;

namespace SimpleWeb.API
{
public class MD5
{
/// <summary>
/// Returns an MD5 has of a string.
/// </summary>
/// <param name="hashMe"></param>
/// <returns></returns>
public string GetHash(string hashMe)
{
MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
UTF7Encoding encoder = new UTF7Encoding();
Byte[] encStringBytes;

encStringBytes = encoder.GetBytes(hashMe);
encStringBytes = md5.ComputeHash(encStringBytes);

string strHex = string.Empty;
foreach (byte b in encStringBytes)
{
strHex += String.Format("{0:x2}", b);
}

return strHex;

}
}
}

 

Tags: , ,

Development

About the author

Something about the author

Month List

Page List