Java 脚本 XSLT 错误:对于扩展函数,找不到方法 java.lang.String。 ([ExpressionContext,] #STRING)
您好,我正在使用 Xalan 来解析我的 xsl 文件。 xsl 在 vb.net 解析器中正常工作。但 Xalan 给出了该 xsl 的错误。
对于扩展功能,找不到 方法 java.lang.String.FctDateDuration([ExpressionContext,] STRING)。
这是我定义 xsl 的方式。
xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt" xmlns:ttVB="ttVB" exclude-result
这是 xsl 中的 java 脚本调用部分:-
<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>
这是我的 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>
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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
扩展函数很难移植,甚至它们的定义方式也不是可移植的。在 .NET 中,您可以使用
msxsl:script
元素来定义扩展函数,但不要指望 Xalan 支持这一点。根据 http://xml.apache.org/xalan-j/extensions .html#ex-basic 如果您将 bsf.jar 和 js.jar 放在类路径中,Xalan Java 支持xalan: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.您应该将 java 脚本部分标记为 CDATA。
见下文
You should mark the java script section as CDATA.
See below
我能够正确解析 xsl。感谢马丁给予的帮助。
我想把我所做的更改放在这里。所以这对其他人会有帮助。
我必须使用 bsf.jar 和 js.jar。由于 bsf jar 未随 xalan 一起提供。 bsf-2.4.0
我还想告诉大家,我必须单独使用 xalan 罐子。 Java 1.5 内置的 xalan 给了我错误。
我更改了 xsl 声明
sl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xalan="http://xml.apache.org/xalan" xmlns:ttVB="ext1" 扩展名- element-prefixes="ttVB" except-result-prefixes="ttVB" version="1.0"
根据 http://xml.apache.org/xalan-j/extensions.html#ex-basic
xalan:component prefix="ttVB"函数=“FctDateDuration”>
xalan:script lang="javascript">
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">