无法使用 XSL -F(格式化)O(对象)输出值。您使用的示例实际上并没有进行 XSLT 转换,这是将值传递给样式表所必需的。
以下代码将执行您想要的操作,与您的代码的唯一区别是 XSLT 转换:
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.io.FileOutputStream;
import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.sax.SAXResult;
import javax.xml.transform.stream.StreamSource;
import org.apache.fop.apps.FOPException;
import org.apache.fop.apps.Fop;
import org.apache.fop.apps.FopFactory;
import org.apache.fop.apps.MimeConstants;
public class test {
public static void main(String[] args) throws FOPException, TransformerException, IOException
{
/*..*/
// Step 1: Construct a FopFactory
// (reuse if you plan to render multiple documents!)
FopFactory fopFactory = FopFactory.newInstance();
// Step 2: Set up output stream.
// Note: Using BufferedOutputStream for performance reasons (helpful with FileOutputStreams).
OutputStream out = new BufferedOutputStream(new FileOutputStream(new File("F:/myfile.pdf")));
try
{
// Step 3: Construct fop with desired output format
Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, out);
// Step 5: Setup input and output for XSLT transformation
// Setup input stream
Source xsltSource = new StreamSource(new File("F:/myfile.xsl"));
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer(xsltSource);
int[] array = {1,2,3};
transformer.setParameter("test", "1234");
transformer.setParameter("testRaw", array);
// Resulting SAX events (the generated FO) must be piped through to FOP
Result res = new SAXResult(fop.getDefaultHandler());
transformer.transform(xsltSource, res);
}
finally
{
out.close();
}
}
}
现在,要在您的 XSL 样式表(对我来说是 myfile.xsl)中使用该参数,它必须如下所示:
<xsl:stylesheet version="2.0" xmlns:fo="http://www.w3.org/1999/XSL/Format" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:param name="test"/>
<xsl:param name="testRaw"/>
<xsl:template match="/">
<fo:root>
<fo:layout-master-set>
<fo:simple-page-master master-name="my-page">
<fo:region-body margin="0.5in"/>
</fo:simple-page-master>
</fo:layout-master-set>
<fo:page-sequence master-reference="my-page">
<fo:flow flow-name="xsl-region-body" font="7pt Times">
<fo:block border="thin solid black" text-align="center">
<xsl:value-of select="$test"/>
</fo:block>
<fo:block break-before="page" border="thin solid black" text-align="center">
<xsl:value-of select="$testRaw"/>
</fo:block>
</fo:flow>
</fo:page-sequence>
</fo:root>
</xsl:template>
</xsl:stylesheet>
此样式表定义了两个参数:“test”和“testRaw”,它们都输出到两个单独的页面。为此,您使用<xsl:value-of select="$paramName"/>
.
在我发布的代码中,这两个值是一个字符串和一个 int 数组。请注意:您不能只将任何类传递给样式表并期望 XSL 转换器知道它。这行不通。通常,传递给样式表的参数被认为是字符串值。如果你传递了其他任何东西,你需要确保你的样式表和转换器能够真正处理它。
在你的机器上运行代码,看看第二页写了什么:这只是胡言乱语,因为样式表和转换器无法处理 int 数组。如果您确实需要处理来自自定义类的数据,我建议将您真正需要的值作为(连接)字符串传递给样式表。