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

查看:18
本文介绍了加载 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.

更新

我已经根据 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天全站免登陆