HowTo: Modify the guestbook

This HowTo modifies the guestbook implementation template. We want to modify the implementation template, because this doesn't contain any design and therefore we can focus on only functionality.

  • User ID and IP will be shown next to a guestbook entry
  • Name will be printed in red, when user = "admin"
  • Name will be printed in green, when user is a guestuser
  • Name will be printed in blue, when the posting is from a registered user
  • An additional text will be printed, when the posting is from "admin"
  • All entries will be sorted by date

It is recommended to read the implementation basics documentation before starting with this (first) HowTo.

used files "\xsl\implementation\GuestbookgetEntry.xsl".

Introduction – Where are the modifications done?

Because we don't want to change any functionality, but only the display, we don't have to change anything in the JSP-file. We only modify the XSL.file. You'll find this under IMPLEMENTATIONDIR\xsl\implementation\GuestbookgetEntry?.xsl". Please open the file with an editor of your choice:

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  <xsl:template match="/">
    <table>
      <tr>
        <td>
          <b>User</b>
        </td>
        <td>
          <b>creation date</b>
        </td>
        <td>
          <b>message</b>
        </td>
      </tr>
      <xsl:for-each select="GUESTBOOK/ENTRY">
        <tr>
          <td>
            <strong>
              <xsl:value-of select="CREATE/USER/NAME"/>
            </strong>
          </td>
          <td>
            <xsl:value-of select="CREATE/DATE"/>
          </td>
          <td>
            <xsl:value-of select="MESSAGE"/>
          </td>
        </tr>
      </xsl:for-each>
    </table>
  </xsl:template>
</xsl:stylesheet>

Currently we show name, date and entry:

User

creation date

message

the guest

16.06.2005 12:07

A new message from a guest!

hansi

16.06.2005 12:06

I leave a little message

admin

16.06.2005 12:05

A newer message

admin

16.06.2005 12:04

A new message !!!

XML of the guestbook

<GUESTBOOK>
  <MODULENAME>global</MODULENAME>
  <ENTRYID>-1</ENTRYID>
  <PERSONAL>false</PERSONAL>
  <ENTRY>
      <CREATE>
       <DATE>31.10.2002 02:54</DATE>
       <LDATE>123311020020254</LDATE>
       <USER>
         <NAME>deruser</NAME>
         <ID>15648432465</ID>
         <IP>127.0.0.1</IP>
        </USER>
      </CREATE>
      <MESSAGE>Eine Message</MESSAGE>
    </ENTRY>
  <ENTRY>
      <CREATE>
       <DATE>31.09.2002 02:54</DATE>
       <LDATE>123311020020254</LDATE>
       <USER>
         <NAME>guest</NAME>
         <ID>2</ID>
         <IP>127.0.0.1</IP>
        </USER>
      </CREATE>
      <MESSAGE>Message from guest - > hey its my NAME</MESSAGE>
    </ENTRY>
  <ENTRY>
      <CREATE>
       <DATE>1.03.2002 02:54</DATE>
       <LDATE>123311020020254</LDATE>
       <USER>
         <NAME>admin</NAME>
         <ID>1</ID>
         <IP>127.0.0.1</IP>
        </USER>
      </CREATE>
      <MESSAGE>Nachricht des Admins...irgendwas</MESSAGE>
    </ENTRY>
</GUESTBOOK>

MODIFICATIONS

Display UserID / User IP

The XML-Tree shows that the userid will be returned in GUESTBOOK/ENTRY/CREATE/USER/ID. So we add the following line:

GuestbookgetEntry.xsl line 21:

         <td>
            <strong>
              <xsl:value-of select="CREATE/USER/NAME"/>
            </strong><br/>
            ID: <xsl:value-of select="CREATE/USER/ID"/><br/>
          </td>

User

creation date

message

the guest
ID: 1

16.06.2005 12:07

A new message from a guest!

hansi
ID: 1115638983565

16.06.2005 12:06

I leave a little message

admin
ID: 2

16.06.2005 12:05

A newer message

admin
ID: 2

16.06.2005 12:04

A new message !!!

Furthermore we want to show the IP, but only the first 3 digits:

GuestbookgetEntry.xsl line 22:

         <td>
            <strong>
              <xsl:value-of select="CREATE/USER/NAME"/>
            </strong><br/>
            ID: <xsl:value-of select="CREATE/USER/ID"/><br/>
            IP: <xsl:value-of select="substring-before(CREATE/USER/IP, '.')"/>
            <xsl:text>.xxx.xxx.xxx</xsl:text>
          </td>

User

creation date

message

the guest
ID: 1
IP: 127.xxx.xxx.xxx

16.06.2005 12:07

A new message from a guest!

hansi
ID: 1115638983565
IP: 127.xxx.xxx.xxx

16.06.2005 12:06

I leave a little message

admin
ID: 2
IP: 127.xxx.xxx.xxx

16.06.2005 12:05

A newer message

admin
ID: 2
IP: 127.xxx.xxx.xxx

16.06.2005 12:04

A new message !!!

Posting in red if from admin, green if from guest, blue if from registered user

To find out which user entered the posting we check the userid. The user admin has the ID 2, the user guest the ID 1. We simply add the corresponding style-attribute to the enclosing <td> :

GuestbookgetEntry.xsl line 18:

         <td>
            <xsl:choose>
              <xsl:when test="CREATE/USER/ID=2">
                <xsl:attribute name="style">
                  <xsl:text>color:red</xsl:text>
                </xsl:attribute>
              </xsl:when>
              <xsl:when test="CREATE/USER/ID=1">
                <xsl:attribute name="style">
                  <xsl:text>color:green</xsl:text>
                </xsl:attribute>
              </xsl:when>
              <xsl:otherwise>
                <xsl:attribute name="style">
                  <xsl:text>color:blue</xsl:text>
                </xsl:attribute>
              </xsl:otherwise>
            </xsl:choose>
            <strong>
              <xsl:value-of select="CREATE/USER/NAME"/>
            </strong><br/>
            ID: <xsl:value-of select="CREATE/USER/ID"/><br/>
            IP: <xsl:value-of select="substring-before(CREATE/USER/IP, '.')"/>
            <xsl:text>.xxx.xxx.xxx</xsl:text>
          </td>

User

creation date

message

the guest
ID: 1
IP: 127.xxx.xxx.xxx

16.06.2005 12:07

A new message from a guest!

hansi
ID: 1115638983565
IP: 127.xxx.xxx.xxx

16.06.2005 12:06

I leave a little message

admin
ID:
2 IP: 127.xxx.xxx.xxx

16.06.2005 12:05

A newer message

admin
ID: 2
IP: 127.xxx.xxx.xxx

16.06.2005 12:04

A new message !!!

Display of an additional text, if the posting is from user admin

We check the ID (2 = admin) and show a special text if so:

GuestbookgetEntry.xsl line 36:

           </xsl:choose>
            <strong>
              <xsl:if test="CREATE/USER/ID='2'">
                The boss
              </xsl:if>
              <xsl:value-of select="CREATE/USER/NAME"/>
            </strong><br/>

User

creation date

message

the guest
ID: 1
IP: 127.xxx.xxx.xxx

16.06.2005 12:07

A new message from a guest!

hansi
ID: 1115638983565
IP: 127.xxx.xxx.xxx

16.06.2005 12:06

I leave a little message

The boss admin
ID: 2
IP: 127.xxx.xxx.xxx

16.06.2005 12:05

A newer message

The boss admin
ID: 2
IP: 127.xxx.xxx.xxx

16.06.2005 12:04

A new message !!!

Sort the postings ascending (by date)

Currently the lates entry will be displayed first. In our example the entry from 12:07 is the first one, the entry from 12:04 the last. We want to reverse this order, so that the latest entry comes last. In the XML-tree we find the date in GUESTBOOK/ENTRY/CREATE/DATE. But this is the human readable version of the date, which is very complicated to sorted at. In the XML-tree you will find an additional field called LDATE. This is the date as a number - meaning that the number is higher wenn the date is later. So we can user GUESTBOOK/ENTRY/CREATE/LDATE for sorting:

GuestbookgetEntry.xsl line 16:

      </tr>
      <xsl:for-each select="GUESTBOOK/ENTRY">
        <xsl:sort select="CREATE/LDATE" order="ascending"/>
        <tr>
          <td>
            <xsl:choose>

User

creation date

message

The boss admin
ID: 2
IP: 127.xxx.xxx.xxx

16.06.2005 12:04

A new message !!!

The boss admin
ID: 2
IP: 127.xxx.xxx.xxx

16.06.2005 12:05

A newer message

hansi
ID: 1115638983565
IP: 127.xxx.xxx.xxx

16.06.2005 12:06

I leave a little message

the guest
ID: 1
IP: 127.xxx.xxx.xxx

16.06.2005 12:07

A new message from a guest!

The order was changed.

The final XSL

<?xml version="1.0" encoding="ISO-8859-1"?> 
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
   <xsl:template match="/">
    <table border="3">
      <tr>
        <td>
          <b>User</b>
        </td>
        <td>
          <b>creation date</b>
        </td>
        <td>
          <b>message</b>
        </td>
      </tr>
      <xsl:for-each select="GUESTBOOK/ENTRY">
        <xsl:sort select="CREATE/LDATE" order="ascending"/>
        <tr>
          <td>
            <xsl:choose>
              <xsl:when test="CREATE/USER/ID=2">
                <xsl:attribute name="style">
                  <xsl:text>color:red</xsl:text>
                </xsl:attribute>
              </xsl:when>
              <xsl:when test="CREATE/USER/ID=1">
                <xsl:attribute name="style">
                  <xsl:text>color:green</xsl:text>
                </xsl:attribute>
              </xsl:when>
              <xsl:otherwise>
                <xsl:attribute name="style">
                  <xsl:text>color:blue</xsl:text>
                </xsl:attribute>
              </xsl:otherwise>
            </xsl:choose>
            <strong>
              <xsl:if test="CREATE/USER/ID='2'">
                The boss
              </xsl:if>
              <xsl:value-of select="CREATE/USER/NAME"/>
            </strong><br/>
            ID: <xsl:value-of select="CREATE/USER/ID"/><br/>
            IP: <xsl:value-of select="substring-before(CREATE/USER/IP, '.')"/>
            <xsl:text>.xxx.xxx.xxx</xsl:text>
          </td>
          <td>
            <i>
              <xsl:value-of select="CREATE/DATE"/>
            </i>
          </td>
          <td>
            <xsl:value-of select="MESSAGE"/>
          </td>
        </tr>
      </xsl:for-each>
    </table>
  </xsl:template>
</xsl:stylesheet>