Domanda

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?

È stato utile?

Soluzione

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.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top