Re: [xsl] get Data BETWEEN FromDate and ToDate

Subject: Re: [xsl] get Data BETWEEN FromDate and ToDate
From: Rudolf P. Weinmann <rudolf.weinmann@xxxxxxxxxxxx>
Date: Sun, 22 May 2005 07:44:59 +0200
><xsl:if test="($doj &gt;= $date1) and ($doj &gt;= $date1)">
Should be amended to:
<xsl:if test="($doj &gt;= $date1) and ($doj &lt;= $date2)">

As Mukul said, his solution is XSLT 2.0.
Under XSLT 1.0 the result would be empty,
because neither $doj nor $date1 and $date2 can be converted to numbers for
the
comparison in the xsl:if statement.

XSLT 1.0 can do the job as well, see example below.

Using param as a toplevel element, you can pass fromDate and toDate from the
command line or over an API.
I prefere passing them as numbers (see Michael's reasoning) in YYYYMMDD
format.
When comparing the values with &gt;=, they get implicitly converted to
numbers.
I prefere the explicit convertion using the number() function.

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>

	<xsl:output method="xml" encoding="UTF-8"/>

	<xsl:param name="fromDate" select="'20050403'"/>
	<xsl:param name="toDate" select="'20050405'"/>

	<xsl:template match="Employee">
		<xsl:variable name="doj"
select="number(translate('efghcdab','ab/cd/efgh',@DOJ))"/>
		<xsl:if test="$doj &gt;= number($fromDate) and $doj &lt;= number($toDate)">
			<xsl:call-template name="copy"/>
		</xsl:if>
	</xsl:template>

	<xsl:template match="/|*" name="copy">
		<xsl:copy>
			<xsl:apply-templates select="@*"/>
			<xsl:apply-templates/>
		</xsl:copy>
	</xsl:template>

	<xsl:template match="@*">
		<xsl:copy/>
	</xsl:template>

</xsl:stylesheet>

The given input example isn't  wellformed XML.
Employee isn't closed and attribute Empname occurs twice on the first
element.
Corrected input XML:

<?xml version="1.0" encoding="UTF-8"?>
<Employees>
	<Employee Empname="Raj" DOJ="02/04/2005" SftTime="0030"/>
	<Employee Empname="Rajkumar" DOJ="02/04/2005" SftTime="0030"/>
	<Employee Empname="Raja" DOJ="03/04/2005" SftTime="0000"/>
	<Employee Empname="Ravi" DOJ="04/04/2005" SftTime="2330"/>
	<Employee Empname="john" DOJ="05/04/2005" SftTime="1600"/>
	<Employee Empname="gopi" DOJ="06/04/2005" SftTime="0100"/>
	<Employee Empname="ajith" DOJ="13/04/2005" SftTime="2200"/>
</Employees>

Rudolf P. Weinmann

Current Thread