多个输入文件的转换 [英] Transformation of multiple input files

查看:41
本文介绍了多个输入文件的转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在我正在使用这个 java(它接收一个 xml 文件参数)方法来执行 XSLT 转换:

Right now i am using this java (which receives one xml file parameter) method to perform XSLT transformation:

static public byte[] simpleTransform(byte[] sourcebytes, int ref_id) {   
    try {
        StreamSource xmlSource = new StreamSource(new ByteArrayInputStream(sourcebytes));
        StringWriter writer = new StringWriter();
        transformations_list.get(ref_id).transformer.transform(xmlSource, new StreamResult(writer));
        return writer.toString().getBytes("UTF-8");
    } catch (Exception e) {   
        e.printStackTrace();   
        return new byte[0];
    }   
} 

在我的 xslt 文件中,我使用 document('f2.xml') 来引用其他与转换相关的文件.

And in my xslt file i am using the document('f2.xml') to refer to other transform related files.

我想像这样使用我的 Java(获取多个 xml 文件):

I want to use my Java like this (get multiple xml files):

static public byte[] simpleTransform(byte[] f1, byte[] f2, byte[] f3, int ref_id) 

在我的 XSLT 中,我不想调用 document('f2.xml'),而是使用在我的 Java 方法中接收到的 f2 来引用对象.

An in my XSLT i don't want to call document('f2.xml') but refer to the object by using f2 received in my Java method.

有办法吗?我如何引用

f2.xml

在我的 XSLT 中使用这种方式?

in my XSLT using this way?

推荐答案

我不完全确定 f1、f2 等中的内容.它是文档的 URL 吗?还是 XML 文档内容本身?

I'm not entirely sure what is in f1, f2 etc. Is it the URL of a document? or the XML document content itself?

您可以考虑两种可能的方法.

There are two possible approaches you could consider.

一个是写一个URIResolver.当您调用 document('f2.xml') 时,Saxon 将调用您的 URIResolver 以获取相关文档作为 Source 对象.您的 URIResolver 可以返回一个 StreamSource,该 StreamSource 使用 ByteArrayInputStream 初始化,引用相关的 btye[] 值.

One is to write a URIResolver. When you call document('f2.xml') Saxon will call your URIResolver to get the relevant document as a Source object. Your URIResolver could return a StreamSource initialized with a ByteArrayInputStream referring to the relevant btye[] value.

第二种方法是将文档作为参数提供给样式表.您可以声明一个全局参数 <xsl:param name="f2" as="document-node()"/> 然后使用 Transfomer.setParameter()提供实际文件;在样式表中,将 document('f2.xml') 替换为 $f2.Saxon 将接受 Source 对象作为提供给 setParameter 的值,因此您可以再次创建一个 StreamSource,该 StreamSource 使用 ByteArrayInputStream 初始化,并引用相关的 btye[] 值;或者(也许更好)您可以通过调用 Saxon DocumentBuilder 来预先构建树.

A second approach is to supply the documents as parameters to the stylesheet. You could declare a global parameter <xsl:param name="f2" as="document-node()"/> and then use Transfomer.setParameter() to supply the actual document; within the stylesheet, replace document('f2.xml') by $f2. Saxon will accept a Source object as the value supplied to setParameter, so you could again create a StreamSource initialized with a ByteArrayInputStream referring to the relevant btye[] value; alternatively (and perhaps better) you could pre-build the tree by calling a Saxon DocumentBuilder.

这篇关于多个输入文件的转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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