Project

Profile

Help

Sample XQueryEvaluator with collections in C#

Added by Anonymous over 15 years ago

Legacy ID: #5724079 Legacy Poster: Eric Chaves (epchaves)

Hi Folks, I'm playing arounf with Saxon API in order to get used to it and for that I'm trying to build a console application pretty much like the XQuery console. The problem i'm facing is that all samples use a single document in the XQueryEvaluator.ContextItem and I'd like to use a collection dclared in xquery itself. How should I create the instance of my XQueryEvaluator for that? Thansk in advance.


Replies (6)

Please register to reply

RE: Sample XQueryEvaluator with collections in C# - Added by Anonymous over 15 years ago

Legacy ID: #5724183 Legacy Poster: Michael Kay (mhkay)

Please make sure that you are using Saxon 9.1.0.3 to ensure that you don't run in to this bug: https://sourceforge.net/tracker2/?func=detail&aid=2151995&group_id=29872&atid=397617 Beyond that (a) you don't need to supply a ContextItem if your query doesn't use it (b) which of the various techniques to you want to use for naming your collection? See http://www.saxonica.com/documentation/sourcedocs/collections.html and also the Processor.RegisterCollection() method.

RE: Sample XQueryEvaluator with collections in C# - Added by Anonymous over 15 years ago

Legacy ID: #5724686 Legacy Poster: Eric Chaves (epchaves)

Hi Michael, Thanks for the promptly reply. After reading your answer I believe that I might facing some other error. I have an xquery that works ok when I execute it with query.exe that cames with Saxon. This xquery use a collection in the form of the following pseudo-code: let $path := "file:///C:/MyXmlFiles?select=*.xml" let $files := collection($path) let $purchases := $files/XPath/Expression return $purchases My console application does pretty much the same of ExampleFromXmlReader Processor processor = new Processor(); XQueryCompiler compiler = processor.NewXQueryCompiler(); XQueryExecutable exp = compiler.Compile(textQuery.Text); XQueryEvaluator eval = exp.Load(); 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); When I hit the eval.Run I got a NullReferenceException (described below) that seems to be associated with the DotNetCollectionURIResolver, but I just cant figure out what I might be doing wrong. Am I lacking something here? Thanks again for the help. Full exception details: System.NullReferenceException was unhandled Message="Object reference not set to an instance of an object." Source="IKVM.OpenJDK.ClassLibrary" StackTrace: at java.lang.String.instancehelper_length(String this) at java.net.URI.Parser.parse(Boolean ) at java.net.URI..ctor(String str) at net.sf.saxon.dotnet.DotNetCollectionURIResolver.resolve(String href, String base, XPathContext context) at net.sf.saxon.functions.Collection.iterate(XPathContext context) at net.sf.saxon.expr.PathExpression.iterate(XPathContext context) at net.sf.saxon.expr.PathExpression.iterate(XPathContext context) at net.sf.saxon.expr.PathExpression.iterate(XPathContext context) at net.sf.saxon.expr.PathExpression.iterate(XPathContext context) at net.sf.saxon.expr.PathExpression.iterate(XPathContext context) at net.sf.saxon.expr.PathExpression.iterate(XPathContext context) at net.sf.saxon.sort.DocumentSorter.iterate(XPathContext context) at net.sf.saxon.expr.ForExpression.process(XPathContext context) at net.sf.saxon.query.XQueryExpression.run(DynamicQueryContext env, Result result, Properties outputProperties) at Saxon.Api.XQueryEvaluator.Run(XmlDestination destination) at ProcessQuery.Form1.RunQuery() in C:\Saxon\samples\ProcessQuery\ProcessQuery\Form1.cs:line 46 at ProcessQuery.Form1.cutToolStripButton_Click(Object sender, EventArgs e) in C:\Saxon\samples\ProcessQuery\ProcessQuery\Form1.cs:line 34 at System.Windows.Forms.ToolStripItem.RaiseEvent(Object key, EventArgs e) at System.Windows.Forms.ToolStripButton.OnClick(EventArgs e) at System.Windows.Forms.ToolStripItem.HandleClick(EventArgs e) at System.Windows.Forms.ToolStripItem.HandleMouseUp(MouseEventArgs e) at System.Windows.Forms.ToolStripItem.FireEventInteractive(EventArgs e, ToolStripItemEventType met) at System.Windows.Forms.ToolStripItem.FireEvent(EventArgs e, ToolStripItemEventType met) at System.Windows.Forms.ToolStrip.OnMouseUp(MouseEventArgs mea) at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks) at System.Windows.Forms.Control.WndProc(Message& m) at System.Windows.Forms.ScrollableControl.WndProc(Message& m) at System.Windows.Forms.ToolStrip.WndProc(Message& m) at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m) at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m) at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam) at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg) at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData) at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context) at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context) at System.Windows.Forms.Application.Run(Form mainForm) at ProcessQuery.Program.Main() in C:\Saxon\samples\ProcessQuery\ProcessQuery\Program.cs:line 17 at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args) at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args) at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() at System.Threading.ThreadHelper.ThreadStart_Context(Object state) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) at System.Threading.ThreadHelper.ThreadStart()

RE: Sample XQueryEvaluator with collections in C# - Added by Anonymous over 15 years ago

Legacy ID: #5724752 Legacy Poster: Michael Kay (mhkay)

That's rather consistent with the bug I referred you to. Are you sure you are using 9.1.0.3? Michael Kay http://www.saxonica.com/

RE: Sample XQueryEvaluator with collections in C# - Added by Anonymous over 15 years ago

Legacy ID: #5725003 Legacy Poster: Eric Chaves (epchaves)

Yep. below you have the output of the loaded assemblies. 'ProcessQuery.vshost.exe' (Managed): Loaded 'C:\Saxon\samples\ProcessQuery\ProcessQuery\bin\Debug\ProcessQuery.exe', Symbols loaded. 'ProcessQuery.vshost.exe' (Managed): Loaded 'C:\WINDOWS\assembly\GAC_MSIL\saxon9api\9.1.0.3__e1fdd002d5083fe6\saxon9api.netmodule', No symbols loaded. 'ProcessQuery.vshost.exe' (Managed): Loaded 'C:\WINDOWS\assembly\GAC\saxon9\9.1.0.3__e1fdd002d5083fe6\saxon9.dll', No symbols loaded. 'ProcessQuery.vshost.exe' (Managed): Loaded 'C:\WINDOWS\assembly\GAC\IKVM.OpenJDK.ClassLibrary\0.36.0.11__13235d27fcbfff58\IKVM.OpenJDK.ClassLibrary.dll', No symbols loaded. 'ProcessQuery.vshost.exe' (Managed): Loaded 'C:\WINDOWS\assembly\GAC\IKVM.Runtime\0.36.0.11__13235d27fcbfff58\IKVM.Runtime.dll', No symbols loaded. 'ProcessQuery.vshost.exe' (Managed): Loaded 'C:\WINDOWS\assembly\GAC_MSIL\System.Configuration\2.0.0.0__b03f5f7f11d50a3a\System.Configuration.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.

RE: Sample XQueryEvaluator with collections in C# - Added by Anonymous over 15 years ago

Legacy ID: #5735512 Legacy Poster: Michael Kay (mhkay)

I think (after investigating lot of other possibilities without success...) that the problem is that your query has no static base URI. Please use the BaseUri property of the XQueryCompiler to set a base URI. I'll change the code for the future to be more resilient to this condition - it shouldn't really matter given that the collection URI you have given is absolute, but Saxon tries to resolve it against the base URI of the query anyway. (It's always a good idea to ensure the query has a known base URI, though Saxon does try to do as much as it can even if none is supplied.)

RE: Sample XQueryEvaluator with collections in C# - Added by Anonymous over 15 years ago

Legacy ID: #5747173 Legacy Poster: Eric Chaves (epchaves)

Hi Michael, That was it!! thanks. Regards, Eric.

    (1-6/6)

    Please register to reply