Project

Profile

Help

Adding auxiliar xml to global document pool cache

Added by Juan RF about 11 years ago

Hi all,

I´m trying to pre-load a xml doc in Saxon´s cache in order to use it inside my xslt with a document() function.

This is my Java code:


Source xsltTarget = new StreamSource(xsltInput);
String path = "C:/xml";
try {
	TransformerFactory tFactory = TransformerFactory.newInstance();
	tFactory.setURIResolver(new MyResolver(path));
	Transformer transformer = tFactory.newTransformer(xsltTarget);
	Controller controller = (net.sf.saxon.Controller) transformer;
	Configuration configuration = controller.getConfiguration();
        configuration.setConfigurationProperty(FeatureKeys.PRE_EVALUATE_DOC_FUNCTION, Boolean.TRUE);

        Source source = transformer.getURIResolver().resolve("document.xml", "");
        DocumentInfo newdoc = configuration.buildDocument(source);
	configuration.getGlobalDocumentPool().add(newdoc, "document.xml");

        Source streamSource = null;
        StreamResult resultTarget = null;

	streamSource = new StreamSource(new StringReader(xml));
	resultTarget = new StreamResult(new StringWriter());
	transformer.transform(streamSource, resultTarget);

....

and my implemented URIResolver is:



    public static class MyResolver implements URIResolver {
	String base_path;

	public MyResolver(String base) {
	    this.base_path = base;
	}

	@Override
	public Source resolve(String href, String base) {
	    StringBuffer path = new StringBuffer(this.base_path);
	    if (!base_path.endsWith("/") && !base_path.endsWith("\\")) {
		path.append(File.separator);
	    }

	    path.append(href);
	    File file = new File(path.toString());
	    if (file.exists()) {
		System.out.println("Resolved!!");
		return new StreamSource(file);
	    }

	    return null;
	}

    }

Then, in my xslt, I want to define my doc variable to be accesed by xpath expressions:




...


I wanted to know if this is a correct mode to pre-cache a xml file. With that code I don´t see any performance gain between pre-caching and without pre-caching.

Am I doing right? Is there any useless code? Do I have to add anything?

Thank you in advance.

Regards.


Replies (4)

Please register to reply

RE: Adding auxiliar xml to global document pool cache - Added by Michael Kay about 11 years ago

The second argument to documentPool.add() must be an absolute URI. You have supplied a relative URI, so when the system tries to find your document in the pool, it won't find it.

I would advise against trying to preLoad the global document cache in this way. If the property FeatureKeys.PRE_EVALUATE_DOC_FUNCTION is set, then when Saxon sees the function call

<xsl:variable name="doc" select="document('document.xml')"/>

while compiling the stylesheet, it will automatically build the document and put it in the global cache (with the correct absolute URI) without any further action on your part.

RE: Adding auxiliar xml to global document pool cache - Added by Juan RF about 11 years ago

Thank you Michael,

So, if I remove the sentence:


configuration.setConfigurationProperty(FeatureKeys.PRE_EVALUATE_DOC_FUNCTION, Boolean.TRUE);

and I change the other to:


configuration.getGlobalDocumentPool().add(newdoc, "C:/xml/document.xml");

It should work, right?

Please confirm me whether this is true or not, and also please excuse me if I didn´t understand you well.

Regards

RE: Adding auxiliar xml to global document pool cache - Added by Michael Kay about 11 years ago

No, C:/xml/document.xml is not an absolute URI. The URI will typically have file:/ or file:/// at the start. Saxon does make some effort to normalize the URI, but it does have to be a URI for this to work. As I said before, I would advise you NOT to do it this way.

RE: Adding auxiliar xml to global document pool cache - Added by Juan RF about 11 years ago

Thank you again.

Please no offence: I just wanted to know how to program these two options separately to pre-cache files with saxon.

So, first alternative uses URIs explicitly and needs an URIresolver to be defined; and the other one (and more recommended) is just putting configuration.setConfigurationProperty(FeatureKeys.PRE_EVALUATE_DOC_FUNCTION, Boolean.TRUE); before transformation is called, is that correct?

I forgot to mention that I´m using Saxon 9.1.0.8

Thanks and excuse me, Michael

    (1-4/4)

    Please register to reply