Example #1
0
      public static void BuildSchematronValidatorStylesheet(this IXsltProcessor processor, IXPathNavigable schemaDoc, XmlWriter output) {

         if (processor == null) throw new ArgumentNullException("processor");
         if (schemaDoc == null) throw new ArgumentNullException("schemaDoc");
         if (output == null) throw new ArgumentNullException("output");

         XPathNavigator nav = schemaDoc.CreateNavigator();
         nav.MoveToChild(XPathNodeType.Element);

         string queryBinding = nav.GetAttribute("queryBinding", "");

         string xsltVersion = String.IsNullOrEmpty(queryBinding)
            || queryBinding.Equals("xslt2", StringComparison.OrdinalIgnoreCase)
            || queryBinding.Equals("xpath2", StringComparison.OrdinalIgnoreCase) ? 
            "xslt2" : "xslt1";

         Assembly assembly = Assembly.GetExecutingAssembly();
         
         Uri baseUri = new UriBuilder {
            Scheme = XmlEmbeddedResourceResolver.UriSchemeClires,
            Host = null,
            Path = String.Concat(assembly.GetName().Name, "/schematron/", xsltVersion, "/")
         }.Uri;

         XsltCompileOptions compileOptions = new XsltCompileOptions();
         compileOptions.BaseUri = baseUri;

         string[] stages = { "iso_dsdl_include.xsl", "iso_abstract_expand.xsl", String.Concat("iso_svrl_for_", xsltVersion, ".xsl") };

         IXPathNavigable input = schemaDoc;

         for (int i = 0; i < stages.Length; i++) {

            Uri stageUri = new Uri(baseUri, stages[i]);

            using (Stream stageDoc = (Stream)compileOptions.XmlResolver.GetEntity(stageUri, null, typeof(Stream))) {

               XsltExecutable executable = processor.Compile(stageDoc, compileOptions);

               XsltRuntimeOptions runtimeOptions = new XsltRuntimeOptions {
                  InitialContextNode = input
               };

               if (i < stages.Length - 1) {
                  // output becomes the input for the next stage
                  input = executable.Run(runtimeOptions);
               } else {
                  // last stage is output to writer
                  executable.Run(output, runtimeOptions);
               }
            }
         }
      }
Example #2
0
      public static SchematronXsltValidator CreateSchematronValidator(this IXsltProcessor processor, IXPathNavigable schemaDoc) {

         if (processor == null) throw new ArgumentNullException("processor");
         if (schemaDoc == null) throw new ArgumentNullException("schemaDoc");

         IXPathNavigable stylesheetDoc = processor.ItemFactory.CreateNodeEditable();

         using (XmlWriter builder = stylesheetDoc.CreateNavigator().AppendChild()) 
            processor.BuildSchematronValidatorStylesheet(schemaDoc, builder);

         XsltCompileOptions compileOptions = new XsltCompileOptions();

         XPathNavigator schemaNav = schemaDoc.CreateNavigator();

         if (!String.IsNullOrEmpty(schemaNav.BaseURI))
            compileOptions.BaseUri = new Uri(schemaNav.BaseURI);

         return new SchematronXsltValidator(processor.Compile(stylesheetDoc, compileOptions));
      }