I'm looking at converting this XSL template from sorting a-z to date ascending and descending. There are two tags that I could utilise to sort date by:
<delivery_start_date>05-Feb-2015</delivery_start_date>
and
<machine_date>20150205</machine_date>
I had tried applying sort like this:
<xsl:sort select="@machine_date" data-type="numeric" order="ascending"/>
However that resulted in no styling being applied to the XML. The template I have is:
<?xml version="1.0" encoding="windows-1251"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://ift.tt/tCZ8VR" exclude-result-prefixes="xhtml">
<xsl:output method="html"/>
<!-- Assigning first-letter key to the nodes -->
<xsl:key name="items-key" match="//*[local-name()='div' and @class='infoItem']/*[local-name()='p']/*[local-name()='a']" use="substring(., 1, 1)" />
<!-- <xsl:template match="*[local-name()='html']">-->
<xsl:template match="*[local-name()='html']">
<xsl:call-template name="make-indexes">
<xsl:with-param name="nodelist" select="//*[local-name()='div' and @class='infoItem']/*[local-name()='p']/*[local-name()='a']" />
<xsl:with-param name="key-name" select="'items-key'" />
</xsl:call-template>
</xsl:template>
<!--
See http://ift.tt/11erA1K
Main index template
Properties:
nodelist — set of nodes the index is to be based on, must equal the number of nodes in the items key of call-template
count-columns — number of columns
direction — way of filling columns with data (0 — vertical, 1 — horizontal)
-->
<xsl:template name="make-indexes">
<xsl:param name="nodelist"/>
<xsl:param name="key-name"/>
<!-- Grouping using Muenchian method -->
<xsl:variable name="blocks" select="$nodelist[generate-id(.) = generate-id(key($key-name, substring(., 1, 1)))]"/>
<!-- Alphabetical Links -->
<div class="alphalist">
<ul class="inline">
<xsl:apply-templates select="$blocks" mode="alpha-indexes">
<xsl:with-param name="key-name" select="$key-name"/>
<xsl:sort select="."/>
</xsl:apply-templates>
</ul>
</div>
<xsl:comment>Letter Blocks START</xsl:comment>
<div class="alphalinks" style="float:left;">
<xsl:for-each select="$blocks">
<xsl:sort select="."/>
<xsl:apply-templates select="." mode="block-indexes">
<xsl:with-param name="key-name" select="$key-name"/>
</xsl:apply-templates>
</xsl:for-each>
</div>
<xsl:comment>Letter Blocks END</xsl:comment>
</xsl:template>
<!-- Alphabetical Links Template -->
<xsl:template match="* | @*" mode="alpha-indexes">
<xsl:param name="key-name"/>
<xsl:variable name="letter">
<xsl:value-of select="substring(., 1, 1)"/>
</xsl:variable>
<li>
<a href="#{$letter}">
<xsl:value-of select="$letter"/>
</a>
</li>
</xsl:template>
<!-- Block template -->
<xsl:template match="* | @*" mode="block-indexes">
<xsl:param name="key-name"/>
<xsl:variable name="letter">
<xsl:value-of select="substring(., 1, 1)"/>
</xsl:variable>
<a name="{$letter}" />
<h2>
<xsl:value-of select="$letter"/> 
<span style="font-size:small">
<a href="#">top</a>
</span>
</h2>
<ul>
<xsl:comment>Links for letter <xsl:value-of select="$letter"/> START</xsl:comment>
<xsl:apply-templates select="key($key-name, substring(., 1, 1))" mode="item-indexes">
<xsl:sort select="."/>
</xsl:apply-templates>
<xsl:comment>Links for letter <xsl:value-of select="$letter"/> END</xsl:comment>
</ul>
</xsl:template>
<!-- Block contents template -->
<xsl:template match="* | @*" mode="item-indexes">
<li>
<!--<xsl:copy />-->
<xsl:element name="a">
<xsl:attribute name="href">
<xsl:value-of select="@href"/>
</xsl:attribute>
<xsl:value-of select="."/>
</xsl:element>
<!--<xsl:value-of select="."/>-->
</li>
</xsl:template>
</xsl:stylesheet>
What would be the solution for me to sort by date?
No comments:
Post a Comment