How to make pagination in XSLT - xslt

I have the following XSLT:
<xsl:template match="/">
<div id="dokumentliste">
<xsl:variable name="alleNyheder" select="$currentPage//node" />
<xsl:for-each select="$alleNyheder">
<xsl:sort data-type="text" select="#createDate" order="descending" />
<xsl:if test="./data[#alias='manchet'] != ''">
<div class="newsitem">
<h2>
<xsl:value-of select="./data[#alias='title']"/>
</h2>
<xsl:if test="./data[#alias = 'manchet'] != ''">
<div class="nyhedContent">
<p>
<span class="dokumentListeDato">
<xsl:choose>
<xsl:when test="./data[#alias='date'] != ''">
<xsl:value-of select="umbraco.library:FormatDateTime(./data[#alias='date'], 'dd. MMMM yyyy')"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="umbraco.library:FormatDateTime(./#createDate, 'dd. MMMM yyyy')"/>
</xsl:otherwise>
</xsl:choose>
</span>
<xsl:value-of select="./data[#alias = 'manchet']"/>
</p>
</div>
</xsl:if>
<div class="dokumentListe_laes_mere">
<a href="{umbraco.library:NiceUrl(#id)}">
Læs mere<img src="/frontend/images/macro/macro_laes_mere.png" alt="Læs mere"/>
</a>
</div>
</div>
<!-- End newsitem -->
</xsl:if>
</xsl:for-each>
</div>
</xsl:template>
I am making a newslist, and would like to make some sort of pagination. Almost the same one as seen on Google. You know "the usual one".
But I can't figure out how to do this.
The number of newsitems on each page isn't that important, but lets say 10 on each page. When the 10 first newsitems are shown, I would like the pagination to show up. With the "Next" and "Previous" buttons to the right and the left of the numbers.
Is it possible to make this, and have I explained my problem good enough? I use the Umbraco CMS by the way :)
Thank you very much.
-Kim

Something like this:
I've left some code in there for dealing with images in your listing too :-)
<xsl:output method="xml" omit-xml-declaration="yes"/>
<xsl:param name="currentPage"/>
<xsl:template match="/">
<xsl:variable name="recordsPerPage" select="2"/>
<xsl:variable name="pageNumber">
<xsl:choose>
<!-- first page -->
<xsl:when test="umbraco.library:RequestQueryString('page') <= 0 or string(umbraco.library:RequestQueryString('page')) = '' or string(umbraco.library:RequestQueryString('page')) = 'NaN'">0</xsl:when>
<!-- what was passed in -->
<xsl:otherwise>
<xsl:value-of select="umbraco.library:RequestQueryString('page')"/>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:variable name="numberOfRecords" select="count($currentPage/node)"/>
<!-- The fun starts here -->
<xsl:call-template name="pagination">
<xsl:with-param name="pageNumber" select="$pageNumber"/>
<xsl:with-param name="recordsPerPage" select="$recordsPerPage" />
<xsl:with-param name="numberOfRecords" select="$numberOfRecords" />
</xsl:call-template>
<ul class="listing self-clear">
<xsl:for-each select="$currentPage/node [string(data [#alias='umbracoNaviHide']) != '1']">
<xsl:sort order="descending" select="data[#alias='releasedOn']"></xsl:sort>
<xsl:if test="position() > $recordsPerPage * number($pageNumber) and position() <= number($recordsPerPage * number($pageNumber) + $recordsPerPage )">
<li>
<xsl:attribute name="class">
<xsl:if test="data[#alias='image'] = ''">
no-img
</xsl:if>
<xsl:if test="position() = $recordsPerPage * (number($pageNumber) + 1)">
last
</xsl:if>
</xsl:attribute>
<h3>
<a href="{umbraco.library:NiceUrl(#id)}">
<xsl:value-of select="#nodeName"/>
</a>
</h3>
<xsl:if test="data[#alias='image'] != ''">
<img src="{data[#alias='image']}" class="drop-shadow" />
</xsl:if>
<p class="date"><xsl:value-of select="umbraco.library:LongDate(data[#alias='releasedOn'])"/></p>
<xsl:value-of select="data[#alias='abstract']" disable-output-escaping="yes"/>
Read More
</li>
</xsl:if>
</xsl:for-each>
</ul>
<xsl:call-template name="pagination">
<xsl:with-param name="pageNumber" select="$pageNumber"/>
<xsl:with-param name="recordsPerPage" select="$recordsPerPage" />
<xsl:with-param name="numberOfRecords" select="$numberOfRecords" />
</xsl:call-template>
</xsl:template>
<xsl:template name="pagination">
<xsl:param name="pageNumber"/>
<xsl:param name="recordsPerPage"/>
<xsl:param name="numberOfRecords"/>
<div class="pagination">
<div class="wrapper">
<xsl:if test="(($pageNumber +1 ) * $recordsPerPage) < ($numberOfRecords)">
Next
</xsl:if>
<xsl:if test="$pageNumber > 0">
Prev
</xsl:if>
<span class="page-nos">
Page
<xsl:call-template name="for.loop">
<xsl:with-param name="i">1</xsl:with-param>
<xsl:with-param name="page" select="$pageNumber +1"></xsl:with-param>
<xsl:with-param name="count" select="ceiling(count($currentPage/node)div $recordsPerPage)"></xsl:with-param>
</xsl:call-template>
</span>
</div>
</div>
</xsl:template>
<xsl:template name="for.loop">
<xsl:param name="i"/>
<xsl:param name="count"/>
<xsl:param name="page"/>
<xsl:if test="$i <= $count">
<span>
<xsl:if test="$page != $i">
<a href="{umbraco.library:NiceUrl($currentPage/#id)}?page={$i - 1}" >
<xsl:value-of select="$i" />
</a>
</xsl:if>
<xsl:if test="$page = $i">
<xsl:value-of select="$i" />
</xsl:if>
</span>
</xsl:if>
<xsl:if test="$i <= $count">
<xsl:call-template name="for.loop">
<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="page">
<xsl:value-of select="$page"/>
</xsl:with-param>
</xsl:call-template>
</xsl:if>
</xsl:template>

I figured this one out now. I can see that you have just copy/pasted the pagination that where made by Tim Geyssens here: http://www.nibble.be/?p=11
And the code is also kind'a good, but I changed some of it to get it working. I don't know if I should just accept my own answer as the right one, the answer from Myster as the right one or if I can delete this post?

Related

XSLT list item position issue

pls check the below code. I am matching the list element and as per the below code the position() function returns correct number of the element with in ul elements i.e 1,2,3 where as if i do the test with position i get only ss,ss,ss . Can anyone let me know where am missing ?
XSL-FO
<xsl:template match="html:li" priority="2">
<fo:list-item>
...
<xsl:value-of select="position()"/> -- this returns 1,2,3
<xsl:if test="position() = 1">
<xsl:text>ss</xsl:text> -- only executes
</xsl:if>
<xsl:if test="position() = 2">
<xsl:text>ssddddd</xsl:text> -- does not only execute
</xsl:if>
<xsl:if test="position() = 3">
<xsl:text>sskkkkkkkkk</xsl:text> -- does not only execute
</xsl:if>
</fo:list-item>
</xsl:template
XSL
<xsl:template match="list">
<xsl:if test="list.item">
<xsl:variable name="styleAttr">
<xsl:text>margin-top: 1em;</xsl:text>
</xsl:variable>
<div>
<xsl:if test="string-length($styleAttr) > 0">
<xsl:attribute name="style">
<xsl:value-of select="$styleAttr"/>
</xsl:attribute>
</xsl:if>
<ul>
<xsl:apply-templates select="node()[not(self::list)]" />
</ul>
</div>
</xsl:if>
</xsl:template>
<xsl:template match="list.item" priority="1">
<li style="padding: 0;">
<div style="margin-bottom: 0.5em;">
<xsl:apply-templates />
<xsl:if test="following-sibling::node()[1][self::list]">
<xsl:apply-templates select="following-sibling::node()[1]" />
</xsl:if>
</div>
</li>
</xsl:template>

How to display sitemap/navigation for a site using xslt?

I have a website that has pages and subpages as in the image attached.
Some of the subpages belong to First pagge(About Patient Direct), they have setted 'do not show in menu'.
All right, I want to make an xslt file that will generate an html content like this:
Menu Item 1 (including Home page - About Patient Direct)
-submenu page 1 1
Menu Item 2 (including Home page - About Patient Direct)
-submenu page 2 1
-submenu page 2 2
How can I do that?
This is what I have so far
<?xml version="1.0" encoding="UTF-8"?>
]>
<xsl:output method="xml" omit-xml-declaration="yes" />
<xsl:param name="currentPage"/>
<!-- Input the documenttype you want here -->
<xsl:variable name="level" select="1"/>
<xsl:template match="/">
<xsl:if test="$currentPage/#id = $currentPage/ancestor-or-self::* [#level=$level]/#id">
<div class="column">
<h1>
<a href="#">
Home Page - I don't know what to write here
</a>
</h1>
</div>
</xsl:if>
<xsl:for-each select="$currentPage/ancestor-or-self::* [#level=$level]/* [#isDoc and string(umbracoNaviHide) != '1']">
<div class="column">
<h1>
<xsl:choose>
<xsl:when test="name() = 'Link'">
<a href="{current()/linkUrl}" target="_blank">
<xsl:value-of select="#nodeName" />
</a>
</xsl:when>
<xsl:otherwise>
<a href="{umbraco.library:NiceUrl(#id)}">
<xsl:value-of select="#nodeName" />
</a>
</xsl:otherwise>
</xsl:choose>
</h1>
</div>
</xsl:for-each>
</xsl:template>
I have also opened a discussion on http://our.umbraco.org/forum/developers/xslt/33326-How-to-display-sitemapnavigation-for-a-site-using-xslt
I have finally succeeded to do what I have been looking for. Here is the code for those who might be looking for same functionality
<?xml version="1.0" encoding="UTF-8"?>
]>
<xsl:template name="menu">
<xsl:param name="level"/>
<xsl:variable name="RootNode" select="umbraco.library:GetXmlNodeById(1050)" />
<div class="column">
<h1>
<a href="{umbraco.library:NiceUrl($RootNode/#id)}" style="width:200px;">
<xsl:value-of select="$RootNode/#nodeName"/>
</a>
</h1>
<xsl:call-template name="submenu_Homepage">
</xsl:call-template>
</div>
<xsl:if test="count($currentPage/ancestor-or-self::* [#level=$level]/* [#isDoc and string(umbracoNaviHide) != '1']) > '0'">
<xsl:for-each select="$currentPage/ancestor-or-self::* [#level=$level]/* [#isDoc and string(umbracoNaviHide) != '1']">
<div class="column">
<h1>
<xsl:choose>
<xsl:when test="name() = 'Link'">
<a href="{current()/linkUrl}" target="_blank">
<xsl:value-of select="#nodeName" />
</a>
</xsl:when>
<xsl:otherwise>
<a href="{umbraco.library:NiceUrl(#id)}">
<xsl:value-of select="#nodeName" />
</a>
</xsl:otherwise>
</xsl:choose>
</h1>
<xsl:if test="count(current()/* [#isDoc and string(umbracoNaviHide) != '1']) > '0'">
<xsl:call-template name="submenu">
<xsl:with-param name="level" select="$level+1"/>
</xsl:call-template>
</xsl:if>
</div>
</xsl:for-each>
</xsl:if>
</xsl:template>
<xsl:template name="submenu">
<xsl:param name="level"/>
<ul class="level_{#level}">
<xsl:for-each select="current()/*[#isDoc and string(umbracoNaviHide) != '1']">
<li>
<xsl:if test="position() != last()">
<xsl:attribute name="class">bottom_border</xsl:attribute>
</xsl:if>
<a href="{umbraco.library:NiceUrl(#id)}">
<xsl:value-of select="#nodeName"/>
</a>
<!--case when we have third menu level-->
<xsl:if test="count(current()/* [#isDoc and string(umbracoNaviHide) != '1']) > '0'">
<xsl:call-template name="submenu">
<xsl:with-param name="level" select="$level+1"/>
</xsl:call-template>
</xsl:if>
</li>
</xsl:for-each>
</ul>
</xsl:template>
<xsl:template name="submenu_Homepage">
<ul>
<xsl:for-each select="$currentPage/ancestor-or-self::*/* [#isDoc and string(umbracoNaviHide) = '1']">
<li>
<xsl:if test="position() != last()">
<xsl:attribute name="class">bottom_border</xsl:attribute>
</xsl:if>
<xsl:choose>
<xsl:when test="name() = 'Link'">
<a href="{current()/linkUrl}" target="_blank">
<xsl:value-of select="#nodeName" />
</a>
</xsl:when>
<xsl:otherwise>
<a href="{umbraco.library:NiceUrl(#id)}">
<xsl:value-of select="#nodeName" />
</a>
</xsl:otherwise>
</xsl:choose>
</li>
</xsl:for-each>
</ul>
</xsl:template>

Umbraco Navigation using XSLT macro - issue with childless nodes

I have implemented the superfish navigation menu into an umbraco installation. Superfish simply takes a UL element and turns it into a hierarchical menu that shows child items when you hover over the parent (you remember when they were cool back in 1999 right?).
I cannot figure out why, on certain pages (usually ones without children), the menu does not show child items for any page. My exposure to XSLT is minimal, so i must be overlooking some logic.
You can see the actual site here Hover over 'personal training' to see the menu work, now click on 'weight management' and hey-presto the magic stops happening.
The XSLT that creates the UL structure is below, and the HTML page source tells me that its simply not generating any LI elements for child pages when the issue occurs.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE xsl:stylesheet [ <!ENTITY nbsp " "> ]>
<xsl:stylesheet
version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxml="urn:schemas-microsoft-com:xslt"
xmlns:umbraco.library="urn:umbraco.library"
exclude-result-prefixes="msxml umbraco.library">
<xsl:output method="html" omit-xml-declaration="yes"/>
<xsl:param name="currentPage" />
<!--This sets the level that the nav starts at and tells us if we should recurse through child elements-->
<xsl:variable name="startDepth" select="/macro/startingLevel" />
<xsl:variable name="recurse" select="/macro/recurse" />
<xsl:variable name="selectBranches" select="/macro/selectBranches"></xsl:variable>
<xsl:variable name="maxMenuDepth" select="/macro/maxMenuDepth"></xsl:variable>
<xsl:variable name="forceNode" select="/macro/forceNode"></xsl:variable>
<xsl:variable name="walkChildren" select="/macro/expandChildren"></xsl:variable>
<xsl:variable name="forceHome" select="/macro/forceHome"></xsl:variable>
<xsl:variable name="securityTrimming" select="/macro/securityTrimming"></xsl:variable>
<!--Alternate page title variable in here-->
<!--Styles for the navigation-->
<xsl:variable name="ulBaseClass" select="/macro/ulBaseClass"></xsl:variable>
<xsl:variable name="branchClass" select="/macro/branchClass"></xsl:variable>
<xsl:variable name="selectedClass" select="/macro/selectedClass"></xsl:variable>
<xsl:variable name="startLevel">
<xsl:choose>
<xsl:when test="$startDepth >= 0">
<xsl:value-of select="$startDepth"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$currentPage/#level"/>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<!--This calls first iteration of the navigation, sending the first node at the correct depth found in the ancestors of the current page-->
<xsl:template match="/">
<xsl:choose>
<xsl:when test="$forceNode">
<xsl:variable name="currentNode" select="umbraco.library:GetXmlNodeById($forceNode)"></xsl:variable>
<xsl:call-template name="nodeIterator">
<xsl:with-param name="parentNode" select="$currentNode/ancestor-or-self::*[#isDoc][#level=$startLevel]
[
string(umbracoNaviHide) != '1'
and ($securityTrimming != '1'
or umbraco.library:IsProtected(#id, #path) = false()
or umbraco.library:HasAccess(#id, #path) = true())
]" />
<xsl:with-param name="pseudoCurrentPage" select="$currentNode" />
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:variable name="currentNode" select="$currentPage"></xsl:variable>
<xsl:call-template name="nodeIterator">
<xsl:with-param name="parentNode" select="$currentNode/ancestor-or-self::*[#isDoc][#level=$startLevel]
[
string(umbracoNaviHide) != '1'
and ($securityTrimming != '1'
or umbraco.library:IsProtected(#id, #path) = false()
or umbraco.library:HasAccess(#id, #path) = true())
]" />
<xsl:with-param name="pseudoCurrentPage" select="$currentNode" />
</xsl:call-template>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template name="nodeIterator">
<xsl:param name="parentNode" />
<xsl:param name="pseudoCurrentPage" />
<!-- do not show info doc node types-->
<xsl:variable name="calculatedMenuDepth" select="($parentNode/#level - $startLevel)+1" />
<xsl:if test="$parentNode/*[#isDoc] or ($calculatedMenuDepth = 1 and $forceHome)">
<ul>
<xsl:attribute name="class">
<xsl:choose>
<xsl:when test="$calculatedMenuDepth = 1">
<xsl:value-of select="$ulBaseClass" />
</xsl:when>
<!--<xsl:when test="$calculatedMenuDepth = 1">
<xsl:value-of select="concat($ulBaseClass, ' lv', $calculatedMenuDepth)" />
</xsl:when>
<xsl:when test="$calculatedMenuDepth > 1">
<xsl:value-of select="concat('lv', $calculatedMenuDepth)" />
</xsl:when>-->
</xsl:choose>
</xsl:attribute>
<xsl:if test="$forceHome = 1 and $calculatedMenuDepth = 1">
<!-- Create the class for the li element-->
<li>
<xsl:variable name="isHomeSelected">
<xsl:choose>
<xsl:when test="$currentPage/ancestor-or-self::*[#isDoc][#level=1]/#id = $currentPage/#id">1</xsl:when>
</xsl:choose>
</xsl:variable>
<xsl:call-template name="cssClassConstructor">
<xsl:with-param name="isSelected" select="$isHomeSelected" />
<xsl:with-param name="isSelectedBranch" select="0" />
<xsl:with-param name="hasChildren" select="1" />
<xsl:with-param name="selectedClass" select="$selectedClass" />
<xsl:with-param name="branchClass" select="$branchClass" />
</xsl:call-template>
<a href="{umbraco.library:NiceUrl($currentPage/ancestor-or-self::*[#isDoc][#level=1]/#id)}">
<xsl:call-template name="cssClassConstructor">
<xsl:with-param name="isSelected" select="$isHomeSelected" />
<xsl:with-param name="isSelectedBranch" select="0" />
<xsl:with-param name="hasChildren" select="0" />
<xsl:with-param name="selectedClass" select="$selectedClass" />
<xsl:with-param name="branchClass" select="$branchClass" />
</xsl:call-template>
<!--set the innerText for the a element-->
<xsl:value-of select="$currentPage/ancestor-or-self::*[#isDoc][#level=1]/text()"/>
<xsl:if test="string($currentPage/ancestor-or-self::*[#isDoc][#level=1]/text()) = ''">
<xsl:value-of select="$currentPage/ancestor-or-self::*[#isDoc][#level=1]/#nodeName"/>
</xsl:if>
</a>
</li>
</xsl:if>
<!--End force home-->
<!--for each node in the parent node that is not hidden by Umbraco-->
<xsl:for-each select="$parentNode/*[#isDoc][
string(umbracoNaviHide) != '1'
and ($securityTrimming != '1'
or umbraco.library:IsProtected(#id, #path) = false()
or umbraco.library:HasAccess(#id, #path) = true())
]">
<!--Set the current node id i.e. the node we have looped to not the current page-->
<xsl:variable name="currentNodeID" select="#id" />
<!--Is the node a branch? i.e. are there children and is it in the colletion of ancestor nodes -->
<xsl:variable name="isBranch">
<xsl:choose>
<xsl:when test="$currentPage/ancestor-or-self::*[#isDoc][#id = $currentNodeID]/child::*[#isDoc]">1</xsl:when>
</xsl:choose>
</xsl:variable>
<!--Is the node selected? i.e. is it the same as the currentPage node-->
<xsl:variable name="isSelected">
<xsl:choose>
<xsl:when test="$currentPage/#id = $currentNodeID">1</xsl:when>
<!-- parent selected -->
<xsl:when test="$pseudoCurrentPage/#id = $currentNodeID">1</xsl:when>
</xsl:choose>
</xsl:variable>
<xsl:variable name="isSelectedBranch">
<xsl:choose>
<xsl:when test="$isBranch = 1 and $selectBranches = 1">1</xsl:when>
</xsl:choose>
</xsl:variable>
<xsl:variable name="hasChildren">
<xsl:choose>
<xsl:when test="./*[#isDoc]">1</xsl:when>
</xsl:choose>
</xsl:variable>
<li>
<!-- Create the class attribute for the element-->
<xsl:call-template name="cssClassConstructor">
<xsl:with-param name="isSelected" select="$isSelected" />
<xsl:with-param name="isSelectedBranch" select="$isSelectedBranch" />
<xsl:with-param name="hasChildren" select="$hasChildren" />
<xsl:with-param name="selectedClass" select="$selectedClass" />
<xsl:with-param name="branchClass" select="$branchClass" />
</xsl:call-template>
<a href="{umbraco.library:NiceUrl(#id)}">
<xsl:call-template name="cssClassConstructor">
<xsl:with-param name="isSelected" select="$isSelected" />
<xsl:with-param name="isSelectedBranch" select="$isSelectedBranch" />
<xsl:with-param name="hasChildren" select="0" />
<xsl:with-param name="selectedClass" select="$selectedClass" />
<xsl:with-param name="branchClass" select="$branchClass" />
</xsl:call-template>
<!--set the innerText for the a element-->
<xsl:value-of select="./pageTitle/text()"/>
<xsl:if test="string(./pageTitle/text()) = ''">
<xsl:value-of select="#nodeName"/>
</xsl:if>
</a>
<!-- if it's a branch recurse through it's children-->
<xsl:if test="((($isBranch = 1 and $recurse = 1) or ($walkChildren = 1 and $pseudoCurrentPage/descendant-or-self::*[#isDoc][#id = $currentNodeID]/child::*[#isDoc])) and $maxMenuDepth > $calculatedMenuDepth)">
<xsl:call-template name="nodeIterator">
<xsl:with-param name="parentNode" select="." />
<xsl:with-param name="pseudoCurrentPage" select="$pseudoCurrentPage" />
</xsl:call-template>
</xsl:if>
</li>
</xsl:for-each>
</ul>
</xsl:if>
</xsl:template>
<xsl:template name="cssClassConstructor">
<xsl:param name="isSelected"></xsl:param>
<xsl:param name="isSelectedBranch"></xsl:param>
<xsl:param name="hasChildren"></xsl:param>
<xsl:param name="selectedClass"></xsl:param>
<xsl:param name="branchClass"></xsl:param>
<xsl:variable name="class">
<xsl:if test="$isSelected = 1">
<xsl:value-of select="concat($selectedClass,' ')"/>
</xsl:if>
<xsl:if test="$isSelectedBranch = 1">
<xsl:value-of select="concat($branchClass,' ')"/>
</xsl:if>
<xsl:if test="$hasChildren = 1">
<xsl:value-of select="'hasChildren '"/>
</xsl:if>
</xsl:variable>
<xsl:if test="string-length($class) > 0">
<xsl:attribute name="class">
<xsl:value-of select="normalize-space($class)"/>
</xsl:attribute>
</xsl:if>
</xsl:template>
</xsl:stylesheet>​
The issue you describe above isn't replicating for me (I've checked your source code using Notepad++ and a diff viewer). I see from your code that you're using the CogWorks' Flexible Navigation package. Can you ensure you're using the latest code from here?
Many thanks,
Benjamin

Converting simple markdown(string) to html with xslt

I'm transforming my XSLT-stylesheets into documentation, and I want a rich experience within the comment nodes for each code-chunk, therefore I want to convert the following comment and output as xhtml:
String:
# This is a title with __bold__ text and *italic* #
This is just a normal line
- list point with some __bold__
- list point with a "link"[http://www.stackoverflow.com]
Wanted output:
<h1> This is a title with <strong>bold</strong> and <span>italic</span> </h1>
<p>This is a normal line</p>
<ul>
<li>list point with some <strong>bold</strong></li>
<li>list point with a link</li>
</ul>
I tried with a recursive function that uses xsl:analyze-string recursively from a ruleset, but can't find a solution that works really well.
Anyone have done this lately, or is there some frameworks out there that has functions to do this ?
thanx in advance! :)
Edit: Added one dirty example:
<!-- Output comments -->
<xsl:template match="comment()" mode="COMMENT">
<xsl:copy-of select="ips:groupReplace(normalize-space(.),
'
(.*)(\n|\r)(.*),
(.*)\*(.*)\*(.*),
(.*)\*\*(.*)\*\*(.*),
(.*)__(.*)__(.*),
(.*)#(.*)#(.*),
(.*)-(.*)
',
'
br,
span.italic,
span.bold,
strong,
h1,
li
')" />
</xsl:template>
<!-- Initializing the iterateRegex function -->
<xsl:function name="ips:groupReplace">
<xsl:param name="string" as="xs:string" />
<xsl:param name="search" />
<xsl:param name="replace" />
<xsl:variable name="regex" select="tokenize($search, ',')" />
<xsl:variable name="replacements" select="tokenize($replace, ',')" />
<xsl:copy-of select="ips:iterateRegex(count($replacements), $string, $regex, $replacements)" />
</xsl:function>
<!-- Iterate each regex -->
<xsl:function name="ips:iterateRegex">
<xsl:param name="counter" />
<xsl:param name="string" />
<xsl:param name="list_regex" />
<xsl:param name="list_replace" />
<xsl:variable name="newStr">
<xsl:analyze-string select="$string" regex="{normalize-space($list_regex[$counter])}" flags="xm">
<xsl:matching-substring>
<xsl:variable name="cc" select="contains($list_replace[$counter], '.')" />
<xsl:variable name="tag" select="normalize-space(if ($cc) then (substring-before($list_replace[$counter], '.')) else ($list_replace[$counter]))" />
<xsl:copy-of select="regex-group(1)" />
<xsl:choose>
<xsl:when test="normalize-space(regex-group(2)) = ''">
<xsl:element name="{$tag}" />
</xsl:when>
<xsl:otherwise>
<xsl:element name="{$tag}" >
<xsl:if test="$cc">
<xsl:attribute name="class" select="substring-after($list_replace[$counter],'.')" />
</xsl:if>
<xsl:copy-of select="regex-group(2)" />
</xsl:element>
</xsl:otherwise>
</xsl:choose>
<xsl:copy-of select="regex-group(3)" />
</xsl:matching-substring>
<xsl:non-matching-substring>
<xsl:copy-of select="." />
</xsl:non-matching-substring>
</xsl:analyze-string>
</xsl:variable>
<xsl:variable name="count" select="number($counter) - 1" />
<xsl:choose>
<xsl:when test="$count > 0">
<xsl:copy-of select="ips:iterateRegex($count, $newStr, $list_regex, $list_replace)" />
</xsl:when>
<xsl:otherwise>
<xsl:copy-of select="$newStr" />
</xsl:otherwise>
</xsl:choose>
</xsl:function>
I think you would need a parser. So this stylesheet implements a verbose one:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="text" name="block">
<xsl:param name="pString" select="."/>
<xsl:if test="$pString != ''">
<xsl:choose>
<xsl:when test="starts-with($pString,'#')">
<xsl:call-template name="header">
<xsl:with-param name="pString"
select="substring($pString,2)"/>
</xsl:call-template>
</xsl:when>
<xsl:when test="starts-with($pString,'
')">
<xsl:call-template name="list">
<xsl:with-param name="pString"
select="substring($pString,2)"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:call-template name="paragraph">
<xsl:with-param name="pString"
select="$pString"/>
</xsl:call-template>
</xsl:otherwise>
</xsl:choose>
</xsl:if>
</xsl:template>
<xsl:template name="header">
<xsl:param name="pString"/>
<xsl:variable name="vInside"
select="substring-before($pString,'#
')"/>
<xsl:choose>
<xsl:when test="$vInside != ''">
<h1>
<xsl:call-template name="inline">
<xsl:with-param name="pString" select="$vInside"/>
</xsl:call-template>
</h1>
<xsl:call-template name="block">
<xsl:with-param name="pString"
select="substring-after($pString,'#
')"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:call-template name="paragraph">
<xsl:with-param name="pString"
select="concat('#',$pString)"/>
</xsl:call-template>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template name="list">
<xsl:param name="pString"/>
<xsl:variable name="vCheckList" select="starts-with($pString,'- ')"/>
<xsl:choose>
<xsl:when test="$vCheckList">
<ul>
<xsl:call-template name="listItem">
<xsl:with-param name="pString" select="$pString"/>
</xsl:call-template>
</ul>
<xsl:call-template name="block">
<xsl:with-param name="pString">
<xsl:call-template name="afterlist">
<xsl:with-param name="pString" select="$pString"/>
</xsl:call-template>
</xsl:with-param>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:call-template name="block">
<xsl:with-param name="pString" select="$pString"/>
</xsl:call-template>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template name="paragraph">
<xsl:param name="pString"/>
<xsl:choose>
<xsl:when test="contains($pString,'
')">
<p>
<xsl:value-of select="substring-before($pString,'
')"/>
</p>
</xsl:when>
<xsl:otherwise>
<p>
<xsl:value-of select="$pString"/>
</p>
</xsl:otherwise>
</xsl:choose>
<xsl:call-template name="block">
<xsl:with-param name="pString"
select="substring-after($pString,'
')"/>
</xsl:call-template>
</xsl:template>
<xsl:template name="afterlist">
<xsl:param name="pString"/>
<xsl:choose>
<xsl:when test="starts-with($pString,'- ')">
<xsl:call-template name="afterlist">
<xsl:with-param name="pString"
select="substring-after($pString,'
')"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$pString"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template name="listItem">
<xsl:param name="pString"/>
<xsl:if test="starts-with($pString,'- ')">
<li>
<xsl:call-template name="inline">
<xsl:with-param name="pString"
select="substring-before(substring($pString,3),'
')"/>
</xsl:call-template>
</li>
<xsl:call-template name="listItem">
<xsl:with-param name="pString"
select="substring-after($pString,'
')"/>
</xsl:call-template>
</xsl:if>
</xsl:template>
<xsl:template name="inline">
<xsl:param name="pString" select="."/>
<xsl:if test="$pString != ''">
<xsl:choose>
<xsl:when test="starts-with($pString,'__')">
<xsl:call-template name="strong">
<xsl:with-param name="pString"
select="substring($pString,3)"/>
</xsl:call-template>
</xsl:when>
<xsl:when test="starts-with($pString,'*')">
<xsl:call-template name="span">
<xsl:with-param name="pString"
select="substring($pString,2)"/>
</xsl:call-template>
</xsl:when>
<xsl:when test="starts-with($pString,'"')">
<xsl:call-template name="link">
<xsl:with-param name="pString"
select="substring($pString,2)"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="substring($pString,1,1)"/>
<xsl:call-template name="inline">
<xsl:with-param name="pString"
select="substring($pString,2)"/>
</xsl:call-template>
</xsl:otherwise>
</xsl:choose>
</xsl:if>
</xsl:template>
<xsl:template name="strong">
<xsl:param name="pString"/>
<xsl:variable name="vInside" select="substring-before($pString,'__')"/>
<xsl:choose>
<xsl:when test="$vInside != ''">
<strong>
<xsl:value-of select="$vInside"/>
</strong>
<xsl:call-template name="inline">
<xsl:with-param name="pString"
select="substring-after($pString,'__')"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="'__'"/>
<xsl:call-template name="inline">
<xsl:with-param name="pString" select="$pString"/>
</xsl:call-template>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template name="span">
<xsl:param name="pString"/>
<xsl:variable name="vInside" select="substring-before($pString,'*')"/>
<xsl:choose>
<xsl:when test="$vInside != ''">
<span>
<xsl:value-of select="$vInside"/>
</span>
<xsl:call-template name="inline">
<xsl:with-param name="pString"
select="substring-after($pString,'*')"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="'*'"/>
<xsl:call-template name="inline">
<xsl:with-param name="pString" select="$pString"/>
</xsl:call-template>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template name="link">
<xsl:param name="pString"/>
<xsl:variable name="vInside"
select="substring-before($pString,'"')"/>
<xsl:choose>
<xsl:when test="$vInside != ''">
<xsl:call-template name="href">
<xsl:with-param name="pString"
select="substring-after($pString,'"')"/>
<xsl:with-param name="pInside" select="$vInside"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="'"'"/>
<xsl:call-template name="inline">
<xsl:with-param name="pString" select="$pString"/>
</xsl:call-template>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template name="href">
<xsl:param name="pString"/>
<xsl:param name="pInside"/>
<xsl:variable name="vHref"
select="substring-before(substring($pString,2),']')"/>
<xsl:choose>
<xsl:when test="starts-with($pString,'[') and $vHref != ''">
<a href="{$vHref}">
<xsl:value-of select="$pInside"/>
</a>
<xsl:call-template name="inline">
<xsl:with-param name="pString"
select="substring-after($pString,']')"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="concat('"',$pInside,'"')"/>
<xsl:call-template name="inline">
<xsl:with-param name="pString" select="$pString"/>
</xsl:call-template>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
With this input:
<text>
# This is a title with __bold__ text and *italic* #
This is just a normal line
- list point with some __bold__
- list point with a "link"[http://www.stackoverflow.com]
</text>
Output:
<h1> This is a title with
<strong>bold</strong> text and
<span>italic</span>
</h1>
<p>This is just a normal line</p>
<ul>
<li>list point with some
<strong>bold</strong>
</li>
<li>list point with a
link
</li>
</ul>
Note: Look how many templates are similar (they follow a pattern), so these could be parametrized. I didn't do that in this case because there seems to be more questions which need some sort of parser, so by the end of the week I will repost an answer implementing functional parser and parser combinators pattern that make very easy to write parsers (just writing its grammar rules).
Edit: XSLT 2.0 solution. This stylesheet:
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="text">
<xsl:param name="pString" select="."/>
<xsl:analyze-string select="$pString"
regex="(#(.*)#
)|((- (.*)
)+)">
<xsl:matching-substring>
<xsl:choose>
<xsl:when test="regex-group(1)">
<h1>
<xsl:call-template name="inline">
<xsl:with-param name="pString"
select="regex-group(2)"/>
</xsl:call-template>
</h1>
</xsl:when>
<xsl:when test="regex-group(3)">
<ul>
<xsl:call-template name="list">
<xsl:with-param name="pString"
select="regex-group(3)"/>
</xsl:call-template>
</ul>
</xsl:when>
</xsl:choose>
</xsl:matching-substring>
<xsl:non-matching-substring>
<xsl:if test=".!='
'">
<p>
<xsl:call-template name="inline">
<xsl:with-param name="pString"
select="normalize-space(.)"/>
</xsl:call-template>
</p>
</xsl:if>
</xsl:non-matching-substring>
</xsl:analyze-string>
</xsl:template>
<xsl:template name="list">
<xsl:param name="pString"/>
<xsl:analyze-string select="$pString" regex="- (.*)
">
<xsl:matching-substring>
<li>
<xsl:call-template name="inline">
<xsl:with-param name="pString"
select="regex-group(1)"/>
</xsl:call-template>
</li>
</xsl:matching-substring>
</xsl:analyze-string>
</xsl:template>
<xsl:template name="inline">
<xsl:param name="pString" select="."/>
<xsl:analyze-string select="$pString"
regex="(__(.*)__)|(\*(.*)\*)|("(.*)"\[(.*)\])">
<xsl:matching-substring>
<xsl:choose>
<xsl:when test="regex-group(1)">
<strong>
<xsl:value-of select="regex-group(2)"/>
</strong>
</xsl:when>
<xsl:when test="regex-group(3)">
<span>
<xsl:value-of select="regex-group(4)"/>
</span>
</xsl:when>
<xsl:when test="regex-group(5)">
<a href="{regex-group(7)}">
<xsl:value-of select="regex-group(6)"/>
</a>
</xsl:when>
</xsl:choose>
</xsl:matching-substring>
<xsl:non-matching-substring>
<xsl:value-of select="."/>
</xsl:non-matching-substring>
</xsl:analyze-string>
</xsl:template>
</xsl:stylesheet>
Output:
<h1> This is a title with
<strong>bold</strong> text and
<span>italic</span>
</h1>
<p>This is just a normal line</p>
<ul>
<li>list point with some
<strong>bold</strong>
</li>
<li>list point with a
link
</li>
</ul>
This transformation (111 lines):
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:my="my:my"
exclude-result-prefixes="xml xsl xs my">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:template match="/">
<xsl:variable name="vLines" select="tokenize(., '\n')"/>
<xsl:sequence select="my:parse-lines($vLines)"/>
</xsl:template>
<xsl:function name="my:parse-lines" as="element()*">
<xsl:param name="pLines" as="xs:string*"/>
<xsl:sequence select=
"my:parse-line($pLines, 1, count($pLines))"/>
</xsl:function>
<xsl:function name="my:parse-line" as="element()*">
<xsl:param name="pLines" as="xs:string*"/>
<xsl:param name="pLineNum" as="xs:integer"/>
<xsl:param name="pTotalLines" as="xs:integer"/>
<xsl:if test="not($pLineNum gt $pTotalLines)">
<xsl:variable name="vLine" select="$pLines[$pLineNum]"/>
<xsl:variable name="vLineLength"
select="string-length($vLine)"/>
<xsl:choose>
<xsl:when test=
"starts-with($vLine, '#')
and
ends-with($vLine, '#')
">
<xsl:variable name="vInnerString"
select="substring($vLine, 2, $vLineLength -2)"/>
<h1>
<xsl:sequence select="my:parse-string($vInnerString)"/>
</h1>
<xsl:sequence select=
"my:parse-line($pLines, $pLineNum+1, $pTotalLines)"/>
</xsl:when>
<xsl:when test=
"starts-with($vLine, '- ')
and
not(starts-with($pLines[$pLineNum -1], '- '))
">
<ul>
<li>
<xsl:sequence select="my:parse-string(substring($vLine, 2))"/>
</li>
<xsl:sequence select=
"my:parse-line($pLines, $pLineNum+1, $pTotalLines)"/>
</ul>
</xsl:when>
<xsl:when test="starts-with($vLine, '- ')">
<li>
<xsl:sequence select="my:parse-string(substring($vLine, 2))"/>
</li>
<xsl:sequence select=
"my:parse-line($pLines, $pLineNum+1, $pTotalLines)"/>
</xsl:when>
<xsl:otherwise>
<p>
<xsl:sequence select="my:parse-string($vLine)"/>
</p>
<xsl:sequence select=
"my:parse-line($pLines, $pLineNum+1, $pTotalLines)"/>
</xsl:otherwise>
</xsl:choose>
</xsl:if>
</xsl:function>
<xsl:function name="my:parse-string" as="node()*">
<xsl:param name="pS" as="xs:string"/>
<xsl:analyze-string select="$pS" flags="x" regex=
'(__(.*?)__)
|
(\*(.*?)\*)
|
("(.*?)"\[(.*?)\])
'>
<xsl:matching-substring>
<xsl:choose>
<xsl:when test="regex-group(1)">
<strong>
<xsl:sequence select="my:parse-string(regex-group(2))"/>
</strong>
</xsl:when>
<xsl:when test="regex-group(3)">
<span>
<xsl:sequence select="my:parse-string(regex-group(4))"/>
</span>
</xsl:when>
<xsl:when test="regex-group(5)">
<a href="{regex-group(7)}">
<xsl:sequence select="regex-group(6)"/>
</a>
</xsl:when>
</xsl:choose>
</xsl:matching-substring>
<xsl:non-matching-substring>
<xsl:value-of select="."/>
</xsl:non-matching-substring>
</xsl:analyze-string>
</xsl:function>
</xsl:stylesheet>
when applied on this XML document (the provided text complicated with nested constructs and wrapped in an element):
<t># This is a title with __bold__ text and *italic* #
This is just a normal line
- list point with some __bold__
- list point with a __*"link"[http://www.stackoverflow.com]*__</t>
produces the wanted, correct output:
<h1> This is a title with <strong>bold</strong> text and <span>italic</span>
</h1>
<p>This is just a normal line</p>
<p/>
<ul>
<li> list point with some <strong>bold</strong>
</li>
<li> list point with a <strong>
<span>
link
</span>
</strong>
</li>
</ul>
Do note: The RegEx mechanism of XPath 2.0 and XSLT 2.0 is adequate for solving this problem.

How to implement sorting using xslt on href

I am using xslt and I want to implement sorting on my href link.
below is the xslt part where I need to implement sorting.
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:tlink="urn:TridionLinking" xmlns:msxsl="urn:schemas-microsoft-com:xslt" xmlns:utils="urn:XSLTExtensions" exclude-result-prefixes="xsl xlink tlink msxsl utils">
<xsl:output method="xml" version="1.0" encoding="UTF-8" omit-xml-declaration="yes" />
<!--+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Title: View All the Destinations XHTML
Description: Render view all the destinations control
Author: Manoj Singh
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-->
<xsl:include href="dynamic_linking.xslt"/>
<!-- Translations are still loaded here because of XHTML content, research a way around that -->
<xsl:key name="kCityById" match="city" use="#id"/>
<xsl:variable name="vLocations" select="document(concat($publicationPath, /list/resources/#location))/list"/>
<xsl:variable name="destination" select="/list"/>
<xsl:param name="publicationPath"/>
<xsl:param name="pubURL"/>
<xsl:param name="pageURL"/>
<xsl:param name="region"/>
<xsl:param name="country"/>
<!-- offset controls the starting position of the results to show -->
<xsl:param name="offset">0</xsl:param>
<!-- blockSize controls how many results to show on a single page -->
<xsl:param name="blockSize" select="15" />
<!-- Amount of page links to show by default -->
<xsl:param name="pagesShown">20</xsl:param>
<xsl:variable name="totalHits" select="count(/list/destination[contains(concat($pubURL,#url),substring-before($pageURL,'/index.aspx'))])" />
<xsl:template name="calcStart">
<xsl:choose>
<xsl:when test="$offset = 0">1</xsl:when>
<xsl:otherwise>
<xsl:value-of select="($offset * $blockSize) + 1"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template name="calcEnd">
<xsl:choose>
<xsl:when test="(($offset + 1) * $blockSize) > $totalHits">
<xsl:value-of select="$totalHits"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="($offset + 1) * $blockSize"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template name="pageNavigation">
<xsl:param name="pageCount"/>
<xsl:param name="currPage"/>
<xsl:param name="showPages">
<xsl:choose>
<xsl:when test="$pagesShown > $pageCount">
<xsl:value-of select="$pageCount"/>
</xsl:when>
<xsl:when test="($pagesShown mod 2) = 0">
<xsl:value-of select="$pagesShown"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$pagesShown + 1"/>
</xsl:otherwise>
</xsl:choose>
</xsl:param>
<xsl:param name="currEntry" select="1"/>
<xsl:param name="offset">
<xsl:choose>
<xsl:when test="($currPage < $showPages) or ($pageCount = 1)">0</xsl:when>
<xsl:when test="$currPage > ($pageCount - $showPages + 1)">
<xsl:value-of select="$pageCount - $showPages"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$currPage - ($showPages div 2) - 1"/>
</xsl:otherwise>
</xsl:choose>
</xsl:param>
<!-- Header Processing -->
<xsl:if test="$currEntry = 1">
<xsl:if test="($pageCount > $showPages) and ($currPage >= $showPages)">
<li>... </li>
</xsl:if>
</xsl:if>
<xsl:if test="not ($currEntry > $showPages)">
<li>
<xsl:choose>
<xsl:when test="($currEntry + $offset) = $currPage">
<strong class="thisPage">
<xsl:value-of select="$currEntry + $offset"/>
</strong>
</xsl:when>
<xsl:otherwise>
<a href="{utils:GetHashedUrl(concat($pageURL,'?offset=',$currEntry + $offset - 1,'&blockSize=',$blockSize,'&pagesShown=',$pagesShown))}">
<xsl:value-of select="$currEntry + $offset"/>
</a>
</xsl:otherwise>
</xsl:choose>
</li>
<xsl:if test="not ($currEntry >= $showPages)">
<li class="separatorLine">|</li>
</xsl:if>
<xsl:call-template name="pageNavigation">
<xsl:with-param name="pageCount" select="$pageCount"/>
<xsl:with-param name="currPage" select="$currPage"/>
<xsl:with-param name="showPages" select="$showPages"/>
<xsl:with-param name="currEntry" select="$currEntry + 1"/>
<xsl:with-param name="offset" select="$offset"/>
</xsl:call-template>
</xsl:if>
<!-- Footer Processing -->
<xsl:if test="$currEntry = 1">
<xsl:if test="($pageCount > $showPages) and (($pageCount - $currPage + 1) >= $showPages)">
<li> ...</li>
</xsl:if>
</xsl:if>
</xsl:template>
<xsl:template name="displayPageNavigation">
<div class="continueBar">
<div class="continueBarLeft">
<xsl:variable name="displayStart">
<xsl:call-template name="calcStart"/>
</xsl:variable>
<xsl:variable name="displayEnd">
<xsl:call-template name="calcEnd"/>
</xsl:variable>
<strong>
<xsl:value-of select="utils:TextFormatted('DisplayingDestinations2Arg', concat($displayStart, '-', $displayEnd), $totalHits)"/>
</strong>
</div>
<div class="continueBarRight">
<ul class="paginationLinks">
<!-- Show a back button when available -->
<xsl:choose>
<xsl:when test="$offset > 0">
<li class="noBorder first">
<a class="iconButtonBackBar" href="{$pageURL}?offset={$offset - 1}&blockSize={$blockSize}&pagesShown={$pagesShown}">
<xsl:text disable-output-escaping="yes">&nbsp;</xsl:text>
</a>
</li>
</xsl:when>
<xsl:otherwise>
<li class="noBorder first">
<span class="iconButtonBackBarOff">
<xsl:text disable-output-escaping="yes">&nbsp;</xsl:text>
</span>
</li>
</xsl:otherwise>
</xsl:choose>
<!-- Output the page navigation links -->
<xsl:call-template name="pageNavigation">
<xsl:with-param name="pageCount">
<xsl:choose>
<xsl:when test="$blockSize >= $totalHits">1</xsl:when>
<xsl:when test="($totalHits mod $blockSize) != 0">
<xsl:value-of select="ceiling($totalHits div $blockSize)"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$totalHits div $blockSize"/>
</xsl:otherwise>
</xsl:choose>
</xsl:with-param>
<xsl:with-param name="currPage" select="$offset + 1"/>
</xsl:call-template>
<!-- Show a next button when available -->
<xsl:choose>
<xsl:when test="(($offset + 1) * $blockSize) > $totalHits">
<li class="last">
<span class="iconButtonForwardBarOff">
<xsl:text disable-output-escaping="yes">&nbsp;</xsl:text>
</span>
</li>
</xsl:when>
<xsl:otherwise>
<li class="last">
<a class="iconButtonForwardBar" href="{$pageURL}?offset={$offset + 1}&blockSize={$blockSize}&pagesShown={$pagesShown}">
<xsl:text disable-output-escaping="yes">&nbsp;</xsl:text>
</a>
</li>
</xsl:otherwise>
</xsl:choose>
</ul>
</div>
<div class="clearBoth">
<xsl:comment/>
</div>
</div>
</xsl:template>
<!-- root match -->
<xsl:template match="/list">
<!--<xsl:value-of select="$publicationPath"/>-->
<div class="brownBarContainer">
<div class="brownBar">
All Destinations
</div>
</div>
<table width="100%" cellspacing="0" cellpadding="0" border="0" class="displayTable">
<tbody>
<tr>
<th scope="col" class="first sortSelected">
<div class="thPadding">
<xsl:element name="a">
<xsl:attribute name="href"></xsl:attribute>
<xsl:attribute name="class">iconDownSortArrow</xsl:attribute>
</xsl:element>Destination
</div>
</th>
<th scope="col" class="sortHover">
<div class="thPadding">
<xsl:element name="a">
<xsl:attribute name="href"></xsl:attribute>
<xsl:attribute name="class">iconSortArrowOff</xsl:attribute>
</xsl:element>Country
</div>
</th>
<th scope="col" class="sortHover">
<div class="thPadding">
<xsl:element name="a">
<xsl:attribute name="href"></xsl:attribute>
<xsl:attribute name="class">iconSortArrowOff</xsl:attribute>
</xsl:element>Region
</div>
</th>
</tr>
<xsl:choose>
<xsl:when test="$region='' and $country=''">
<xsl:apply-templates select="destination">
<xsl:sort select="#title" order="ascending" />
</xsl:apply-templates>
</xsl:when>
<xsl:when test="$country!=''">
<xsl:apply-templates select="destination[city/#id=$vLocations/region/country[#id=$country]/city/#id]">
<xsl:sort select="#title" order="ascending" />
</xsl:apply-templates>
</xsl:when>
<xsl:when test="$region!=''">
<xsl:apply-templates select="destination[city/#id=$vLocations/region[#id=$region]/country/city/#id]">
<xsl:sort select="#title" order="ascending" />
</xsl:apply-templates>
</xsl:when>
</xsl:choose>
</tbody>
</table>
<div class="horRuleWhite">
<hr/>
</div>
<xsl:call-template name="displayPageNavigation" />
</xsl:template>
<xsl:template match="text()"/>
<xsl:template match="destination">
<xsl:variable name="vReverseURL">
<xsl:call-template name="reverse">
<xsl:with-param name="string" select="#url"/>
</xsl:call-template>
</xsl:variable>
<xsl:variable name="vCountryURL">
<xsl:call-template name="reverse">
<xsl:with-param name="string" select="substring-after($vReverseURL,'/')"/>
</xsl:call-template>
<xsl:text>/index.aspx</xsl:text>
</xsl:variable>
<xsl:variable name="vRegionURL">
<xsl:call-template name="reverse">
<xsl:with-param name="string" select="substring-after(substring-after($vReverseURL,'/'),'/')"/>
</xsl:call-template>
<xsl:text>/index.aspx</xsl:text>
</xsl:variable>
<xsl:variable name="current" select="."/>
<xsl:for-each select="$vLocations">
<tr>
<td class="detail first">
<a class="arrowSmallFront" href="{$current/#url}">
<xsl:value-of select="$current/#title"/>
</a>
</td>
<td class="detail noLeftBorder">
<a class="arrowSmallFront" href="{$vCountryURL}">
<xsl:value-of select="key('kCityById',$current/city/#id)/../#name"/>
</a>
</td>
<td class="detail noLeftBorder">
<a class="arrowSmallFront" href="{$vRegionURL}">
<xsl:value-of select="key('kCityById',$current/city/#id)/../../#name"/>
</a>
</td>
</tr>
</xsl:for-each>
</xsl:template>
<xsl:template name="reverse">
<xsl:param name="string" select="''"/>
<xsl:if test="$string != ''">
<xsl:call-template name="reverse">
<xsl:with-param name="string" select="substring($string,2)"/>
</xsl:call-template>
<xsl:value-of select="substring($string,1,1)"/>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
Now everything is working fine, however I want to implement sorting on the href link in above xslt.
<tr>
<th scope="col" class="first sortSelected">
<div class="thPadding">
<xsl:element name="a">
<xsl:attribute name="href"></xsl:attribute>
<xsl:attribute name="class">iconDownSortArrow</xsl:attribute>
</xsl:element>Destination
</div>
</th>
<th scope="col" class="sortHover">
<div class="thPadding">
<xsl:element name="a">
<xsl:attribute name="href"></xsl:attribute>
<xsl:attribute name="class">iconSortArrowOff</xsl:attribute>
</xsl:element>Country
</div>
</th>
<th scope="col" class="sortHover">
<div class="thPadding">
<xsl:element name="a">
<xsl:attribute name="href"></xsl:attribute>
<xsl:attribute name="class">iconSortArrowOff</xsl:attribute>
</xsl:element>Region
</div>
</th>
</tr>
1) Destinations.xml
<?xml version="1.0"?>
<list type="Destinations">
<resources location="include/xml/locations.xml">
<publication>481</publication>
</resources>
<destination id="594051" title="Sydney" url="/asiapacific/australia/sydney.aspx" >
<city id="192409" />
</destination>
<destination id="594088" title="Brisbane" url="/asiapacific/australia/brisbane.aspx" >
<city id="192397" />
</destination>
<destination id="594579" title="Dubai" url="/middleeast/uae/dubai.aspx" >
<city id="192855" />
</destination>
<destination id="594580" title="Abu Dhabi" url="/middleeast/uae/abudhabi.aspx" >
<city id="192851" />
</destination>
</list>
2) Locations.xml
<?xml version="1.0"?>
<list type="Locations">
<region id="192393" code="ASIA" name="Asia & the Pacific" shortname="Asia & the Pacific">
<country id="192395" code="AU" name="Australia" shortname="Australia">
<city id="192397" code="BNE" name="Brisbane" shortname="Brisbane">
<airport id="192399" code="BNE" name="Brisbane International Airport" shortname="Brisbane"></airport>
</city>
<city id="192409" code="SYD" name="Sydney" shortname="Sydney">
<airport id="192411" code="SYD" name="Kingsford Smith Airport" shortname="Sydney"></airport>
</city>
</country>
</region>
<region id="192847" code="MEAF" name="The Middle East & Africa" shortname="The Middle East & Africa">
<country id="192849" code="AE" name="United Arab Emirates" shortname="United Arab Emirates">
<city id="192851" code="AUH" name="Abu Dhabi" shortname="Abu Dhabi">
<airport id="192853" code="AUH" name="Abu Dhabi" shortname="Abu Dhabi"></airport>
</city>
<city id="192855" code="DXB" name="Dubai" shortname="Dubai">
<airport id="192857" code="DXB" name="Dubai International Airport" shortname="Dubai"></airport>
</city>
</country>
</region>
</list>
Please suggest!
Thanks.
Before each close tag </xsl:apply-templates> you need to include an <xsl:sort>, e.g.
<xsl:when test="$region='' and $country=''">
<xsl:apply-templates select="destination">
<xsl:sort select="#href" />
</xsl:apply-templates>
</xsl:when>
The above code assumes that #href is an attribute of the destination element, but that's just a guess, since you haven't told us where #href appears in the input. Unless I missed something.
If you need further help please post a sample of your input XML.
On second reading, I wonder if you mean that you want to sort based on which of the three a elements is clicked on? Please clarify, and edit your question to include a sample of your input XML.
OK, based on your comment it's clear that you want to sort based on whichever column the user clicked on last.
To expand on what Alejandro was saying, interactive behavior is outside the scope of XSLT. If you want XSLT to sort based on varying columns, you need to pass that column to the XSLT as an initial parameter. If the XSLT is running on the server, the whole page will reload when you change the sort order of the table. Do you want that?
For instant sorting you could call the XSLT within js on the client, but that's a bit hairy. Usually for interactive sorting, people just sort directly in javascript. Here are some tutorials and implementations:
http://www.javascripttoolbox.com/lib/table/
http://neil.fraser.name/software/tablesort/
http://dynamictable.com/