I'm try to write an ExtensionInstruction for Saxon 8.9. When the stylesheet encounters my custom element, I would like to call applyTemplates on a completely different document, where the output is inserted in the current result document. Everything is going fine, apart from actually calling apply-templates. The code i have is:
/**
   sax - the SAXSource for my other document
   path  - the XPath to call applyTemplates on in that document
   context - the context object passed into the 'process' method of my SimpleExpression
   styleElement  - my ExtensionInstruction
   compiledExpression - the SimpleExpression returned by 'compile' on the ExtensionInstruction
*/
public void applyTemplatesToSAXSource(SAXSource sax, 
                                                      String path, 
                                                      XPathContext context, 
                                                      StyleElement styleElement, 
                                                      Expression compiledExpression) throws TransformerException {
	DocumentInfo doc = createSAXSource(sax);
		
	XPathContextMajor ctx = new XPathContextMajor(doc, styleElement.getExecutable());
	ctx.setCaller(context);
		
	Expression selectExpression = styleElement.makeExpression(path);

	
	net.sf.saxon.instruct.ApplyTemplates a = new net.sf.saxon.instruct.ApplyTemplates(selectExpression, 
											  true, 
											  false, 
											  context.getCurrentMode(),
											  false,
											  true);
	a.process(ctx);	
    }

    /**
      Create a DocumentInfo from a SAXSource
    */  
    private DocumentInfo createSAXSource(Source s) throws TransformerException {

	net.sf.saxon.event.Builder sourceBuilder = this.controller.makeBuilder();
	Sender sender = new Sender(sourceBuilder.getPipelineConfiguration());
	Receiver r = sourceBuilder;

	sender.send(s, r);

	DocumentInfo doc = (DocumentInfo)sourceBuilder.getCurrentRoot();
	this.controller.registerDocument(doc, s.getSystemId());

	return doc;
    }
This doesn't work however, I get the following exception when I call 'process' on the ApplyTemplates:
java.lang.NullPointerException
	at net.sf.saxon.expr.ValueComparison.compare(ValueComparison.java:605)
	at net.sf.saxon.expr.GeneralComparison10.compare(GeneralComparison10.java:312)
	at net.sf.saxon.expr.GeneralComparison10.effectiveBooleanValue(GeneralComparison10.java:253)
	at net.sf.saxon.expr.FilterIterator$NonNumeric.matches(FilterIterator.java:178)
	at net.sf.saxon.expr.FilterIterator.getNextMatchingItem(FilterIterator.java:69)
	at net.sf.saxon.expr.FilterIterator.next(FilterIterator.java:49)
	at net.sf.saxon.expr.ContextMappingIterator.next(ContextMappingIterator.java:52)
	at net.sf.saxon.instruct.ApplyTemplates.applyTemplates(ApplyTemplates.java:298)
	at net.sf.saxon.instruct.ApplyTemplates.apply(ApplyTemplates.java:187)
	at net.sf.saxon.instruct.ApplyTemplates.process(ApplyTemplates.java:147)
	at nz.co.coretech.aviarc.screen.transform.AviarcTransformHelper.applyTemplatesToSAXSource(AviarcTransformHelper.java:160)
I used to do this with Saxon 6.5.5 but now the api has changed and I can't seem to figure out the right way to set up a new context against this other document. Any help I can get is greatly appreciated. By the way, I'm using Saxon 8.9 instead of 9 becuase with version 9 I was encountering errors like the ones in this thread.

Thanks,
Lindsay

View this message in context: Calling ApplyTemplates on a different document from an Extension Function
Sent from the saxon-help mailing list archive at Nabble.com.