java 是否支持在 xslt 中使用 EXSLT custom fuction

keshuichonglx 2015-07-31 09:25:52
在使用java做一个xslt的过程中遇到xsl文件中存在是使用custom fuction的情况,我仍然采用javax.xml.transform.Transformer和javax.xml.transform.TransformerFactory进行处理,但是报出了以下错误:

ERROR: '非 static Java 函数 'listFind' 的第一个参数不是有效的对象引用。'
FATAL ERROR: '无法将数据类型 'void' 转换为 'boolean'。'
:无法将数据类型 'void' 转换为 'boolean'。

望各位大神给予大力技术支持,急,谢谢!

下面是例子的相关数据:

xsl文件代码

<?xml version="1.0" encoding="ISO-8859-1"?>

<xsl:transform
version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ben="http://www.bennadel.com/xslt"
xmlns:func="http://exslt.org/functions"
xmlns:str="http://exslt.org/strings"
extension-element-prefixes="func str ben">

<func:function
name="ben:list-find"
ben:return-type="boolean"
ben:hint="I take a delimited list and a value and return true or false as to whether that value is a list item in the given list.">

<!-- Define arguments. -->
<xsl:param
name="list"
ben:type="string"
ben:required="true"
ben:hint="I am the delimited list of values that is being searched."
/>

<xsl:param
name="value"
ben:type="string"
ben:required="true"
ben:hint="I am the value for which we are searching in the above list."
/>

<xsl:param
name="delimiters"
select="','"
ben:type="string"
ben:required="false"
ben:hint="I am the collection of delimiters used to deliniate the list values. I can be one or more characters (each character is used as a valid delimiter). I am defaulted to the comma."
/>

<xsl:variable
name="tokens"
select="str:tokenize( $list, $delimiters )"
/>


</func:function>


<!-- Match the root girls node. -->
<xsl:template match="/girls">

<girls>

<xsl:copy-of
select="./girl[ ben:list-find( 'athletic,hot', @type ) ]"
/>

</girls>

</xsl:template>

</xsl:transform>


xml文件代码:

<?xml version="1.0"?>
<girls>
<girl type="cute">
<name>Sarah</name>
</girl>
<girl type="athletic">
<name>Tricia</name>
</girl>
<girl type="hot">
<name>Katie</name>
</girl>
<girl type="cute">
<name>Libby</name>
</girl>
</girls>


java代码如下(参数xml_text为xml文本数据,参数xsl_stylesheet_path为xsl文件路径):

static Transformer m_transformer ;
static String m_strLastXslPath = "" ;
public static String xsl_transform_with_stylesheet(String xml_text ,String xsl_stylesheet_path){
String strRet = null ;
try {
//
TransformerFactory ddd = TransformerFactory.newInstance() ;
if ( m_strLastXslPath != xsl_stylesheet_path ){ //xsl
StreamSource SourceXsl = new StreamSource(xsl_stylesheet_path) ;
m_transformer = ddd.newTransformer (SourceXsl);

m_strLastXslPath = xsl_stylesheet_path;
}

//xml
ByteArrayInputStream bisXml = new ByteArrayInputStream(xml_text.getBytes());
StreamSource SourceXml = new StreamSource(bisXml) ;

//html ouput
ByteArrayOutputStream bosHtml = new ByteArrayOutputStream();
Result ResultHtml = new StreamResult( bosHtml );

//do it
m_transformer.transform(SourceXml, ResultHtml);
strRet = bosHtml.toString();
}
catch (TransformerConfigurationException e) {
e.printStackTrace( );
}
catch (Exception e) {
e.printStackTrace( );
}
return strRet ;
}
...全文
55 点赞 收藏 回复
写回复
回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
相关推荐
发帖
Web 开发
创建于2007-09-28

8.0w+

社区成员

Java Web 开发
申请成为版主
帖子事件
创建了帖子
2015-07-31 09:25
社区公告
暂无公告