Project

Profile

Help

Processor Lifecycle, or Can I Cache a Processor?

Added by Chris Despopoulos over 8 years ago

I'm trying to use Saxon-CE in an AngularJS app. I want to create a service so that I make a singleton instance of the processor, and then I can change the XSLT and XML file (or any other settings) each time I need to use it. My hope is to reduce processing and to reduce calls to the server for XSLT files.

I thought that inside my service I could cache the processor in a variable -- if the var is undefined then the service has never been called, so I NEW a processor. After that I want to use the same processor. The code looks like this.

var _processor = undefined;

var transformToObj = function(content, transform, debug) { if(undefined === _processor) { _processor = Saxon.newXSLT20Processor(Saxon.requestXML(_baseUrl + transform)); } _processor.reset(); _processor.importStylesheet(Saxon.requestXML(_baseUrl + transform)); return _processor.proc.transformToDocument(Saxon.requestXML(_baseUrl + content)); };

The problem is, I never get a different XSLT stylesheet -- It always uses the initial stylesheet for my transforms. I know I'm passing different stylesheets, but the results always use the original one.

So the questio is, does Saxon-CE support caching the processor in this way?

(I notice that you can't cache the XSLT files, for example, because requestXML only gives a placeholder, not the actual file. So caching the request rsult doesn't change how or when Saxon-CE calls the server, right? So I'm wondering if something similar is afoot with the processor itself...)

Thanks for any help... cud


Replies (5)

Please register to reply

RE: Processor Lifecycle, or Can I Cache a Processor? - Added by Michael Kay over 8 years ago

Im afraid I can't see any obvious reason why the same stylesheet should be used when you try to load a new one.

The only thing I can suggest is checking that the URI is different - but even with the same URI, I think that Saxon document pooling only happens at the level of a Configuration object, and a Configuration is local to an XSLT20Processor.

RE: Processor Lifecycle, or Can I Cache a Processor? - Added by Chris Despopoulos over 8 years ago

So to recap, there's no reason that one CANNOT create a singleton processor and reuse it at arbitrary times in an app. Correct?

I need to make sure I have eliminated any scoping issues that AngularJS might cause. But I have verified:

  • I am reusing the same processor
  • I am passing in a unique URI to the XSLT

I don't see any obvious way to ask the processor what stylesheet is thinks it's using -- is there something I can look at in the processor?

Thanks cud

RE: Processor Lifecycle, or Can I Cache a Processor? - Added by Chris Despopoulos over 8 years ago

Sadly, I believe the processor does not update with a new stylesheet. I put a test together that minimizes the variables. I call two different stylesheets to run against the same XML. One is supposed to only deliver the short description, and the other delivers the whole topic. I prove that I'm getting a different stylesheet. Then I prove that the processor delivers the same result no matter which stylesheet I use. So the results I get indicate that I can't reset the stylesheet. Am I doing something wrong?

Here's the HTML with two links to transform the XML. Note that the XSLT files, the XML File, and the test.js file are all in the same directory, to keep things simple.

<script type="text/javascript" src="./Saxon-CE_1_1/SaxonceDebug/Saxonce.nocache.js"></script> <script type="text/javascript" src="./test.js"></script> Short Desc
Task

Now here's test.js which creates the processor to init, and then reuses it for further calls. :

var myTest = { _processor : undefined,

transformToHtmlString : function(content, transform) {

    if(this._processor === undefined) {
        alert("CREATING PROCESSOR: "+transform);
        this._processor = Saxon.newXSLT20Processor(Saxon.requestXML(transform));
    } else {
        alert("USING EXISTING PROCESSOR: "+transform);
    }

    this._processor.reset();
    this._processor.importStylesheet(Saxon.requestXML(transform));
    var result = this._processor.transformToDocument(Saxon.requestXML(content));
    alert(Saxon.serializeXML(result));
}

};

RE: Processor Lifecycle, or Can I Cache a Processor? - Added by Michael Kay over 8 years ago

I'm not sure what's going on here.

I'm also sorry to say that I'm not sure there is all that much point in investigating it. Even before the launch of Saxon-JS, it has become increasingly unlikely that we will ever ship a maintenance release of Saxon-CE, because it takes about a month just to test it, and with the passage of time and changes in the browser environment, and increasing number of the tests that we have are no longer valid. One of the aims of the Saxon-JS development has been to put this situation behind us and create a product that is much more readily testable. So even if we do isolate the bug, I don't think it is likely to be fixed. I'm afraid that's a price you pay with open source licenses.

RE: Processor Lifecycle, or Can I Cache a Processor? - Added by Chris Despopoulos over 8 years ago

Fair enough. By any chance, is there a way for me to get hold of the source to see if I can fix it locally? (Probably not, but I have to ask...)

I might want to use Saxon-JS, then. Will it be open source as well? And can I see a Beta?

This is why I asked about lifecycle... I believe you manage XML files as placeholders under the covers, and then get them synchronously or asynchronously depending on the need. So do you perform any sort of under-the-covers management of the processor as well? Like when you execute the NEW, do you see that one already exists and use it? Or does every NEW create a unique object, and it's up to garbage collection to get rid of them all?

    (1-5/5)

    Please register to reply