tr | en
7
Jul
2020
merhabadunya.xsl
Category: 

The purpose of the XSL template file is to draw the page in HTML whenever the module's pages are requested.

In order to be able to attach its contents to the global page layout of the portal, every module must define a special XSL tamplate named module-my-page-layout.

In our example module merhabadunya only one content are is enough and we can define the xsl:template as follows:

<xsl:template name="module-my-page-layout">
 <xsl:copy-of select="$site-layout/document/page[@im='merhabadunya']"/
</xsl:template>

Additonally, another xsl:template which is executed based on a match condition needs to be defined for displaying the content area.

In this xsl:template we will display the "Merhaba Dunya!" heading at the top of the content area Just after the heading we will display a line depending on the URL query parameters as follows:

  1. If the URL query part contains "?im=merhabadunya&ip=1" then the IP address of the visitor will be asked to the server side script and the returned value will be displayed
  2. If the URL query part contains "?im=merhabadunya&ip=x" where x is any value other than an error message will be displayed
  3. If there is no parameter named ip then a paragraph with a link to the page to show the visitor IP will ve displayed.

The xsl:template for the above scenario can be coded as follows::

<xsl:template match="content[@type='merhabadunya']" mode="page-layout">
 <div id="merhabaDiv">
 <h1>Merhaba Dünya</h1>
 <xsl:choose>
  <xsl:when test="$get/ip=1">
   <p>IP: <xsl:value-of select="php:function( 'visitorIP')"/></p>
   <p><a href="?im=merhabadunya">Geri</a></p>
  </xsl:when>
  <xsl:when test="$get/ip">
   <p>Hatalı URL: ip=1 olmalı</p>
   <p><a href="?im=merhabadunya">Geri</a></p>
  </xsl:when>
  <xsl:otherwise>
   <p><a href="?im=merhabadunya&amp;ip=1">IP'mi göster</a></p>
  </xsl:otherwise>
 </xsl:choose>
 </div>
</xsl:template>

NOTE: The function which named visitorIP()  and also shown in bold above will be coded as server side script later.

So, the resulting  merhabadunya.xsl file is given below:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:php="http://php.net/xsl"
    exclude-result-prefixes="php">

 <xsl:import href="index.xsl"/> 

 <xsl:template match="content[@type='merhabadunya']" mode="page-layout">
  <div id="merhabaDiv">
   <h1>Merhaba Dünya!</h1>
   <xsl:choose>
    <xsl:when test="$get/ip=1">
     <p>IP: <xsl:value-of select="php:function( 'visitorIP')"/></p>
     <p><a href="?im=merhabadunya">Geri</a></p>
    </xsl:when>
    <xsl:when test="$get/ip">
     <p>Hatalı URL: ip=1 olmalı</p>
     <p><a href="?im=merhabadunya">Geri</a></p>
    </xsl:when>
    <xsl:otherwise>
     <p><a href="?im=merhabadunya&amp;ip=1">IP'mi göster</a></p>
    </xsl:otherwise>
   </xsl:choose>
  </div>
 </xsl:template>

 <xsl:template name="module-my-page-layout">
  <xsl:copy-of select="$site-layout/document/page[@im='merhabadunya']"/
 </xsl:template>
</xsl:stylesheet>