[英]How to call Java function from within a XSL file?
我正在使用APACHE FOP將我的xml文件轉換為PDF。 我有一個XSL文件,我需要在其中調用Java函數來轉換從XML讀取的一些值。
我已經在名稱空間中聲明了包含該函數的java類,並按如下方式調用靜態java函數:
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:fo="http://www.w3.org/1999/XSL/Format"
xmlns:java="http://xml.apache.org/xslt/java" exclude-result- prefixes="java"
xmlns:dateConversion = "urn:java:com.geonexus.worx.sync.reports.ReportMaker">
<fo:block background-color="yellow" padding="1pt">
<fo:inline padding-left="2pt" display-align="center">
<xsl:value-of select="dateConversion.convertDate(@dateDetected)"/>
</fo:inline>
</fo:block>
但是我不斷收到以下錯誤:
[ERROR] STDERR - ERROR: 'Could not compile stylesheet'
[ERROR] STDERR - FATAL ERROR: 'Could not compile stylesheet'
:Error checking type of the expression 'funcall(dateConversion.convertDate, [step("attribute", 51)])'.
[ERROR] STDERR - javax.xml.transform.TransformerConfigurationException: Could not compile stylesheet
[ERROR] STDERR - at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl.newTemplates(Unknown Source)
[ERROR] STDERR - at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl.newTransformer(Unknown Source)
如果您使用的是XALAN,請嘗試以下操作。
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:fo="http://www.w3.org/1999/XSL/Format"
xmlns:dateConversion="xalan://com.geonexus.worx.sync.reports.ReportMaker"
extension-element-prefixes="dateConversion"
exclude-result-prefixes="dateConversion">
<fo:block background-color="yellow" padding="1pt">
<fo:inline padding-left="2pt" display-align="center">
<xsl:value-of select="dateConversion:convertDate(@dateDetected)"/>
</fo:inline>
</fo:block>
使用簡單的XSL和Java類進行更新。
XSL :
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:dateConversion="xalan://com.geonexus.worx.sync.reports.ReportMaker"
extension-element-prefixes="dateConversion" exclude-result-prefixes="dateConversion">
<xsl:output method="xml" />
<xsl:template match="/">
<abcd>
<xsl:value-of select="dateConversion:convertDate('2005-03-02')" />
</abcd>
</xsl:template>
</xsl:stylesheet>
Java類:
package com.geonexus.worx.sync.reports;
public class ReportMaker {
public static String convertDate(String date) {
return date.replaceAll("-", ":");
}
}
輸出量
<?xml version="1.0" encoding="UTF-8"?><abcd>2005:03:02</abcd>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.