1

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>

3 Answers 3

1

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 a xalan:script element if you put bsf.jar and js.jar on the classpath.

Sign up to request clarification or add additional context in comments.

3 Comments

+1 for the link. But answer is confuse. What do you mean by "extension functions are hardly portable"? As long as you follow the XSLT standard mechanism you can port an extension function for one XSLT processor to other, without the need to change the stylesheet. Extension mechanism as part of the standard specification is a feature of XSLT!
Alejandro, an extension function written in JScript for MSXML is not necessarily portable as an extension function written in JavaScript for Xalan or even portable as an extension function written in JScript.NET for Micorosft´s XslCompiledTransform. Use an extension function that takes a node-set from XSLT, manipulates it and returns a node-set to XSLT. You might be able to use J(ava)Script on the surface for all three XSLT processors but the data type and API exposed to script for an XSLT node-set is usually not the same.
I'm saying that your answer doesn't clarify about that this is not a problem with extension function but a problem with non standard extension elements used for embbeded extension function declarations wich are not the XSLT model.
1

You should mark the java script section as CDATA.

See below

<xalan:component prefix="ttVB" functions="FctDateDuration">
<xalan:script lang="javascript">

<![CDATA[
function FctDateDuration(p_startDate,p_endDate){
    //alert("inside");
.
.

}]]>
</xalan:script>

Comments

0

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">

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.