Transformation of multiple input files

怎甘沉沦 提交于 2019-12-11 19:45:00

问题


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];
    }   
} 

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

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) 

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.

Is there a way to do it? how do i refer to

f2.xml

in my XSLT using this way?


回答1:


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.

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.

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.



来源:https://stackoverflow.com/questions/21232128/transformation-of-multiple-input-files

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!