1
votes

This code is in Java and uses Saxon

I am implementing a transform function to transform xml and several secondary xml sources All of the inputs are not files, so I cannot use document() or other methods that define files directly

String transform(String xml, List<String> secondaryXmls, String xslt);

It outputs the transformed xml result

I am successful in applying the transformation from xslt to the single xml file, but I have difficulties in applying transformation that also utilize the secondaryXmls. I have done my research and still could not find the right method to apply these

here is a snapshot of the code

    TransformerFactory tFactory = TransformerFactory.newInstance("net.sf.saxon.TransformerFactoryImpl",null);

    Document transformerDoc = loadXMLFromString(xslt);
    Source transformerSource = new DOMSource(transformerDoc);
    Transformer transformer =  tFactory.newTransformer(transformerSource); 

    Document sourceDoc = loadXMLFromString(xml);        
    Source source = new DOMSource(sourceDoc);

    DOMResult result = new DOMResult();
    transformer.transform(source, result);
    Document resultDoc = (Document) result.getNode();

    return getStringFrom(resultDoc);

Thanks!

EDIT:

Which is the better way:

  1. concatenating all the xmls, transform, return only the original part filtering the concatenated secondary xmls

  2. Write a code that adds

    <xsl:variable name="asd" select="document('asd')">
    

    on top of the xslt string

1

1 Answers

1
votes

First thing - get rid of all that DOM stuff! Using the DOM with Saxon slows it down by a factor of ten. Let Saxon build the trees in its own format, by using a StreamSource or SAXSource, and a StreamResult. Or you can build a tree in Saxon format yourself, if you want, using the s9api DocumentBuilder class.

Then as to the answer to your question: here are three possible solutions:

(a) supply the documents as a stylesheet parameter of type document-node()* (that is, a sequence of document nodes). In the Java, convert your list of XML strings to a list of document nodes by calling Configuration.buildDocument() on each one.

(b) write a URIResolver whose effect is to interpret the URI doc/3 as meaning the third document in the list; then use document('doc/3') to fetch that document.

(c) write a CollectionURIResolver which makes the whole collection of documents available using the collection() function.