Archive

Archive for the ‘XSLT’ Category

Paging in xsl

October 22nd, 2009 3 comments

You might have seen a lot of paging options using xml and xsl. In such options you need to pass some parameters from xml and then you do the paging on it. I have implemented such pagings before. But this time it’s a simple example of creating all the paging from xsl.

Recenetly I was using a third party dll to generate xml, my task was to implement paging on returned xml without doing any change in server side code. So here is the solution.

 

<xsl:stylesheet version=1.0 xmlns:xsl=http://www.w3.org/1999/XSL/Transform>

 

      <xsl:output method=html indent=yes omit-xml-declaration=yes />

 

  <!– Define some primary variables–>

  <xsl:param name=qs-p />

  <xsl:variable name=newsCount select=count(rows/row) />

  <xsl:variable name=pageMaxLimit select=10 />

 

  <xsl:template match=/>

    <!– This page constant has been defined just to make some calculation for starting

    number and end number, we will asume constant value as 0 if there is no page variable

    value in query string–>

    <xsl:variable name=page_constant>

      <xsl:choose>

        <xsl:when test=$qs-p = ”>

          <xsl:value-of select=0/>

        </xsl:when>

        <xsl:otherwise>

          <xsl:value-of select=$qs-p – 1/>

        </xsl:otherwise>

      </xsl:choose>

    </xsl:variable>

   

    <xsl:apply-templates select=/rows/row >

      <!– Calculate the records start number and end number, later we will mactch

      records position for starting number and end number to display records for current page–>

      <xsl:with-param name=start_no select=($page_constant * 10 ) + 1 />

      <xsl:with-param name=end_no select=($page_constant * 10 ) + 10 />

    </xsl:apply-templates>

 

    <xsl:if test=$newsCount &gt; 10>

      <xsl:call-template name=paging />

    </xsl:if>

  </xsl:template>

 

  <xsl:template match=row>

    <xsl:param name=start_no />

    <xsl:param name=end_no />

    <!– Display the records for the current page number by making a check on their position –>

    <xsl:if test=position() &gt;= ($start_no) and position() &lt;= ($end_no)>

      <p>

        <a href=/{substring-after(FileRef/text(), ‘#’)} class=bold>

          <xsl:value-of select=Title/>

        </a>

        <br/>

        <xsl:value-of select=Intro/>

      </p>

    </xsl:if>

  </xsl:template>

 

  <xsl:template name=paging>

    <br></br>

    <xsl:variable name=pageCount select=floor($newsCount div 10) + ($newsCount mod 10 != 0) />

    <!–Call the template to create page numbered links,

    send loop iterator ‘i’, page count ‘count’ and current page ‘pageNo’ as parameters.–>

    <xsl:call-template name=page_links>

      <xsl:with-param name=i select=1 />       

      <xsl:with-param name=count select=$pageCount />     

      <xsl:with-param name=pageNo select=$qs-p />

    </xsl:call-template>

  </xsl:template>

  <!–Loop to create page numbers.–>

  <xsl:template name=page_links>

    <xsl:param name=i      />

    <xsl:param name=count  />

    <xsl:param name=pageNo />

   

    <xsl:if test=$i &lt;= $count>

      <xsl:variable name=page_no>

        <xsl:value-of select=$i – 1 />

      </xsl:variable>

      <a href=?p={$i} style=padding-right:2px;>

        <xsl:choose>

          <xsl:when test=$i = $pageNo or $pageNo = ”>

            <strong>

              <xsl:value-of select=$i />

            </strong>

          </xsl:when>

          <xsl:otherwise>

            <xsl:value-of select=$i />

          </xsl:otherwise>

        </xsl:choose>

      </a>

      <xsl:text xml:space=preserve> </xsl:text>

    </xsl:if>

    <xsl:if test=$i &lt;= $count>

      <xsl:call-template name=page_links>

        <xsl:with-param name=i>

          <xsl:value-of select=$i + 1/>

        </xsl:with-param>

        <xsl:with-param name=count>

          <xsl:value-of select=$count/>

        </xsl:with-param>

        <xsl:with-param name=pageNo>

          <xsl:value-of select=$pageNo/>

        </xsl:with-param>

 

      </xsl:call-template>

    </xsl:if>

  </xsl:template> 

</xsl:stylesheet>

Categories: XSLT Tags: , ,

Replace function for xsl

July 17th, 2009 4 comments

If you are using xsl 1.0, you will face small things missing from the functions set. String manipulation is very common and you need in every programming and scripting language. In xsl 1.0 most of the functions are missing. Today I’m going to give you idea that how can we create and use Replace function for strings.

<xsl:template name=string-replace>
   <
xsl:param name=text />
   <
xsl:param name=pattern />
   <
xsl:param name=replace-with />
  
<xsl:choose>
      <
xsl:when test=contains($text, $pattern)>
         <
xsl:value-of select=substring-before($text, $pattern) />
         <
xsl:value-of select=$replace-with />
         <
xsl:call-template name=string-replace>
            <
xsl:with-param name=text select=substring-after($text, $pattern) />
            <
xsl:with-param name=pattern select=$pattern />
            <
xsl:with-param name=replace-with select=$replace-with />
         </
xsl:call-template>
      </
xsl:when>
      <
xsl:otherwise>
         <
xsl:value-of select=$text />
      </
xsl:otherwise>
   </
xsl:choose>
</
xsl:template>

Here how to use this string-replace function. Here I’m creating a variable name MYTextReplaced, and then calling the function replace-with. I’m passing the orignal text (MyText), pattern string ([Title]) to search and replace with value (Replaced-text).

For example value of MyText is “This is a sample [Title], created by Salman”.

<xsl:variable name=MyTextReplaced>
   <
xsl:call-template name=string-replace>
      <
xsl:with-param name=text select=xml/MyText />
      <
xsl:with-param name=pattern select=‘[Title]‘ />
      <
xsl:with-param name=replace-with select=‘Replaced-Text’ />
   </
xsl:call-template>
</
xsl:variable>

Now here I’m displaying the replaced string. The output string will be “This is a sample Replaced-Text, created by Salman”. One more thing to notice is that this is a replace-all method and will replace all the occurrence of searched text.
<xsl:value-of select=$MyTextReplaced disable-output-escaping=yes />
 
I hope you enjoy it.

Cheers.

Categories: XSLT Tags: , ,

Use querystring values in xsl

July 17th, 2009 Comments off

It’s just a small tip of the day. Sometime when you work with xsl you need to use querystring values for some processing or display. It’s very simple to use querystring values without posting them from server side code in your xml document.

On top of your xsl document just define a variable like this

<xsl:param name=qs-title/>

here title is a querystring variable and now you can use this xsl variable any where like this

<xsl:value-of select=$qs-title />
 

 

Is not it simple enough?

Categories: XSLT Tags: ,