加载XSLT文件时解析相对路径 [英] Resolving relative paths when loading XSLT files

查看:265
本文介绍了加载XSLT文件时解析相对路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用Apache FOP进行XSL转换,我的代码如下:

I need to do an XSL transformation using Apache FOP and I had code like this:

//Setup FOP
Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, out);
//Setup Transformer
Source xsltSrc = new StreamSource(new File(xslPath));
Transformer transformer = tFactory.newTransformer(xsltSrc);

//Make sure the XSL transformation's result is piped through to FOP
Result res = new SAXResult(fop.getDefaultHandler());
//Setup input
Source src = new StreamSource(new File(xmlPath));
//Start the transformation and rendering process
transformer.transform(src, res);

其中 xslPath 是我的XSLT的路径文件存储。

where xslPath is the path where my XSLT file is stored.

我已经确认它只有一个XSLT文件时可以工作,但是在我的项目中,我将事物分成了几个XSLT文件并加入了它们< xsl:import /> 代码。使用这种配置,我得到一个NullPointerException,因为它不能理解存储在XSLT中的所有信息,因为它分布在不同的文件中。

I have confirmed that it works when I have only one XSLT file, but in my project I have divided things into several XSLT files and joined them with the <xsl:import /> tag. With this configuration, I get a NullPointerException because it doesn't understand all the information stored in XSLT because it's distributed over different files.

我想知道是否有任何方法可以加载 Source xsltSrc 变量中的所有这些文件,以便所有XSL信息都可用。

I wonder if there's any way to load all these files in the Source xsltSrc variable so all the XSL information is available.

UPDATE

我根据Mads Hansen给出的答案更改了代码,但它仍然无效。我必须在类路径中包含XSLT slt文件,因此我使用ClassLoader加载XSLT文件。我在执行 url.toExternalForm()时检查了URL是否具有正确的路径。这是我的新代码:

I've changed the code based on the answer given by Mads Hansen, but it still doesn't work. I have to include the XSLT slt files in the classpath, so I load the XSLT file with ClassLoader. I've checked that the URL has the correct path when executing url.toExternalForm(). This is my new piece of code:

ClassLoader cl = this.getClass().getClassLoader();
String systemID = "resources/xslt/myfile.xslt";
InputStream in = cl.getResourceAsStream(systemID);
URL url = cl.getResource(systemID);
Source source = new StreamSource(in);
source.setSystemId(url.toExternalForm());
transformer = tFactory.newTransformer(source);

它找到并加载 myfile.xslt 但是它仍然无法解析其他XSLT文件的相对路径。

It finds and loads myfile.xslt but it still doesn't resolve the relative paths to the other XSLT files.

我做错了什么?

推荐答案

我刚刚得到它,一个迟到的答案(在FOP 1.0测试)------

I just got it, a late answer(tested on FOP 1.0) ------

你需要的只是为你的工厂设置一个uri解析器,因为以下对我有效:

All you need is to set an uri resolver for your factory, as following works for me:

TransformerFactory transFact = TransformerFactory.newInstance();
StreamSource xsltSource = new StreamSource(xsl);

// XXX for 'xsl:import' to load other xsls from class path
transFact.setURIResolver(new ClasspathResourceURIResolver());
Templates cachedXSLT = transFact.newTemplates(xsltSource);
Transformer transformer = cachedXSLT.newTransformer();


class ClasspathResourceURIResolver implements URIResolver {
  @Override
  public Source resolve(String href, String base) throws TransformerException {
    return new StreamSource(XXX.getClassLoader().getResourceAsStream(href));
  }
}

和我的导入xsl(所以'imported.xsl '应该在类路径中):

and my importing xsl(so the 'imported.xsl' should be in the classpath):

<xsl:import href="META-INF/companybusinesscredit/imported.xsl"/>

这篇关于加载XSLT文件时解析相对路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆