Saturday, December 18, 2010

Transforming Documents with XSLT

Transforming Documents with XSLT

Extensible Stylesheet Language Transformations, or XSLT, is a straightforward language that allows you to transform existing XML documents into new XML, Hypertext Markup Language (HTML), Extensible Hypertext Markup Language (XHTML), or plain text documents. XML Path Language, or XPath, is a companion technology to XSLT that helps identify and find nodes in XML documents—elements, attributes, and other structures. If you want to deep learning or develop applications with XSLT using
one of the programming languages then you have to download one of the free e-books from eLibrary2you.com

Here are a few ways you can put XSLT to work:

  1. Transforming an XML document into an HTML or XHTML document for display in a web browser;
  2. Converting from one markup vocabulary to another;
  3. Extracting plain text out of an XML document for use in a non-XML application or environment;
  4. Building a new German language document by pulling and repurposing all the German text from a multilingual XML document;

This is barely a start. There are many other ways that you can use XSLT, and you'll get acquainted with a number of them in the chapters that follow.

This articles assumes that you don't know much about XSLT, but that you are ready to put it to work. Through a series of numerous hands-on examples, Learning XSLT guides you through many features of XSLT 1.0 and XPath 1.0, while at the same time introducing you to XSLT 2.0 and XPath 2.0.

If you don't know much about XML yet, it shouldn't be a problem because I'll try also cover many of the basics of XML in this articles. The XML specification is located at http://www.w3.org/TR/REC-xml.html.

Another specification closely related to XSLT is Extensible Stylesheet Language, or XSL, commonly referred to as XSL-FO (see http://www.w3.org/TR/xsl/). XSL-FO is a language for applying styles and formatting to XML documents. It is similar to Cascading Style Sheets (CSS), but it is written in XML and is somewhat more extensive. (FO is short for formatting objects.) Initially, XSLT and XSL-FO were developed in a single specification, but they were later split into separate initiatives.

Monday, February 22, 2010

Introduction

When you have to display a large number of records, the common practice is to use data paging so the information can be presented in a more user-friendly manner. There are many solutions one can use to implement such a system, and each of them has its own advantages and disadvantages. One of the excellent ways of implementing this solution is using XML and XSL. In this article, I show you how implement this functionality in XSLT. If you want to deep learning or develop applications with XSLT using
one of the programming languages then you have to download one of the free e-books from eLibrary2you.com
Let's start.

Results:

Using the code


There is XML data below:

 version="1.0" encoding="ISO-8859-1"?>

<data>
<xdata>
Any data
</xdata>
<total_results count="105"/>
<displayCount>20</displayCount>
<pageNumber>1</pageNumber>
</data>

To implement paging for the above situation, we need to be able to pass parameters to the stylesheet, and these parameters can be used to specify information such as the number of records to be displayed, the page number to be displayed, as well as the total number of records. Accomplishing this requires the following two steps.

  • Declaring parameters in the XSLT stylesheet
  • Supplying parameters to the stylesheet
For this example, let us create a new XSLT stylesheet named paging.xsl and the code for the XSLT stylesheet looks like the following. (And the code itself is given below)

<xsl:template name="Page">

<xsl:param name="pCurrentPageNumber" select="pageNumber"/>
<xsl:param name="pRecordsPerPage" select="displayCount"/>
<xsl:param name="pPecordCount" select="total_results/@count"/>
<xsl:param name="pNextPageNumber"/>
<xsl:choose>
<xsl:when test="$pCurrentPageNumber=$pNextPageNumber">
<li>
<a class="act" href="#">
<xsl:value-of select="$pNextPageNumber"/>
</a>
</li>
</xsl:when>
<xsl:when test="((number($pNextPageNumber)-1)*number($pRecordsPerPage)) <= number($pPecordCount)">
<li>
<a>
<xsl:attribute name="href">
<xsl:value-of select="concat('?use_jump=',$pCurrentPageNumber,'&jump_first_car_F=',1+((number($pNextPageNumber)-1) * $pRecordsPerPage),'&current_page_this_search_F=',$pNextPageNumber)"/>
</xsl:attribute>
<xsl:value-of select="$pNextPageNumber"/>
</a>
</li>
</xsl:when>
</xsl:choose>
</xsl:template>

<xsl:template name="for.loop">
<xsl:param name="i"/>
<xsl:param name="count"/>
<xsl:param name="currentPage"/>

<xsl:if test="$i <= $count">
<xsl:call-template name="Page">
<xsl:with-param name="pNextPageNumber" select="$i"/>
</xsl:call-template>
<xsl:call-template name="for.loop">
<xsl:with-param name="i" select="$i+1"/>
<xsl:with-param name="count" select="$count"/>
<xsl:with-param name="currentPage" select="$currentPage"/>
</xsl:call-template>
</xsl:if>
</xsl:template>

<xsl:template name="Paging">
<xsl:param name="pCurrentPageNumber" select="pageNumber"/>
<xsl:param name="pRecordsPerPage" select="displayCount"/>
<xsl:param name="pPecordCount" select="total_results/@count"/>

<xsl:if test="$pPecordCount > $pRecordsPerPage">


<xsl:if test="$pCurrentPageNumber > 1">
<li>
<xsl:variable name="PrePageNumber" select="number($pCurrentPageNumber)-2"/>
<a>
<xsl:attribute name="href">
<xsl:value-of select="concat('?use_jump=',$pCurrentPageNumber,'&jump_first_car_F=', 1+$PrePageNumber*$pRecordsPerPage ,'&current_page_this_search_F=', $PrePageNumber+1 )"/>
</xsl:attribute>
Previous
<xsl:value-of select="$pRecordsPerPage" />
</a>
</li>
</xsl:if>


<xsl:variable name="pageCount" select="ceiling($pPecordCount div $pRecordsPerPage)"/>
<xsl:variable name="nrOfPagesToDisplay" select="10"/>
<xsl:variable name="temp" select="$pCurrentPageNumber+$nrOfPagesToDisplay"/>
<xsl:variable name="middel" select="ceiling($temp div 2)-1"/>
<xsl:variable name="start" select="number($middel)-5"/>
<xsl:choose>
<xsl:when test="$pCurrentPageNumber > 5 and $start > 5">
<xsl:call-template name="for.loop">
<xsl:with-param name="i" select="number($start)-5"/>
<xsl:with-param name="count" select="($pCurrentPageNumber+$nrOfPagesToDisplay)-1"/>
<xsl:with-param name="currentPage" select="$pCurrentPageNumber"/>
</xsl:call-template>
</xsl:when>
<xsl:when test="$start=0 or ($pageCount <= $nrOfPagesToDisplay)">
<xsl:call-template name="for.loop">
<xsl:with-param name="i" select="1"/>
<xsl:with-param name="count" select="$nrOfPagesToDisplay"/>
<xsl:with-param name="currentPage" select="$pCurrentPageNumber"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:call-template name="for.loop">
<xsl:with-param name="i" select="$start"/>
<xsl:with-param name="count" select="($start+$nrOfPagesToDisplay)-1"/>
<xsl:with-param name="currentPage" select="$pCurrentPageNumber"/>
</xsl:call-template>
</xsl:otherwise>
</xsl:choose>




<xsl:if test="$pageCount > $pCurrentPageNumber">
<li>
<a class="end">
<xsl:attribute name="href">
<xsl:value-of select="concat('?use_jump=',$pCurrentPageNumber,'&jump_first_car_F=', 1+($pCurrentPageNumber* $pRecordsPerPage) ,'&current_page_this_search_F=',$pCurrentPageNumber+1)"/>
</xsl:attribute>
Next
<xsl:value-of select="$pRecordsPerPage" />
</a>
</li>
</xsl:if>

</xsl:if>
</xsl:template>

The template for.loop implement "for(int=0; i<> and the parametrs i is start index and the parametr count is length.

How can use it

In order to use the paging template you only have to call it with parametrs. The pCurrentPageNumber parametr is current page number or selected page number, the pRecordsPerPage parametr is the number of records per page, the pPecordCount parametr is record count. For example:


<xsl:call-template name="Paging">

<xsl:with-param name="pCurrentPageNumber" select="pageNumber"/>
<xsl:with-param name="pRecordsPerPage" select="displayCount"/>
<xsl:with-param name="pPecordCount" select="total_results/@count"/>
</xsl:call-template>



Monday, January 25, 2010

When you have to display a large number of records, the common practice is to use data paging so the information can be presented in a more user-friendly manner. There are many solutions one can use to implement such a system, and each of them has its own advantages and disadvantages. One of the excellent ways of implementing this solution is using XML and XSL. In this article, I show you how implement this functionality in XSLT. Let's start.

There is XML data below:

<?xml version="1.0" encoding="ISO-8859-1"?>
<data>
<xdata>
Any data
</xdata>
<total_results count="105"/>
<displayCount>20</displayCount>
<pageNumber>1</pageNumber>
</data>

To implement paging for the above situation, we need to be able to pass parameters to the stylesheet, and these parameters can be used to specify information such as the number of records to be displayed, the page number to be displayed, as well as the total number of records. Accomplishing this requires the following two steps.
* Declaring parameters in the XSLT stylesheet
* Supplying parameters to the stylesheet

For this example, let us create a new XSLT stylesheet named paging.xsl and the code for the XSLT stylesheet looks like the following.

<xsl:template name="Page">
<xsl:param name="pCurrentPageNumber" select="pageNumber"/>
<xsl:param name="pRecordsPerPage" select="displayCount"/>
<xsl:param name="pPecordCount" select="total_results/@count"/>
<xsl:param name="pNextPageNumber"/>
<xsl:choose>
  <xsl:when test="$pCurrentPageNumber=$pNextPageNumber">
   <li>
    <a class="act" href="#">
     <xsl:value-of select="$pNextPageNumber"/>
    </a>
   </li>
  </xsl:when>
  <xsl:when test="((number($pNextPageNumber)-1)*number($pRecordsPerPage)) <= number($pPecordCount)">
   <li>
    <a>
     <xsl:attribute name="href">
      <xsl:value-of select="concat('?use_jump=',$pCurrentPageNumber,'&jump_first_car_F=',1+((number($pNextPageNumber)-1) * $pRecordsPerPage),'&current_page_this_search_F=',$pNextPageNumber)"/>
     </xsl:attribute>
     <xsl:value-of select="$pNextPageNumber"/>
    </a>
   </li>
  </xsl:when>
</xsl:choose>
</xsl:template>

<xsl:template name="for.loop">
<xsl:param name="i"/>
<xsl:param name="count"/>
<xsl:param name="currentPage"/>

<xsl:if test="$i <= $count">
  <xsl:call-template name="Page">
   <xsl:with-param name="pNextPageNumber" select="$i"/>
  </xsl:call-template>
  <xsl:call-template name="for.loop">
   <xsl:with-param name="i" select="$i+1"/>
   <xsl:with-param name="count" select="$count"/>
   <xsl:with-param name="currentPage" select="$currentPage"/>
  </xsl:call-template>
</xsl:if>
</xsl:template>

<xsl:template name="Paging">
<xsl:param name="pCurrentPageNumber" select="pageNumber"/>
<xsl:param name="pRecordsPerPage" select="displayCount"/>
<xsl:param name="pPecordCount" select="total_results/@count"/>

<xsl:if test="$pPecordCount > $pRecordsPerPage">

 
  <xsl:if test="$pCurrentPageNumber > 1">
   <li>
    <xsl:variable name="PrePageNumber" select="number($pCurrentPageNumber)-2"/>
    <a>
     <xsl:attribute name="href">
      <xsl:value-of select="concat('?use_jump=',$pCurrentPageNumber,'&jump_first_car_F=', 1+$PrePageNumber*$pRecordsPerPage ,'&current_page_this_search_F=', $PrePageNumber+1 )"/>
     </xsl:attribute>
     Previous <xsl:value-of select="$pRecordsPerPage" />
    </a>
   </li>
  </xsl:if>

 
  <xsl:variable name="pageCount" select="ceiling($pPecordCount div $pRecordsPerPage)"/>
  <xsl:variable name="nrOfPagesToDisplay" select="10"/>
  <xsl:variable name="temp" select="$pCurrentPageNumber+$nrOfPagesToDisplay"/>
  <xsl:variable name="middel" select="ceiling($temp div 2)-1"/>
  <xsl:variable name="start" select="number($middel)-5"/>
  <xsl:choose>
   <xsl:when test="$pCurrentPageNumber > 5 and $start > 5">
    <xsl:call-template name="for.loop">
     <xsl:with-param name="i" select="number($start)-5"/>
     <xsl:with-param name="count" select="($pCurrentPageNumber+$nrOfPagesToDisplay)-1"/>
     <xsl:with-param name="currentPage" select="$pCurrentPageNumber"/>
    </xsl:call-template>
   </xsl:when>
   <xsl:when test="$start=0 or ($pageCount <= $nrOfPagesToDisplay)">
    <xsl:call-template name="for.loop">
     <xsl:with-param name="i" select="1"/>
     <xsl:with-param name="count" select="$nrOfPagesToDisplay"/>
     <xsl:with-param name="currentPage" select="$pCurrentPageNumber"/>
    </xsl:call-template>
   </xsl:when>
   <xsl:otherwise>
    <xsl:call-template name="for.loop">
     <xsl:with-param name="i" select="$start"/>
     <xsl:with-param name="count" select="($start+$nrOfPagesToDisplay)-1"/>
     <xsl:with-param name="currentPage" select="$pCurrentPageNumber"/>
    </xsl:call-template>
   </xsl:otherwise>
  </xsl:choose>
 

 
 
  <xsl:if test="$pageCount > $pCurrentPageNumber">
   <li>
    <a class="end">
     <xsl:attribute name="href">
      <xsl:value-of select="concat('?use_jump=',$pCurrentPageNumber,'&jump_first_car_F=', 1+($pCurrentPageNumber* $pRecordsPerPage) ,'&current_page_this_search_F=',$pCurrentPageNumber+1)"/>
     </xsl:attribute>
     Next <xsl:value-of select="$pRecordsPerPage" />
    </a>
   </li>
  </xsl:if>

</xsl:if>
</xsl:template>


* This source code was highlighted with Source Code Highlighter.


The template for.loop implement "for(int=0; i<> and the parametrs i is start index and the parametr count is length.

How can use it

In order to use the paging template you only have to call it with parametrs. The pCurrentPageNumber parametr is current page number or selected page number, the pRecordsPerPage parametr is the number of records per page,the pPecordCount parametr is record count.

For example:


<xsl:call-template name="Paging">

<xsl:with-param name="pCurrentPageNumber" select="pageNumber"/>
<xsl:with-param name="pRecordsPerPage" select="displayCount"/>
<xsl:with-param name="pPecordCount" select="total_results/@count"/>
</xsl:call-template>