public void GenXML() { String sourceUri = Server.MapPath("5648.xml"); String xqUri = Server.MapPath("graph.xq"); using (FileStream sXml = File.OpenRead(sourceUri)) { using (FileStream sXq = File.OpenRead(xqUri)) { Processor processor = new Processor(); XQueryCompiler compiler = processor.NewXQueryCompiler(); compiler.BaseUri = sourceUri; XQueryExecutable exp = compiler.Compile(sXq); XQueryEvaluator eval = exp.Load(); DocumentBuilder loader = processor.NewDocumentBuilder(); loader.BaseUri = new Uri(sourceUri); XdmNode indoc = loader.Build(new FileStream(sourceUri, FileMode.Open, FileAccess.Read)); eval.ContextItem = indoc; Serializer qout = new Serializer(); qout.SetOutputProperty(Serializer.METHOD, "xml"); qout.SetOutputProperty(Serializer.INDENT, "yes"); qout.SetOutputProperty(Serializer.SAXON_INDENT_SPACES, "1"); qout.SetOutputWriter(Response.Output); eval.Run(qout); } } }
/// <summary> /// Show a query producing a document as its result and serializing this to a FileStream /// </summary> public override void run(Uri samplesDir) { Processor processor = new Processor(); XQueryCompiler compiler = processor.NewXQueryCompiler(); compiler.BaseUri = samplesDir.ToString(); compiler.DeclareNamespace("saxon", "http://saxon.sf.net/"); XQueryExecutable exp = compiler.Compile("<saxon:example>{static-base-uri()}</saxon:example>"); XQueryEvaluator eval = exp.Load(); Serializer qout = new Serializer(); qout.SetOutputProperty(Serializer.METHOD, "xml"); qout.SetOutputProperty(Serializer.INDENT, "yes"); qout.SetOutputProperty(Serializer.SAXON_INDENT_SPACES, "1"); qout.SetOutputStream(new FileStream("testoutput.xml", FileMode.Create, FileAccess.Write)); Console.WriteLine("Output written to testoutput.xml"); eval.Run(qout); }
/// <summary> /// Show a query producing a sequence as its result and returning the sequence /// to the C# application in the form of an iterator. The sequence is then /// output by serializing each item individually, with each item on a new line. /// </summary> public override void run(Uri samplesDir) { Processor processor = new Processor(); String inputFileName = new Uri(samplesDir, "data/books.xml").ToString(); //XmlTextReader reader = new XmlTextReader(inputFileName, // new FileStream(inputFileName, FileMode.Open, FileAccess.Read)); XmlTextReader reader = new XmlTextReader(inputFileName, UriConnection.getReadableUriStream(new Uri(samplesDir, "data/books.xml"))); reader.Normalization = true; // add a validating reader - not to perform validation, but to expand entity references XmlValidatingReader validator = new XmlValidatingReader(reader); validator.ValidationType = ValidationType.None; XdmNode doc = processor.NewDocumentBuilder().Build(reader); XQueryCompiler compiler = processor.NewXQueryCompiler(); XQueryExecutable exp = compiler.Compile("//ISBN"); XQueryEvaluator eval = exp.Load(); eval.ContextItem = doc; foreach (XdmNode node in eval) { Console.WriteLine(node.OuterXml); } }
/// <summary> /// Show a query producing a sequence as its result and returning the sequence /// to the C# application in the form of an iterator. For each item in the /// result, its string value is output. /// </summary> public override void run(Uri samplesDir) { Processor processor = new Processor(); XQueryCompiler compiler = processor.NewXQueryCompiler(); XQueryExecutable exp = compiler.Compile("for $i in 1 to 10 return $i * $i"); XQueryEvaluator eval = exp.Load(); XdmValue value = eval.Evaluate(); IEnumerator e = value.GetEnumerator(); while (e.MoveNext()) { XdmItem item = (XdmItem)e.Current; Console.WriteLine(item.ToString()); } }
/// <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); }
/// <summary> /// Show a query producing a single atomic value as its result and returning the value /// to the C# application /// </summary> public override void run(Uri samplesDir) { Processor processor = new Processor(); XQueryCompiler compiler = processor.NewXQueryCompiler(); XQueryExecutable exp = compiler.Compile("avg(for $i in 1 to 10 return $i * $i)"); XQueryEvaluator eval = exp.Load(); XdmAtomicValue result = (XdmAtomicValue)eval.EvaluateSingle(); Console.WriteLine("Result type: " + result.Value.GetType()); Console.WriteLine("Result value: " + (decimal)result.Value); }
/// <summary> /// Show a query consisting of two modules, using a QueryResolver to resolve /// the "import module" declaration /// </summary> public override void run(Uri samplesDir) { String mod1 = "import module namespace m2 = 'http://www.example.com/module2';" + "m2:square(3)"; String mod2 = "module namespace m2 = 'http://www.example.com/module2';" + "declare function m2:square($p) { $p * $p };"; Processor processor = new Processor(); XQueryCompiler compiler = processor.NewXQueryCompiler(); InlineModuleResolver resolver = new InlineModuleResolver(); resolver.AddModule(new Uri("http://www.example.com/module2"), mod2); compiler.QueryResolver = resolver; XQueryExecutable exp = compiler.Compile(mod1); XQueryEvaluator eval = exp.Load(); XdmAtomicValue result = (XdmAtomicValue)eval.EvaluateSingle(); Console.WriteLine("Result type: " + result.Value.GetType()); Console.WriteLine("Result value: " + (long)result.Value); }
public override void run(Uri samplesDir) { String query = "import schema default element namespace \"\" at \"" + samplesDir + "\\data\\books.xsd\";\n" + "for $integer in (validate { doc(\"" + samplesDir + "\\data\\books.xml\") })//schema-element(ITEM)\n" + "return <OUTPUT>{$integer}</OUTPUT>"; Processor processor = new Processor(); XQueryCompiler compiler = processor.NewXQueryCompiler(); compiler.XQueryLanguageVersion = "1.0"; XQueryExecutable exp = compiler.Compile(query); XQueryEvaluator eval = exp.Load(); Serializer qout = new Serializer(); eval.Run(qout); }
/// <summary> /// Demonstrate XQuery extensibility using user-written extension functions /// </summary> /// <remarks>Note: If SamplesExtensions is compiled to a different assembly than ExamplesPE, use /// the namespace URI clitype:SampleExtensions.SampleExtensions?asm=ASSEMBLY_NAME_HERE /// </remarks> public override void run(Uri samplesDir) { String query = "declare namespace ext = \"clitype:SampleExtensions.SampleExtensions?asm=ExamplesPE\";" + "<out>" + " <addition>{ext:add(2,2)}</addition>" + " <average>{ext:average((1,2,3,4,5,6))}</average>" + " <language>{ext:hostLanguage()}</language>" + "</out>"; Processor processor = new Processor(); XQueryCompiler compiler = processor.NewXQueryCompiler(); XQueryExecutable exp = compiler.Compile(query); XQueryEvaluator eval = exp.Load(); Serializer qout = new Serializer(); eval.Run(qout); }
/// <summary> /// Show a direct call on a user-defined function defined within the query /// </summary> public override void run(Uri samplesDir) { Processor processor = new Processor(); XQueryCompiler qc = processor.NewXQueryCompiler(); XQueryExecutable exp1 = qc.Compile("declare namespace f='f.ns';" + "declare variable $z := 1 + xs:integer(doc-available('" + new Uri(samplesDir, "data/books.xml").ToString() + "'));" + "declare variable $p as xs:integer external;" + "declare function f:t1($v1 as xs:integer) { " + " $v1 div $z + $p" + "};" + "10"); XQueryEvaluator ev = exp1.Load(); ev.SetExternalVariable(new QName("", "p"), new XdmAtomicValue(39)); XdmValue v1 = new XdmAtomicValue(10); XdmValue result = ev.CallFunction(new QName("f.ns", "f:t1"), new XdmValue[] { v1 }); Console.WriteLine("First result (expected 44): " + result.ToString()); v1 = new XdmAtomicValue(20); result = ev.CallFunction(new QName("f.ns", "f:t1"), new XdmValue[] { v1 }); Console.WriteLine("Second result (expected 49): " + result.ToString()); }
/// <summary> /// Show a query that takes a parameter (external variable) as input. /// The query produces a single atomic value as its result and returns the value /// to the C# application. /// </summary> public override void run(Uri samplesDir) { Processor processor = new Processor(); XQueryCompiler compiler = processor.NewXQueryCompiler(); compiler.DeclareNamespace("p", "http://saxon.sf.net/ns/p"); XQueryExecutable exp = compiler.Compile( "declare variable $p:in as xs:integer external; $p:in * $p:in"); XQueryEvaluator eval = exp.Load(); eval.SetExternalVariable(new QName("http://saxon.sf.net/ns/p", "p:in"), new XdmAtomicValue(12)); XdmAtomicValue result = (XdmAtomicValue)eval.EvaluateSingle(); Console.WriteLine("Result type: " + result.Value.GetType()); Console.WriteLine("Result value: " + (long)result.Value); }
public override void run(Uri samplesDir) { String query = "xquery version '1.1'; try {doc('book.xml')}catch * {\"XQuery 1.1 catch clause - file not found.\"}"; Processor processor = new Processor(); XQueryCompiler compiler = processor.NewXQueryCompiler(); compiler.XQueryLanguageVersion = "1.1"; XQueryExecutable exp = compiler.Compile(query); XQueryEvaluator eval = exp.Load(); Serializer qout = new Serializer(); eval.Run(qout); }
/** * Method main. First argument is the Saxon samples directory. */ public static void Main(String[] argv) { String samplesDir; if (argv.Length > 0) { samplesDir = argv[0]; } else { String home = Environment.GetEnvironmentVariable("SAXON_HOME"); if (home == null) { Console.WriteLine("No input directory supplied, and SAXON_HOME is not set"); return; } else { if (home.EndsWith("/") || home.EndsWith("\\")) { samplesDir = home + "samples/"; } else { samplesDir = home + "/samples/"; } } } UriBuilder ub = new UriBuilder(); ub.Scheme = "file"; ub.Host = ""; ub.Path = samplesDir; Uri baseUri = ub.Uri; Console.WriteLine("Base URI: " + baseUri.ToString()); // Create a schema-aware Processor Processor saxon = new Processor(true); // Load a schema SchemaManager manager = saxon.SchemaManager; manager.ErrorList = new ArrayList(); Uri schemaUri = new Uri(baseUri, "data/books.xsd"); try { manager.Compile(schemaUri); } catch (Exception e) { Console.WriteLine("Schema compilation failed with " + manager.ErrorList.Count + " errors"); foreach (StaticError error in manager.ErrorList) { Console.WriteLine("At line " + error.LineNumber + ": " + error.Message); } return; } // Use this to validate an instance document SchemaValidator validator = manager.NewSchemaValidator(); Uri instanceUri = new Uri(baseUri, "data/books.xml"); validator.SetSource(instanceUri); validator.ErrorList = new ArrayList(); XdmDestination psvi = new XdmDestination(); validator.SetDestination(psvi); try { validator.Run(); } catch (Exception e) { Console.WriteLine("Instance validation failed with " + validator.ErrorList.Count + " errors"); foreach (StaticError error in validator.ErrorList) { Console.WriteLine("At line " + error.LineNumber + ": " + error.Message); } } // Run a query on the result to check that it has type annotations XQueryCompiler xq = saxon.NewXQueryCompiler(); XQueryEvaluator xv = xq.Compile("data((//PRICE)[1]) instance of xs:decimal").Load(); xv.ContextItem = psvi.XdmNode; Console.WriteLine("Price is decimal? " + xv.EvaluateSingle().ToString()); }
public XQueryProcessor() { _processor = new Processor(); _compiler = _processor.NewXQueryCompiler(); }
/// <summary> /// Show validation of an instance document against a schema /// </summary> public override void run(Uri samplesDir) { // Load a schema Processor processor; try { processor = new Processor(true); } catch (Exception err) { Console.WriteLine(err); Console.WriteLine("Failed to load Saxon-EE (use -HE option to run Saxon-HE tests only)"); return; } processor.SetProperty("http://saxon.sf.net/feature/timing", "true"); processor.SetProperty("http://saxon.sf.net/feature/validation-warnings", "true"); SchemaManager manager = processor.SchemaManager; manager.XsdVersion = "1.1"; manager.ErrorList = new ArrayList(); Uri schemaUri = new Uri(samplesDir, "data/books.xsd"); try { manager.Compile(schemaUri); } catch (Exception e) { Console.WriteLine(e); Console.WriteLine("Schema compilation failed with " + manager.ErrorList.Count + " errors"); foreach (StaticError error in manager.ErrorList) { Console.WriteLine("At line " + error.LineNumber + ": " + error.Message); } return; } // Use this to validate an instance document SchemaValidator validator = manager.NewSchemaValidator(); //Uri instanceUri = new Uri(samplesDir, "data/books-invalid.xml"); //validator.SetSource(instanceUri); XmlReader xmlReader = XmlReader.Create(samplesDir + "data/books-invalid.xml"); validator.SetSource(xmlReader); validator.ErrorList = new ArrayList(); XdmDestination psvi = new XdmDestination(); validator.SetDestination(psvi); try { validator.Run(); } catch (Exception e) { Console.WriteLine(e); Console.WriteLine("Instance validation failed with " + validator.ErrorList.Count + " errors"); foreach (StaticError error in validator.ErrorList) { Console.WriteLine("At line " + error.LineNumber + ": " + error.Message); } return; } // Run a query on the result to check that it has type annotations XQueryCompiler xq = processor.NewXQueryCompiler(); XQueryEvaluator xv = xq.Compile("data((//PRICE)[1]) instance of xs:decimal").Load(); xv.ContextItem = psvi.XdmNode; Console.WriteLine("Price is decimal? " + xv.EvaluateSingle().ToString()); }
/// <summary> /// Show a query producing a Saxon tree as its input and producing a Saxon tree as its output /// </summary> public override void run(Uri samplesDir) { Processor processor = new Processor(); DocumentBuilder loader = processor.NewDocumentBuilder(); loader.BaseUri = new Uri(samplesDir, "data/books.xml"); XdmNode indoc = loader.Build(loader.BaseUri); XQueryCompiler compiler = processor.NewXQueryCompiler(); XQueryExecutable exp = compiler.Compile("<doc>{reverse(/*/*)}</doc>"); XQueryEvaluator eval = exp.Load(); eval.ContextItem = indoc; XdmDestination qout = new XdmDestination(); eval.Run(qout); XdmNode outdoc = qout.XdmNode; Console.WriteLine(outdoc.OuterXml); }
/// <summary> /// Show a query reading an input document using an XmlReader (the .NET XML parser) /// </summary> public override void run(Uri samplesDir) { Processor processor = new Processor(); String inputFileName = new Uri(samplesDir, "data/books.xml").ToString(); XmlTextReader reader = new XmlTextReader(inputFileName, UriConnection.getReadableUriStream(new Uri(samplesDir, "data/books.xml"))); //new FileStream(inputFileName, FileMode.Open, FileAccess.Read)); reader.Normalization = true; // add a validating reader - not to perform validation, but to expand entity references XmlValidatingReader validator = new XmlValidatingReader(reader); validator.ValidationType = ValidationType.None; XdmNode doc = processor.NewDocumentBuilder().Build(validator); XQueryCompiler compiler = processor.NewXQueryCompiler(); XQueryExecutable exp = compiler.Compile("/"); XQueryEvaluator eval = exp.Load(); eval.ContextItem = doc; Serializer qout = new Serializer(); qout.SetOutputProperty(Serializer.METHOD, "xml"); qout.SetOutputProperty(Serializer.INDENT, "yes"); qout.SetOutputStream(new FileStream("testoutput2.xml", FileMode.Create, FileAccess.Write)); eval.Run(qout); }
/// <summary> /// Demonstrate XQuery Update /// </summary> public override void run(Uri samplesDir) { Processor processor = new Processor(true); DocumentBuilder loader = processor.NewDocumentBuilder(); loader.BaseUri = new Uri(samplesDir, "data/books.xml"); loader.TreeModel = TreeModel.LinkedTree; XdmNode indoc = loader.Build(new Uri(samplesDir, "data/books.xml")); Console.Out.WriteLine("=========== BEFORE UPDATE ==========="); Serializer serializer0 = new Serializer(); serializer0.SetOutputProperty(Serializer.METHOD, "xml"); serializer0.SetOutputProperty(Serializer.INDENT, "yes"); serializer0.SetOutputWriter(Console.Out); processor.WriteXdmValue(indoc, serializer0); String query = "for $i in //PRICE return \n" + "replace value of node $i with $i - 0.05"; XQueryCompiler compiler = processor.NewXQueryCompiler(); compiler.UpdatingEnabled = true; XQueryExecutable exp = compiler.Compile(query); XQueryEvaluator eval = exp.Load(); eval.ContextItem = indoc; XdmNode[] updatedNodes = eval.RunUpdate(); foreach (XdmNode root in updatedNodes) { Uri documentUri = root.DocumentUri; if (documentUri != null && documentUri.Scheme == "file") { Stream stream = UriConnection.getWritableUriStream(documentUri); Serializer serializer = new Serializer(); serializer.SetOutputProperty(Serializer.METHOD, "xml"); serializer.SetOutputProperty(Serializer.INDENT, "yes"); serializer.SetOutputStream(stream); processor.WriteXdmValue(root, serializer); } } Console.Out.WriteLine("=========== AFTER UPDATE ==========="); processor.WriteXdmValue(indoc, serializer0); }