Friday, 4 June 2010

ISO Schematron loader for Saxon.Net and C#

I'm introducing an ISO Schematron 'compiler' into the CoherentWeb XSLT tool. This uses the open source XSLT implementation available from http://www.schematron.com/

To load a schema into CoherentWeb, first press: Schematron > [SVRL | MSG] (underneath the 'Recent XSLT' list) to generate the XSLT.

With this XSLT, you can now initiate a multi-threaded batch-validation of the set of files (zip-compressed or otherwise) by just pressing F5 .

After validating, you can review the SVRL output for any file by switching the XML viewer to 'Output' and browsing through the file list. Alternatively, with xsl:message output selected, you can see validation failures 'at a glance' from the file-list icons (see figure), and the adjacent message box.

CoherentWeb is a proprietary app written in C# and uses a bundled Saxon.NET 9.2 PE processor. The C# code used for compiling the schema into XSLT is shown below, error-checking and XSLT parameter handling code has been (mostly) removed.

          public static string ImportSchematron(string schemaPath, bool forMessage,
Processor sxnProc, List<Xslt2Parameter> paramsList)
{
string outPath = CreateDerivedXslPath(schemaPath, forMessage);

string baseXsltPath = Settings.ResourceFilePath + "\\xslt\\";

string[] schConverter = new string[] {
baseXsltPath + "iso_dsdl_include.xsl",
baseXsltPath + "iso_abstract_expand.xsl",
baseXsltPath + "iso_svrl_for_xslt2.xsl"
};

if (forMessage)
{
schConverter[2] = baseXsltPath + "iso_schematron_message_xslt2.xsl";
}

Uri schemaUri = new Uri(schemaPath);

XsltCompiler xslComp = sxnProc.NewXsltCompiler();
//////transform-1/////
Uri xslUri = new Uri(schConverter[0]);

XsltExecutable xslExec = xslComp.Compile(xslUri);


XsltTransformer xslTrans = xslExec.Load();
LoadAllXslt2Params(xslTrans, paramsList);
DomDestination domOut1 = new DomDestination(new XmlDocument());
using(FileStream fs = File.Open(schemaPath, FileMode.Open))
{
xslTrans.SetInputStream(fs, schemaUri); // set baseUri
xslTrans.Run(domOut1);
}
//////transform-2/////
xslUri = new Uri(schConverter[1]);
xslExec = xslComp.Compile(xslUri);

xslTrans = xslExec.Load();
LoadAllXslt2Params(xslTrans, paramsList);
DomDestination domOut2 = new DomDestination(new XmlDocument());
DocumentBuilder docBuilder = sxnProc.NewDocumentBuilder();
docBuilder.BaseUri = schemaUri;
XdmNode inputDoc2 = docBuilder.Wrap(domOut1.XmlDocument);

xslTrans.InitialContextNode = inputDoc2;
xslTrans.Run(domOut2);

//////transform-3/////
xslUri = new Uri(schConverter[2]);
xslExec = xslComp.Compile(xslUri);
xslTrans = xslExec.Load();
LoadAllXslt2Params(xslTrans, paramsList);
XdmNode inputDoc3 = docBuilder.Wrap(domOut2.XmlDocument);
xslTrans.InitialContextNode = inputDoc3;
Serializer serializer = new Serializer();
using (TextWriter tw = new StreamWriter(outPath, false))
{
serializer.SetOutputWriter(tw);
serializer.SetOutputProperty(Serializer.INDENT, "no");
xslTrans.Run(serializer);
}
return outPath;


}

0 comments:

Post a Comment

On Twitter...

    follow me on Twitter