HI, I am using Xalan to parse my xsl file. The xsl is working properly in the vb.net parsers. But Xalan gives error for that xsl.
For extension function, could not find method java.lang.String.FctDateDuration([ExpressionContext,] STRING).
Here is how I have define my xsl.
xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt" xmlns:ttVB="ttVB" exclude-result
Here is the java script calling part in xsl : -
<xsl:variable name="start">
xsl:value-of select="substring(DepartureDateTime,1,10)" />
</xsl:variable>
xsl:variable name="end">
xsl:value-of select="substring(following-sibling::OriginDestinationInformation/DepartureDateTime,1,10)" />
</xsl:variable>
xsl:variable name="end1">
xsl:value-of select="substring(preceding-sibling::OriginDestinationInformation/DepartureDateTime,1,10)" />
</xsl:variable>
xsl:variable name="dd" select="ttVB:FctDateDuration(string('2011-02-20'),string('2011-02-25'))"/>
xsl:variable name="dd1" select="ttVB:FctDateDuration(string('2011-02-20'),string('2011-02-25'))"/>
<xsl:choose>
xsl:when test="$dd = 0 or $dd = 1">
<timeQualifier>TA</timeQualifier>
</xsl:when>
xsl:otherwise>
timeQualifier>TD</timeQualifier>
</xsl:otherwise>
</xsl:choose>
Here is my Javascript
<msxsl:script language="JavaScript" implements-prefix="ttVB">
<![CDATA[
function FctDateDuration(p_startDate,p_endDate){
if (IsDate(p_startDate) && IsDate(p_endDate)){
FctDateDuration = String(calcDays(p_startDate, p_endDate))
}else{
FctDateDuration = p_startDate + p_endDate
}
return FctDateDuration;
}
function IsDate(ddate){
//alert("Inside IsDate >> "+ddate);
var dteDate;
var year = ddate.substring(0, 4);
var month = ddate.substring(5, 7);
var day = ddate.substring(8,10);
month = month-1;
//alert(year);
//alert(month);
//alert(day);
dteDate=new Date(year,month,day);
return ((day==dteDate.getDate()) && (month==dteDate.getMonth()) && (year==dteDate.getFullYear()));
}
function calcDays(date1,date2){
date1 = date1.split("-");
date2 = date2.split("-");
var sDate = new Date(date1[0]+"/"+date1[1]+"/"+date1[2]);
var eDate = new Date(date2[0]+"/"+date2[1]+"/"+date2[2]);
var daysApart = Math.abs(Math.round((sDate-eDate)/86400000));
//document.getElementById('diffDays').lastChild.data = daysApart;
//alert(daysApart);
return daysApart;
}
]]>
</msxsl:script>
Well extension functions are hardly portable, not even the way they are defined is portable. With .NET you can use the
msxsl:script
element to define extension functions but don't expect Xalan to support that. According to http://xml.apache.org/xalan-j/extensions.html#ex-basic Xalan Java supports axalan:script
element if you put bsf.jar and js.jar on the classpath.I was able to parse the xsl properly. Thanks Martin for the help you gave. I would like to put here the changes I made. So It'll help to others.
I had to use bsf.jar and js.jar. Since bsf jar is not shipped with xalan. bsf-2.4.0 Also I would like to tell that I had to use the xalan jars separately. Java 1.5 inbuilt xalan gave me errors.
I changed the xsl decleration sl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xalan="http://xml.apache.org/xalan" xmlns:ttVB="ext1" extension-element-prefixes="ttVB" exclude-result-prefixes="ttVB" version="1.0"
And Javascript declaration according to the http://xml.apache.org/xalan-j/extensions.html#ex-basic
xalan:component prefix="ttVB" functions="FctDateDuration">
xalan:script lang="javascript">
You should mark the java script section as CDATA.
See below