Example #1
0
 public XmlNode RunQuery(XmlNode xmlNode)
 {
     using (var xmlNodeReader = new XmlNodeReader(xmlNode))
     {
         XdmNode indoc = _processor.NewDocumentBuilder().Build(xmlNodeReader);
         DomDestination dest = new DomDestination();
         _xqueryEvaluator.ContextItem = indoc;
         _xqueryEvaluator.Run(dest);
         return dest.XmlDocument;
     }
 }
    private XmlDocument Process (Context context) {

      using (this._SourceXml) {

        using (this._TemplateStream) {

          XmlDocument doc = new XmlDocument();
          doc.Load(this._SourceXml);
          XdmNode input = this._Processor.NewDocumentBuilder().Wrap(doc);

          XsltTransformer transformer = this._Template.Load();
          transformer.InitialContextNode = input;

          DomDestination result = new DomDestination();
          transformer.Run(result);
          return result.XmlDocument;

        }
      }
    }
Example #3
0
        internal XmlDocument DoTransform()
        {
            Processor processor = new Processor();

            XdmNode input = processor.NewDocumentBuilder().Wrap(_docToTransform);

            // Create a compiler
            XsltCompiler compiler = processor.NewXsltCompiler();

            DocumentBuilder builder = processor.NewDocumentBuilder();
            XdmNode xsltSheetNode = builder.Build(_xsltDocReader);

            // Compile the stylesheet
            XsltTransformer transformer = compiler.Compile(xsltSheetNode).Load();

            // Run the transformation
            transformer.InitialContextNode = input;
            DomDestination result = new DomDestination();
            transformer.Run(result);

            return result.XmlDocument;
        }
 public XmlDestination HandleResultDocument(string href, Uri baseUri) {
     DomDestination destination = new DomDestination();
     results[href] = destination;
     return destination;
 }
Example #5
0
        /// <summary>
        /// Show a query producing a DOM as its input and producing a DOM as its output
        /// </summary>
        public override void run(Uri samplesDir)
        {
            Processor processor = new Processor();

            XmlDocument input = new XmlDocument();
            input.Load(new Uri(samplesDir, "data/books.xml").ToString());
            XdmNode indoc = processor.NewDocumentBuilder().Build(new XmlNodeReader(input));

            XQueryCompiler compiler = processor.NewXQueryCompiler();
            XQueryExecutable exp = compiler.Compile("<doc>{reverse(/*/*)}</doc>");
            XQueryEvaluator eval = exp.Load();
            eval.ContextItem = indoc;
            DomDestination qout = new DomDestination();
            eval.Run(qout);
            XmlDocument outdoc = qout.XmlDocument;
            Console.WriteLine(outdoc.OuterXml);
        }
Example #6
0
        /// <summary>
        /// Run a transformation from a DOM (System.Xml.Document) input to a DOM output
        /// </summary>
        public override void run(Uri samplesDir)
        {
            // Create a Processor instance.
            Processor processor = new Processor();

            // Load the source document (in practice, it would already exist as a DOM)
            XmlDocument doc = new XmlDocument();
            doc.Load(new XmlTextReader(samplesDir.AbsolutePath + "data/othello.xml"));
            XdmNode input = processor.NewDocumentBuilder().Wrap(doc);

            // Create a compiler
            XsltCompiler compiler = processor.NewXsltCompiler();

            // Compile the stylesheet
            XsltTransformer transformer = compiler.Compile(new Uri(samplesDir, "styles/summarize.xsl")).Load();

            // Run the transformation
            transformer.InitialContextNode = input;
            DomDestination result = new DomDestination();
            transformer.Run(result);

            // Serialize the result so we can see that it worked
            Console.WriteLine(result.XmlDocument.OuterXml);
        }
        /// <summary> Use the Saxon-HE library to transform a source XML file with the XSLT file and return the string </summary>
        /// <param name="SourceFile"> Source XML file, to be transformed </param>
        /// <param name="XSLT_File"> XSLT file to perform the transform </param>
        /// <returns> XSLT return arguments including a flag for success, and possibly an exception </returns>
        public static XSLT_Transformer_ReturnArgs Saxon_Transform(string SourceFile, string XSLT_File)
        {
            // Keep track of the start time
            DateTime starTime = DateTime.Now;

            // Create the return object
            XSLT_Transformer_ReturnArgs returnArgs = new XSLT_Transformer_ReturnArgs();
            returnArgs.Engine = XSLT_Transformer_Engine_Enum.Saxon;

            // Ensure the XSLT file exists
            if (!File.Exists(XSLT_File))
            {
                returnArgs.Successful = false;
                returnArgs.ErrorMessage = "Indicated XSLT file ( " + XSLT_File + " ) does not exist.";
                return returnArgs;
            }

            // Ensure the source file exists
            if (!File.Exists(SourceFile))
            {
                returnArgs.Successful = false;
                returnArgs.ErrorMessage = "Indicated source file ( " + SourceFile + " ) does not exist.";
                return returnArgs;
            }

            FileInfo input = new FileInfo(SourceFile);

            try
            {

                // Compile stylesheet
                var processor = new Processor();
                var compiler = processor.NewXsltCompiler();
                var executable = compiler.Compile(new Uri(XSLT_File));

                // Do transformation to a destination
                var destination = new DomDestination();
                using (var inputStream = input.OpenRead())
                {
                    var transformer = executable.Load();
                    transformer.SetInputStream(inputStream, new Uri(input.DirectoryName));
                    transformer.Run(destination);
                }

                // Save result to a file (or whatever else you wanna do)
                using (var stringWriter = new StringWriter())
                using (var xmlTextWriter = XmlWriter.Create(stringWriter))
                {
                    destination.XmlDocument.WriteTo(xmlTextWriter);
                    xmlTextWriter.Flush();
                    returnArgs.TransformedString = stringWriter.GetStringBuilder().ToString();
                }

                returnArgs.Successful = true;
            }
            catch (Exception ee)
            {
                returnArgs.Successful = false;
                returnArgs.ErrorMessage = ee.Message;
                returnArgs.InnerException = ee;
            }

            // Determine the elapsed time, in milliseconds
            returnArgs.Milliseconds = DateTime.Now.Subtract(starTime).TotalMilliseconds;

            return returnArgs;
        }
        public static string ImportSchematron(string baseDir, string schemaPath, string xsltPath)
        {
            // Builds a new XSLT file from a schematron file.
            // This only needs to be done when the schematron file changes.
            var sxnProc = new Processor();

            var outPath = xsltPath;
            var baseXsltPath = Path.Combine(baseDir, @"Content\Saxon\");

            var schConverter = new string[]
                                   {
                                       baseXsltPath + "iso_dsdl_include.xsl",
                                       baseXsltPath + "iso_abstract_expand.xsl",
                                       baseXsltPath + "iso_svrl_for_xslt2.xsl"
                                   };
            var schemaUri = new Uri(schemaPath);
            var xslComp = sxnProc.NewXsltCompiler();

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

            //////transform-2/////
            xslUri = new Uri(schConverter[1]);
            xslExec = xslComp.Compile(xslUri);
            xslTrans = xslExec.Load();
            var domOut2 = new DomDestination(new XmlDocument());
            var docBuilder = sxnProc.NewDocumentBuilder();
            docBuilder.BaseUri = schemaUri;
            var 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();
            var inputDoc3 = docBuilder.Wrap(domOut2.XmlDocument);
            xslTrans.InitialContextNode = inputDoc3;
            var serializer = new Serializer();
            using (TextWriter tw = new StreamWriter(outPath, false))
            {
                serializer.SetOutputWriter(tw);
                serializer.SetOutputProperty(Serializer.INDENT, "no");
                xslTrans.Run(serializer);
            }

            return outPath;
        }
Example #9
-22
 private static XmlDocument Process (XmlReader funcSeq, XmlReader extTransform, Uri baseUri) {
   Processor processor = new Processor();
   XdmNode input = processor.NewDocumentBuilder().Build(funcSeq);
   XsltCompiler compiler = processor.NewXsltCompiler();
   compiler.BaseUri = baseUri;
   XsltTransformer transformer = compiler.Compile(extTransform).Load();
   transformer.InitialContextNode = input;
   DomDestination result = new DomDestination();
   transformer.Run(result);
   return result.XmlDocument;
 }