Permalink
Browse files

Compile Schematron stylesheets in schema-aware mode if the document t…

…o be processed is typed
  • Loading branch information...
ndw committed Dec 8, 2011
1 parent 4c8879e commit 4a3795af8ca3967801d7e0fa25ca0ed5dc34c47e
Showing with 11 additions and 3 deletions.
  1. +11 −3 src/com/xmlcalabash/library/ValidateWithSCH.java
@@ -1,5 +1,6 @@
package com.xmlcalabash.library;
import net.sf.saxon.om.StandardNames;
import net.sf.saxon.s9api.*;
import net.sf.saxon.Configuration;
import net.sf.saxon.sxpath.IndependentContext;
@@ -43,6 +44,7 @@
private WritablePipe resultPipe = null;
private WritablePipe reportPipe = null;
private Hashtable<QName,RuntimeValue> params = new Hashtable<QName,RuntimeValue> ();
private boolean schemaAware = false;
/** Creates a new instance of ValidateWithXSD */
@@ -80,6 +82,11 @@ public void reset() {
public void run() throws SaxonApiException {
super.run();
XdmNode sourceXML = source.read();
// If we're dealing with a typed document, we must compile the XSLT in schema-aware mode
schemaAware = (sourceXML.getUnderlyingNode().getTypeAnnotation() != StandardNames.XS_UNTYPED);
XsltCompiler compiler;
XsltExecutable exec;
XdmDestination result;
@@ -104,6 +111,7 @@ public void run() throws SaxonApiException {
}
compiler = runtime.getProcessor().newXsltCompiler();
compiler.setSchemaAware(schemaAware);
compiler.setURIResolver(new UResolver());
exec = compiler.compile(getSchematronXSLT("iso_svrl_for_xslt2.xsl"));
XsltTransformer schemaCompiler = exec.load();
@@ -130,9 +138,8 @@ public void run() throws SaxonApiException {
XsltTransformer transformer;
XdmNode sourceXML = source.read();
compiler = runtime.getProcessor().newXsltCompiler();
compiler.setSchemaAware(schemaAware);
exec = compiler.compile(new SAXSource(S9apiUtils.xdmToInputSource(runtime, compiledSchema)));
transformer = exec.load();
transformer.setInitialContextNode(sourceXML);
@@ -217,14 +224,15 @@ public Source resolve(String href, String base) throws TransformerException {
private SAXSource getSchematronXSLT(String xslt) {
InputStream instream = getClass().getResourceAsStream("/etc/schematron/" + xslt);
if (instream == null) {
throw new UnsupportedOperationException("Failed to load iso_svrl.xsl from JAR file.");
throw new UnsupportedOperationException("Failed to load " + xslt + " from JAR file.");
}
return new SAXSource(new InputSource(instream));
}
private XdmNode transform(XdmNode source, SAXSource stylesheet) throws SaxonApiException {
XsltCompiler compiler = runtime.getProcessor().newXsltCompiler();
compiler.setSchemaAware(schemaAware);
compiler.setURIResolver(new UResolver());
XsltExecutable exec = compiler.compile(stylesheet);
XsltTransformer schemaCompiler = exec.load();

0 comments on commit 4a3795a

Please sign in to comment.