Project

Profile

Help

How to connect?
Download (74.1 KB) Statistics
| Branch: | Tag: | Revision:

he / tags / 9.9.1.4 / samples / cs / ExamplesHE.cs @ c74fd4aa

1
using System;
2
using System.IO;
3
using System.Collections;
4
using System.Xml;
5
using System.Net;
6
using Saxon.Api;
7
using System.Collections.Generic;
8

    
9
namespace SaxonHE
10
{
11
    class ExamplesHE
12
    {
13
        /// <summary>
14
        /// Run Saxon XSLT and XQuery sample applications
15
        /// </summary>
16
        /// <param name="argv">
17
        /// <para>Options:</para>
18
        /// <list>
19
        /// <item>-test:testname  run a specific test</item>
20
        /// <item>-dir:samplesdir directory containing the sample data files (default %SAXON_HOME%/samples)</item>
21
        /// <item>-ask:yes|no     indicates whether to prompt for confirmation after each test (default yes)</item>
22
        /// </list>
23
        /// </param>
24

    
25

    
26
        public static void Main(String[] argv)
27
        {
28

    
29
            Example[] examples = {
30
				new XPathSimple(),
31
				new XPathSimple2(),
32
                new XPathVariables(),
33
                new XPathUndeclaredVariables(),
34
                new XPathWithStaticError(),
35
                new XPathWithDynamicError(),
36
                new XsltSimple1(),
37
                new XsltSimple2(),
38
                new XsltSimple3(),
39
                new XsltStripSpace(),
40
                new XsltReuseExecutable(),
41
                new XsltReuseTransformer(),
42
                new XsltFilterChain(),
43
                new XsltDomToDom(),
44
                new XsltXdmToXdm(),
45
                new XsltXdmElementToXdm(),
46
                new XsltUsingSourceResolver(),
47
                new XsltSettingOutputProperties(),
48
                new XsltDisplayingErrors(),
49
                new XsltCapturingErrors(),
50
                new XsltCapturingMessages(),
51
                new XsltProcessingInstruction(),
52
                new XsltMultipleOutput(),
53
                new XsltUsingResultHandler(),
54
                new XsltUsingIdFunction(),
55
                new XsltUsingRegisteredCollection(),
56
                new XsltUsingDirectoryCollection(),
57
                new XsltIntegratedExtension(),
58
                new XQueryToStream(),
59
                new XQueryToAtomicValue(),
60
                new XQueryToSequence(),
61
                new XQueryToDom(),
62
                new XQueryToXdm(),
63
                new XQueryCallFunction(),
64
                new XQueryFromXmlReader(),
65
                new XQueryToSerializedSequence(),
66
				new XQueryUsingParameter(),
67
				new XQueryMultiModule()
68
            };
69

    
70
            Boolean ask = true;
71
            String test = "all";
72

    
73
            String samplesPath = null;
74
            Uri samplesDir;
75

    
76
            foreach (String s in argv)
77
            {
78
                if (s.StartsWith("-test:"))
79
                {
80
                    test = s.Substring(6);
81
                }
82
                else if (s.StartsWith("-dir:"))
83
                {
84
                    samplesPath = s.Substring(5);
85
                }
86
                else if (s == "-ask:yes")
87
                {
88
                    // no action
89
                }
90
                else if (s == "-ask:no")
91
                {
92
                    ask = false;
93
                }
94
                else if (s == "-?")
95
                {
96
                    Console.WriteLine("ExamplesHE -dir:samples -test:testname -ask:yes|no");
97
                }
98
                else
99
                {
100
                    Console.WriteLine("Unrecognized Argument: " + s);
101
                    return;
102
                }
103
            }
104
            if (samplesPath != null)
105
            {
106
                if (samplesPath.StartsWith("file:///"))
107
                {
108
                    samplesPath = samplesPath.Substring(8);
109
                }
110
                else if (samplesPath.StartsWith("file:/"))
111
                {
112
                    samplesPath = samplesPath.Substring(6);
113
                }
114

    
115
            }
116
            else
117
            {
118
                String home = Environment.GetEnvironmentVariable("SAXON_HOME");
119
                if (home == null)
120
                {
121
                    Console.WriteLine("No input directory supplied, and SAXON_HOME is not set");
122
                    return;
123
                }
124
                else
125
                {
126
                    if (!(home.EndsWith("/") || home.EndsWith("\\")))
127
                    {
128
                        home = home + "/";
129
                    }
130
                    samplesPath = home + "samples/";
131
                }
132
            }
133

    
134
            if (!(samplesPath.EndsWith("/") || samplesPath.EndsWith("\\")))
135
            {
136
                samplesPath = samplesPath + "/";
137
            }
138

    
139
            if (!File.Exists(samplesPath + "data/books.xml"))
140
            {
141
                Console.WriteLine("Supplied samples directory " + samplesPath + " does not contain the Saxon sample data files");
142
                return;
143
            }
144

    
145
            try
146
            {
147
                samplesDir = new Uri(samplesPath);
148
            }
149
            catch
150
            {
151
                Console.WriteLine("Invalid URI for samples directory: " + samplesPath);
152
                return;
153
			}
154

    
155
			Boolean found = false;
156
            foreach (Example ex in examples)
157
            {
158
                if (test == "all" || test == ex.testName)
159
                {
160
					Console.WriteLine("\n\n===== " + ex.testName + " =======\n");
161
					found = true;
162
                    try
163
                    {
164
                        ex.run(samplesDir);
165
                    }
166
                    catch (Saxon.Api.StaticError se)
167
                    {
168
                        Console.WriteLine("Test failed with static error " + se.ErrorCode.LocalName + ": " + se.Message);
169
                    }
170
                    catch (Saxon.Api.DynamicError de)
171
                    {
172
                        Console.WriteLine("Test failed with dynamic error " + de.ErrorCode.LocalName + ": " + de.Message);
173
                    }
174
                    catch (Exception exc)
175
                    {
176
                        Console.WriteLine("Test failed unexpectedly (" + exc.GetType() + "): " + exc.Message);
177
                        Console.WriteLine(exc.StackTrace);
178
                    }
179
                    if (ask)
180
                    {
181
                        Console.WriteLine("\n\nContinue? - type (Y(es)/N(o)/A(ll))");
182
                        String answer = Console.ReadLine();
183
                        if (answer == "N" || answer == "n")
184
                        {
185
                            break;
186
                        }
187
                        else if (answer == "A" || answer == "a")
188
                        {
189
                            ask = false;
190
                        }
191
                    }
192
                }
193
			}
194
			if (!found) {
195
				Console.WriteLine("Please supply a valid test name, or 'all' ('" + test + "' is invalid)");
196
			}
197
            Console.WriteLine("\n==== done! ====");
198
        }
199
    }
200

    
201
    ///<summary>
202
    /// Each of the example programs is implemented as a subclass of the abstract class Example
203
    ///</summary> 
204

    
205

    
206
    public abstract class Example
207
    {
208
        /// <summary>
209
        /// Read-only property: the name of the test example
210
        /// </summary>
211
        public abstract String testName { get; }
212
        /// <summary>
213
        /// Entry point for running the example
214
        /// </summary>
215
        public abstract void run(Uri samplesDir);
216
    }
217

    
218
    /// <summary>
219
	/// Evaluate an XPath expression selecting from a source document supplied as a URI
220
    /// </summary>
221

    
222
    public class XPathSimple : Example
223
    {
224

    
225
        public override String testName
226
        {
227
            get { return "XPathSimple"; }
228
        }
229

    
230
        public override void run(Uri samplesDir)
231
        {
232
            // Create a Processor instance.
233
            Processor processor = new Processor();
234

    
235
            // Load the source document
236
            XdmNode input = processor.NewDocumentBuilder().Build(new Uri(samplesDir, "data/books.xml"));
237

    
238
            // Create an XPath compiler
239
            XPathCompiler xpath = processor.NewXPathCompiler();
240

    
241
            // Enable caching, so each expression is only compiled once
242
            xpath.Caching = true;
243

    
244
            // Compile and evaluate some XPath expressions
245
            foreach (XdmItem item in xpath.Evaluate("//ITEM", input))
246
            {
247
                Console.WriteLine("TITLE: " + xpath.EvaluateSingle("string(TITLE)", item));
248
                Console.WriteLine("PRICE: " + xpath.EvaluateSingle("string(PRICE)", item));
249
            }
250
        }
251
	}
252

    
253
	/// <summary>
254
	/// Evaluate an XPath expression against a source document, returning its effective boolean value
255
	/// </summary>
256

    
257
    public class XPathSimple2 : Example
258
    {
259

    
260
        public override String testName
261
        {
262
            get { return "XPathSimple2"; }
263
        }
264

    
265
        public override void run(Uri samplesDir)
266
        {
267
            // Create a Processor instance.
268
            Processor processor = new Processor();
269

    
270
            // Load the source document
271
            XdmNode input = processor.NewDocumentBuilder().Build(new Uri(samplesDir, "data/books.xml"));
272

    
273
            // Create an XPath compiler
274
            XPathCompiler xpath = processor.NewXPathCompiler();
275

    
276
            // Enable caching, so each expression is only compiled once
277
            xpath.Caching = true;
278
            
279
            // Compile and evaluate an XPath expression
280
			XPathSelector selector = xpath.Compile("//ITEM").Load();
281
            selector.ContextItem = input;
282
			Console.WriteLine(selector.EffectiveBooleanValue());
283

    
284
        }
285
    }
286

    
287
    /// <summary>
288
	/// Evaluate an XPath expression using variables (and no source document)
289
    /// </summary>
290

    
291
    public class XPathVariables : Example
292
    {
293

    
294
        public override String testName
295
        {
296
            get { return "XPathVariables"; }
297
        }
298

    
299
        public override void run(Uri samplesDir)
300
        {
301
            // Create a Processor instance.
302
            Processor processor = new Processor();
303

    
304
            // Create the XPath expression.
305
            XPathCompiler compiler = processor.NewXPathCompiler();
306
            compiler.DeclareVariable(new QName("", "a"));
307
            compiler.DeclareVariable(new QName("", "b"));
308
            XPathSelector selector = compiler.Compile("$a + $b").Load();
309

    
310
            // Set the values of the variables
311
            selector.SetVariable(new QName("", "a"), new XdmAtomicValue(2));
312
            selector.SetVariable(new QName("", "b"), new XdmAtomicValue(3));
313

    
314
            // Evaluate the XPath expression
315
            Console.WriteLine(selector.EvaluateSingle().ToString());
316
        }
317
    }
318

    
319
    /// <summary>
320
	/// Evaluate an XPath expression using variables without explicit declaration
321
    /// </summary>
322

    
323
    public class XPathUndeclaredVariables : Example
324
    {
325

    
326
        public override String testName
327
        {
328
            get { return "XPathUndeclaredVariables"; }
329
        }
330

    
331
        public override void run(Uri samplesDir)
332
        {
333
            // Create a Processor instance.
334
            Processor processor = new Processor();
335

    
336
            // Create the XPath expression.
337
            XPathCompiler compiler = processor.NewXPathCompiler();
338
            compiler.AllowUndeclaredVariables = true;
339
            XPathExecutable expression = compiler.Compile("$a + $b");
340
            XPathSelector selector = expression.Load();
341

    
342
            // Set the values of the variables
343
            IEnumerator<QName> vars = expression.EnumerateExternalVariables2();
344
            while (vars.MoveNext())
345
            {
346
                selector.SetVariable(vars.Current, new XdmAtomicValue(10));
347
            }
348

    
349
            // Evaluate the XPath expression
350
            XdmItem item = selector.EvaluateSingle();
351
            if (item == null) {
352
                Console.WriteLine("evaluate single returns null!!!");
353
            } else {
354
                Console.WriteLine(item.ToString());
355
            }
356
        }
357
    }
358

    
359
    /// <summary>
360
	/// Evaluate an XPath expression throwing a static error
361
    /// </summary>
362

    
363
    public class XPathWithStaticError : Example
364
    {
365

    
366
        public override String testName
367
        {
368
            get { return "XPathWithStaticError"; }
369
        }
370

    
371
        public override void run(Uri samplesDir)
372
        {
373
            // Create a Processor instance.
374
            Processor processor = new Processor();
375

    
376
            // Create the XPath expression.
377
            XPathCompiler compiler = processor.NewXPathCompiler();
378
            compiler.AllowUndeclaredVariables = true;
379
            XPathExecutable expression = compiler.Compile("1 + unknown()");
380
            XPathSelector selector = expression.Load();
381

    
382
            // Evaluate the XPath expression
383
            Console.WriteLine(selector.EvaluateSingle().ToString());
384
        }
385
    }
386

    
387
    /// <summary>
388
	/// Evaluate an XPath expression throwing a dynamic error
389
    /// </summary>
390

    
391
    public class XPathWithDynamicError : Example
392
    {
393

    
394
        public override String testName
395
        {
396
            get { return "XPathWithDynamicError"; }
397
        }
398

    
399
        public override void run(Uri samplesDir)
400
        {
401
            // Create a Processor instance.
402
            Processor processor = new Processor();
403

    
404
            // Create the XPath expression.
405
            XPathCompiler compiler = processor.NewXPathCompiler();
406
            compiler.AllowUndeclaredVariables = true;
407
            XPathExecutable expression = compiler.Compile("$a gt $b");
408
            XPathSelector selector = expression.Load();
409

    
410
            // Set the values of the variables
411
            selector.SetVariable(new QName("", "a"), new XdmAtomicValue(10));
412
            selector.SetVariable(new QName("", "b"), new XdmAtomicValue("Paris"));
413

    
414
            // Evaluate the XPath expression
415
            XdmItem item = selector.EvaluateSingle();
416
            if (item == null)
417
            {
418

    
419
                Console.WriteLine("Evaluate single item is null!!");
420
            }
421
            else
422
            {
423
                Console.WriteLine(item.ToString());
424
            }
425
        }
426
    }
427

    
428
    /// <summary>
429
    /// XSLT 2.0 transformation with source document and stylesheet supplied as URIs
430
    /// </summary>
431

    
432
    public class XsltSimple1 : Example
433
    {
434

    
435
        public override String testName
436
        {
437
            get { return "XsltSimple1"; }
438
        }
439

    
440
        public override void run(Uri samplesDir)
441
        {
442
            // Create a Processor instance.
443
            Processor processor = new Processor();
444

    
445
            // Load the source document
446
            XdmNode input = processor.NewDocumentBuilder().Build(new Uri(samplesDir, "data/books.xml"));
447

    
448
            // Create a transformer for the stylesheet.
449
            Xslt30Transformer transformer = processor.NewXsltCompiler().Compile(new Uri(samplesDir, "styles/books.xsl")).Load30();
450

    
451
            // Create a serializer, with output to the standard output stream
452
            Serializer serializer = processor.NewSerializer();
453
            serializer.SetOutputWriter(Console.Out);
454

    
455
            // Transform the source XML and serialize the result document
456
            transformer.ApplyTemplates(input, serializer);
457
        }
458
    }
459

    
460
    /// <summary>
461
    /// Run a transformation, sending the serialized output to a file
462
    /// </summary>
463

    
464
    public class XsltSimple2 : Example
465
    {
466

    
467
        public override String testName
468
        {
469
            get { return "XsltSimple2"; }
470
        }
471

    
472
        public override void run(Uri samplesDir)
473
        {
474
            // Create a Processor instance.
475
            Processor processor = new Processor();
476

    
477
            // Load the source document
478
            XdmNode input = processor.NewDocumentBuilder().Build(new Uri(samplesDir, "data/books.xml"));
479

    
480
            // Create a transformer for the stylesheet.
481
            Xslt30Transformer transformer = processor.NewXsltCompiler().Compile(new Uri(samplesDir, "styles/identity.xsl")).Load30();
482

    
483
            // Create a serializer
484
            String outfile = "OutputFromXsltSimple2.xml";
485
            Serializer serializer = processor.NewSerializer();
486
            serializer.SetOutputStream(new FileStream(outfile, FileMode.Create, FileAccess.Write));
487

    
488
			// Transform the source XML and serialize the result to the output file.
489
            transformer.ApplyTemplates(input, serializer);
490

    
491
            Console.WriteLine("\nOutput written to " + outfile + "\n");
492
        }
493
    }
494

    
495
    /// <summary>
496
    /// XSLT 2.0 transformation with source document and stylesheet supplied as files
497
    /// </summary>
498

    
499
    public class XsltSimple3 : Example
500
    {
501

    
502
        public override String testName
503
        {
504
            get { return "XsltSimple3"; }
505
        }
506

    
507
        public override void run(Uri samplesDir)
508
        {
509
            if (samplesDir.Scheme != Uri.UriSchemeFile)
510
            {
511
                Console.WriteLine("Supplied URI must be a file directory");
512
            }
513
            String dir = samplesDir.AbsolutePath;
514
            String sourceFile = dir + "data/books.xml";
515
            String styleFile = dir + "styles/books.xsl";
516

    
517
            // Create a Processor instance.
518
            Processor processor = new Processor();
519

    
520
            // Load the source document
521
            DocumentBuilder builder = processor.NewDocumentBuilder();
522
            builder.BaseUri = new Uri(samplesDir, "data/books.xml");
523

    
524
            XdmNode input = builder.Build(File.OpenRead(sourceFile));
525

    
526
            // Create a transformer for the stylesheet.
527
            XsltCompiler compiler = processor.NewXsltCompiler();
528
            compiler.BaseUri = new Uri(samplesDir, "styles/books.xsl");
529
            Xslt30Transformer transformer = compiler.Compile(File.OpenRead(styleFile)).Load30();
530

    
531
            // Create a serializer, with output to the standard output stream
532
            Serializer serializer = processor.NewSerializer();
533
            serializer.SetOutputWriter(Console.Out);
534

    
535
            // Transform the source XML and serialize the result document
536
            transformer.ApplyTemplates(input, serializer);
537
        }
538
    }
539

    
540

    
541
    /// <summary>
542
    /// XSLT 2.0 transformation showing stripping of whitespace controlled by the stylesheet
543
    /// </summary>
544

    
545
    public class XsltStripSpace : Example
546
    {
547

    
548
        public override String testName
549
        {
550
            get { return "XsltStripSpace"; }
551
        }
552

    
553
        public override void run(Uri samplesDir)
554
        {
555
            Processor processor = new Processor();
556

    
557
            // Load the source document
558
            DocumentBuilder builder = processor.NewDocumentBuilder();
559
            builder.BaseUri = samplesDir;
560

    
561
            String doc = "<doc>  <a>  <b>text</b>  </a>  <a/>  </doc>";
562
            MemoryStream ms = new MemoryStream();
563
            StreamWriter tw = new StreamWriter(ms);
564
            tw.Write(doc);
565
            tw.Flush();
566
            Stream instr = new MemoryStream(ms.GetBuffer(), 0, (int)ms.Length);
567
            XdmNode input = builder.Build(instr);
568

    
569
            // Create a transformer for the stylesheet.
570
            String stylesheet =
571
                "<xsl:stylesheet xmlns:xsl='http://www.w3.org/1999/XSL/Transform' version='2.0'>" +
572
                "<xsl:strip-space elements='*'/>" +
573
                "<xsl:template match='/'>" +
574
                "  <xsl:copy-of select='.'/>" +
575
                "</xsl:template>" +
576
                "</xsl:stylesheet>";
577

    
578
            XsltCompiler compiler = processor.NewXsltCompiler();
579
            compiler.BaseUri = samplesDir;
580
            Xslt30Transformer transformer = compiler.Compile(new XmlTextReader(new StringReader(stylesheet))).Load30();
581
            
582
			// Create a serializer, with output to the standard output stream
583
            Serializer serializer = processor.NewSerializer();
584
            serializer.SetOutputWriter(Console.Out);
585

    
586
			// Transform the source XML and serialize the result document
587
            transformer.ApplyTemplates(input, serializer);
588
        }
589
    }
590

    
591

    
592
	/// <summary>
593
	/// Run a transformation, compiling the stylesheet once (into an XsltExecutable) and using it to transform two 
594
	/// different source documents
595
	/// </summary>
596

    
597
    public class XsltReuseExecutable : Example
598
    {
599

    
600
        public override String testName
601
        {
602
            get { return "XsltReuseExecutable"; }
603
        }
604

    
605
        public override void run(Uri samplesDir)
606
        {
607
            // Create a Processor instance.
608
            Processor processor = new Processor();
609

    
610
            // Create a compiled stylesheet
611
            XsltExecutable templates = processor.NewXsltCompiler().Compile(new Uri(samplesDir, "styles/summarize.xsl"));
612

    
613
            // Note: we could actually use the same Xslt30Transformer in this case.
614
            // But in principle, the two transformations could be done in parallel in separate threads.
615

    
616
            String sourceFile1 = "data/books.xml";
617
            String sourceFile2 = "data/othello.xml";
618

    
619
            // Do the first transformation
620
            Console.WriteLine("\n\n----- transform of " + sourceFile1 + " -----");
621
            Xslt30Transformer transformer1 = templates.Load30();
622
            XdmNode inputNode = processor.NewDocumentBuilder().Build(new Uri(samplesDir, sourceFile1));
623
            transformer1.ApplyTemplates(inputNode, processor.NewSerializer(Console.Out));     // default destination is Console.Out
624

    
625
            // Do the second transformation
626
            Console.WriteLine("\n\n----- transform of " + sourceFile2 + " -----");
627
            Xslt30Transformer transformer2 = templates.Load30();
628
            XdmNode inputNode2 = processor.NewDocumentBuilder().Build(new Uri(samplesDir, sourceFile2));
629
            transformer2.ApplyTemplates(inputNode2, processor.NewSerializer(Console.Out));     // default destination is Console.Out    
630
        }
631
    }
632

    
633
    /// <summary>
634
    /// Show that the Xslt30Transformer is serially reusable; run a transformation twice using the same stylesheet
635
    /// and the same input document but with different parameters.
636
    /// </summary>
637

    
638
    public class XsltReuseTransformer : Example
639
    {
640

    
641
        public override String testName
642
        {
643
            get { return "XsltReuseTransformer"; }
644
        }
645

    
646
        public override void run(Uri samplesDir)
647
        {
648
            // Create a Processor instance.
649
            Processor processor = new Processor();
650

    
651
            // Load the source document, building a tree
652
            XdmNode input = processor.NewDocumentBuilder().Build(new Uri(samplesDir, "data/books.xml"));
653

    
654
            // Compile the stylesheet
655
            XsltExecutable exec = processor.NewXsltCompiler().Compile(new Uri(samplesDir, "styles/summarize.xsl"));
656

    
657
            // Create a transformer 
658
            Xslt30Transformer transformer = exec.Load30();
659

    
660
            // Run it once        
661
            Dictionary<QName, XdmValue> parameters = new Dictionary<QName, XdmValue>();
662
            parameters.Add(new QName("", "", "include-attributes"), new XdmAtomicValue(false));
663
            transformer.SetStylesheetParameters(parameters);
664
            XdmDestination results = new XdmDestination();
665
            transformer.ApplyTemplates(input, results);
666
            Console.WriteLine("1: " + results.XdmNode.OuterXml);
667

    
668
            // Run it again        
669
            Dictionary<QName, XdmValue> parameters2 = new Dictionary<QName, XdmValue>();
670
            parameters.Add(new QName("", "", "include-attributes"), new XdmAtomicValue(true));
671
            // Create a transformer 
672
            Xslt30Transformer transformer2 = exec.Load30();
673

    
674
            transformer2.SetStylesheetParameters(parameters2);
675
            results.Reset();
676
            transformer2.ApplyTemplates(input, results);
677
            Console.WriteLine("2: " + results.XdmNode.OuterXml);
678
        }
679
    }
680

    
681
    /// <summary>
682
    /// Run a sequence of transformations in a pipeline, each one acting as a filter
683
    /// </summary>
684

    
685
    public class XsltFilterChain : Example
686
    {
687

    
688
        public override String testName
689
        {
690
            get { return "XsltFilterChain"; }
691
        }
692

    
693
        public override void run(Uri samplesDir)
694
        {
695
            // Create a Processor instance.
696
            Processor processor = new Processor();
697

    
698
            // Load the source document
699
            XdmNode input = processor.NewDocumentBuilder().Build(new Uri(samplesDir, "data/books.xml"));
700

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

    
704
            // Compile all three stylesheets
705
            Xslt30Transformer transformer1 = compiler.Compile(new Uri(samplesDir, "styles/identity.xsl")).Load30();
706
            Xslt30Transformer transformer2 = compiler.Compile(new Uri(samplesDir, "styles/books.xsl")).Load30();
707
            Xslt30Transformer transformer3 = compiler.Compile(new Uri(samplesDir, "styles/summarize.xsl")).Load30();
708

    
709
            // Now run them in series
710
            XdmDestination results1 = new XdmDestination();
711
            transformer1.ApplyTemplates(input, results1);
712
            //Console.WriteLine("After phase 1:");
713
            //Console.WriteLine(results1.XdmNode.OuterXml);
714
            
715
            XdmDestination results2 = new XdmDestination();
716
            transformer2.ApplyTemplates(results1.XdmNode, results2);
717
            //Console.WriteLine("After phase 2:");
718
            //Console.WriteLine(results2.XdmNode.OuterXml);
719

    
720
            XdmDestination results3 = new XdmDestination();
721
            transformer3.ApplyTemplates(results2.XdmNode, results3);
722
            Console.WriteLine("After phase 3:");
723
            Console.WriteLine(results3.XdmNode.OuterXml);
724
        }
725
    }
726

    
727
    /// <summary>
728
    /// Transform from an XDM tree to an XDM tree
729
    /// </summary>
730

    
731
    public class XsltXdmToXdm : Example
732
    {
733

    
734
        public override String testName
735
        {
736
            get { return "XsltXdmToXdm"; }
737
        }
738

    
739
        public override void run(Uri samplesDir)
740
        {
741
            // Create a Processor instance.
742
            Processor processor = new Processor();
743

    
744
            // Load the source document
745
            XdmNode input = processor.NewDocumentBuilder().Build(new Uri(samplesDir, "data/books.xml"));
746

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

    
750
            // Compile the stylesheet
751
            Xslt30Transformer transformer = compiler.Compile(new Uri(samplesDir, "styles/summarize.xsl")).Load30();
752

    
753
            // Run the transformation
754
            XdmDestination result = new XdmDestination();
755
            transformer.ApplyTemplates(input, result);
756

    
757
            // Serialize the result so we can see that it worked
758
            StringWriter sw = new StringWriter();
759
            result.XdmNode.WriteTo(new XmlTextWriter(sw));
760
            Console.WriteLine(sw.ToString());
761

    
762
            // Note: we don't do 
763
            //   result.XdmNode.WriteTo(new XmlTextWriter(Console.Out));
764
            // because that results in the Console.out stream being closed, 
765
            // with subsequent attempts to write to it being rejected.
766
        }
767
    }
768

    
769
    /// <summary>
770
    /// Run an XSLT transformation from an Xdm tree, starting at a node that is not the document node
771
    /// </summary>
772

    
773
    public class XsltXdmElementToXdm : Example
774
    {
775

    
776
        public override String testName
777
        {
778
            get { return "XsltXdmElementToXdm"; }
779
        }
780

    
781
        public override void run(Uri samplesDir)
782
        {
783
            // Create a Processor instance.
784
            Processor processor = new Processor();
785

    
786
            // Load the source document
787
            XdmNode input = processor.NewDocumentBuilder().Build(new Uri(samplesDir, "data/othello.xml"));
788

    
789
            // Navigate to the first grandchild
790
            XPathSelector eval = processor.NewXPathCompiler().Compile("/PLAY/FM[1]").Load();
791
            eval.ContextItem = input;
792
            input = (XdmNode)eval.EvaluateSingle();
793

    
794
            // Create an XSLT compiler
795
            XsltCompiler compiler = processor.NewXsltCompiler();
796

    
797
            // Compile the stylesheet
798
            Xslt30Transformer transformer = compiler.Compile(new Uri(samplesDir, "styles/summarize.xsl")).Load30();
799

    
800
            // Run the transformation
801
            XdmDestination result = new XdmDestination();
802
            transformer.ApplyTemplates(input, result);
803

    
804
            // Serialize the result so we can see that it worked
805
            Console.WriteLine(result.XdmNode.OuterXml);
806
        }
807
    }
808

    
809
    /// <summary>
810
    /// Run a transformation from a DOM (System.Xml.Document) input to a DOM output
811
    /// </summary>
812

    
813
    public class XsltDomToDom : Example
814
    {
815

    
816
        public override String testName
817
        {
818
            get { return "XsltDomToDom"; }
819
        }
820

    
821
        public override void run(Uri samplesDir)
822
        {
823
            // Create a Processor instance.
824
            Processor processor = new Processor();
825

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

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

    
834
            // Compile the stylesheet
835
            Xslt30Transformer transformer = compiler.Compile(new Uri(samplesDir, "styles/summarize.xsl")).Load30();
836

    
837
            // Run the transformation
838
            DomDestination result = new DomDestination();
839
            transformer.ApplyTemplates(input, result);
840

    
841
            // Serialize the result so we can see that it worked
842
            Console.WriteLine(result.XmlDocument.OuterXml);
843
        }
844
    }
845

    
846

    
847
    /// <summary>
848
    /// Run a transformation driven by an xml-stylesheet processing instruction in the source document
849
    /// </summary>
850

    
851
    public class XsltProcessingInstruction : Example
852
    {
853

    
854
        public override string testName
855
        {
856
            get { return "XsltProcessingInstruction"; }
857
        }
858

    
859
        public override void run(Uri samplesDir)
860
        {
861
            // Create a Processor instance.
862
            Processor processor = new Processor();
863
            XsltExecutable exec;
864

    
865
            // Load the source document
866
            XdmNode input = processor.NewDocumentBuilder().Build(new Uri(samplesDir, "data/books.xml"));
867
            //Console.WriteLine("=============== source document ===============");
868
            //Console.WriteLine(input.OuterXml);
869
            //Console.WriteLine("=========== end of source document ============");
870

    
871
            // Navigate to the xml-stylesheet processing instruction having the pseudo-attribute type=text/xsl;
872
            // then extract the value of the href pseudo-attribute if present
873

    
874
            String path = @"/processing-instruction(xml-stylesheet)[matches(.,'type\s*=\s*[''""]text/xsl[''""]')]" +
875
                    @"/replace(., '.*?href\s*=\s*[''""](.*?)[''""].*', '$1')";
876

    
877
            XPathSelector eval = processor.NewXPathCompiler().Compile(path).Load();
878
            eval.ContextItem = input;
879
            XdmAtomicValue hrefval = (XdmAtomicValue)eval.EvaluateSingle();
880
            String href = (hrefval == null ? null : hrefval.ToString());
881

    
882
            if (href == null || href == "")
883
            {
884
                Console.WriteLine("No suitable xml-stylesheet processing instruction found");
885
                return;
886

    
887
            }
888
            else if (href[0] == '#')
889
            {
890

    
891
                // The stylesheet is embedded in the source document and identified by a URI of the form "#id"
892

    
893
                Console.WriteLine("Locating embedded stylesheet with href = " + href);
894
                String idpath = "id('" + href.Substring(1) + "')";
895
                eval = processor.NewXPathCompiler().Compile(idpath).Load();
896
                eval.ContextItem = input;
897
                XdmNode node = (XdmNode)eval.EvaluateSingle();
898
                if (node == null)
899
                {
900
                    Console.WriteLine("No element found with ID " + href.Substring(1));
901
                    return;
902
                }
903
                exec = processor.NewXsltCompiler().Compile(node);
904

    
905
            }
906
            else
907
            {
908

    
909
                // The stylesheet is in an external document
910

    
911
                Console.WriteLine("Locating stylesheet at uri = " + new Uri(input.BaseUri, href));
912

    
913
                // Fetch and compile the referenced stylesheet
914
                exec = processor.NewXsltCompiler().Compile(new Uri(input.BaseUri, href.ToString()));
915
            }
916

    
917
            // Create a transformer 
918
            Xslt30Transformer transformer = exec.Load30();
919

    
920
            // Run it       
921
            XdmDestination results = new XdmDestination();
922
            transformer.ApplyTemplates(input, results);
923
            Console.WriteLine(results.XdmNode.OuterXml);
924

    
925
        }
926
    }
927

    
928
    /// <summary>
929
    /// Run an XSLT transformation setting serialization properties from the calling application
930
    /// </summary>
931

    
932
    public class XsltSettingOutputProperties : Example
933
    {
934

    
935
        public override string testName
936
        {
937
            get { return "XsltSettingOutputProperties"; }
938
        }
939

    
940
        public override void run(Uri samplesDir)
941
        {
942
            // Create a Processor instance.
943
            Processor processor = new Processor();
944

    
945
            // Load the source document
946
            XdmNode input = processor.NewDocumentBuilder().Build(new Uri(samplesDir, "data/books.xml"));
947

    
948
            // Create a transformer for the stylesheet.
949
            Xslt30Transformer transformer = processor.NewXsltCompiler().Compile(new Uri(samplesDir, "styles/summarize.xsl")).Load30();
950

    
951
            // Create a serializer, with output to the standard output stream
952
            Serializer serializer = processor.NewSerializer();
953
            serializer.SetOutputProperty(Serializer.METHOD, "xml");
954
            serializer.SetOutputProperty(Serializer.INDENT, "no");
955
            serializer.SetOutputWriter(Console.Out);
956

    
957
            // Transform the source XML and serialize the result document
958
            transformer.ApplyTemplates(input, serializer);
959
        }
960

    
961
    }
962

    
963
    /// <summary>
964
    /// Run an XSLT transformation making use of an XmlResolver to resolve URIs at document build time, at stylesheet compile time 
965
    /// and at transformation run-time
966
    /// </summary>
967

    
968
    public class XsltUsingSourceResolver : Example
969
    {
970

    
971
        public override string testName
972
        {
973
            get { return "XsltUsingSourceResolver"; }
974
        }
975

    
976
        public override void run(Uri samplesDir)
977
        {
978

    
979
            // Create a Processor instance.
980
            Processor processor = new Processor();
981

    
982
            // Load the source document
983
            DocumentBuilder builder = processor.NewDocumentBuilder();
984
            UserXmlResolver buildTimeResolver = new UserXmlResolver();
985
            buildTimeResolver.Message = "** Calling build-time XmlResolver: ";
986
            builder.XmlResolver = buildTimeResolver;
987
            builder.BaseUri = samplesDir;
988

    
989
            String doc = "<!DOCTYPE doc [<!ENTITY e SYSTEM 'flamingo.txt'>]><doc>&e;</doc>";
990
            MemoryStream ms = new MemoryStream();
991
            StreamWriter tw = new StreamWriter(ms);
992
            tw.Write(doc);
993
            tw.Flush();
994
            Stream instr = new MemoryStream(ms.GetBuffer(), 0, (int)ms.Length);
995
            XdmNode input = builder.Build(instr);
996

    
997
            // Create a transformer for the stylesheet.
998
            String stylesheet =
999
                "<xsl:stylesheet xmlns:xsl='http://www.w3.org/1999/XSL/Transform' version='2.0'>" +
1000
                "<xsl:import href='empty.xslt'/>" +
1001
                "<xsl:template match='/'>" +
1002
                "<out note=\"{doc('heron.txt')}\" ><xsl:copy-of select='.'/></out>" +
1003
                "</xsl:template>" +
1004
                "</xsl:stylesheet>";
1005

    
1006
            XsltCompiler compiler = processor.NewXsltCompiler();
1007
            UserXmlResolver compileTimeResolver = new UserXmlResolver();
1008
            compileTimeResolver.Message = "** Calling compile-time XmlResolver: ";
1009
            compiler.XmlResolver = compileTimeResolver;
1010
            compiler.BaseUri = samplesDir;
1011
            Xslt30Transformer transformer = compiler.Compile(new XmlTextReader(new StringReader(stylesheet))).Load30();
1012

    
1013
            // Set the user-written XmlResolver
1014
            UserXmlResolver runTimeResolver = new UserXmlResolver();
1015
            runTimeResolver.Message = "** Calling transformation-time XmlResolver: ";
1016
            transformer.InputXmlResolver = runTimeResolver;
1017

    
1018
            // Create a serializer
1019
            Serializer serializer = processor.NewSerializer();
1020
            serializer.SetOutputWriter(Console.Out);
1021

    
1022
			// Transform the source XML and serialize the result document
1023
            transformer.ApplyTemplates(input, serializer);
1024

    
1025
        }
1026
    }
1027

    
1028
    /// <summary>
1029
    /// Run an XSLT transformation displaying compile-time errors to the console
1030
    /// </summary>
1031

    
1032
    public class XsltDisplayingErrors : Example
1033
    {
1034

    
1035
        public override string testName
1036
        {
1037
            get { return "XsltDisplayingErrors"; }
1038
        }
1039

    
1040
        public override void run(Uri samplesDir)
1041
        {
1042
            // Create a Processor instance.
1043
            Processor processor = new Processor();
1044

    
1045
            // Create the XSLT Compiler
1046
            XsltCompiler compiler = processor.NewXsltCompiler();
1047

    
1048

    
1049
            // Define a stylesheet containing errors
1050
            String stylesheet =
1051
                "<xsl:stylesheet xmlns:xsl='http://www.w3.org/1999/XSL/Transform' version='1.0'>\n" +
1052
                "<xsl:template name='eee:template'>\n" +
1053
                "  <xsl:value-of select='32'/>\n" +
1054
                "</xsl:template>\n" +
1055
                "<xsl:template name='main'>\n" +
1056
                "  <xsl:value-of select='$var'/>\n" +
1057
                "</xsl:template>\n" +
1058
                "</xsl:stylesheet>";
1059

    
1060

    
1061
            // Attempt to compile the stylesheet and display the errors
1062
            try
1063
            {
1064
                compiler.BaseUri = new Uri("http://localhost/stylesheet");
1065
                compiler.Compile(new XmlTextReader(new StringReader(stylesheet)));
1066
                Console.WriteLine("Stylesheet compilation succeeded");
1067
            }
1068
            catch (Exception)
1069
            {
1070
                Console.WriteLine("Stylesheet compilation failed");
1071
            }
1072

    
1073

    
1074
        }
1075
    }
1076

    
1077
    /// <summary>
1078
    /// Run an XSLT transformation capturing compile-time errors within the application
1079
    /// </summary>
1080

    
1081
    public class XsltCapturingErrors : Example
1082
    {
1083

    
1084
        public override string testName
1085
        {
1086
            get { return "XsltCapturingErrors"; }
1087
        }
1088

    
1089
        public override void run(Uri samplesDir)
1090
        {
1091
            // Create a Processor instance.
1092
            Processor processor = new Processor();
1093

    
1094
            // Create the XSLT Compiler
1095
            XsltCompiler compiler = processor.NewXsltCompiler();
1096

    
1097
            // Create a list to hold the error information
1098
            compiler.ErrorList = new List<StaticError>();
1099

    
1100
            // Define a stylesheet containing errors
1101
            String stylesheet =
1102
                "<xsl:stylesheet xmlns:xsl='http://www.w3.org/1999/XSL/Transform' version='1.0'>\n" +
1103
                "<xsl:template name='fff:template'>\n" +
1104
                "  <xsl:value-of select='32'/>\n" +
1105
                "</xsl:template>\n" +
1106
                "<xsl:template name='main'>\n" +
1107
                "  <xsl:value-of select='$var'/>\n" +
1108
                "</xsl:template>\n" +
1109
                "</xsl:stylesheet>";
1110

    
1111

    
1112
            // Attempt to compile the stylesheet and display the errors
1113
            try
1114
            {
1115
                compiler.BaseUri = new Uri("http://localhost/stylesheet");
1116
                compiler.Compile(new StringReader(stylesheet));
1117
                Console.WriteLine("Stylesheet compilation succeeded");
1118
            }
1119
            catch (Exception)
1120
            {
1121
                Console.WriteLine("Stylesheet compilation failed with " + compiler.ErrorList.Count + " errors");
1122
                foreach (StaticError error in compiler.ErrorList)
1123
                {
1124
                    Console.WriteLine("At line " + error.LineNumber + ": " + error.Message);
1125
                }
1126
            }
1127
        }
1128
    }
1129

    
1130
    /// <summary>
1131
    /// Run an XSLT transformation capturing run-time messages within the application
1132
    /// </summary>
1133

    
1134
    public class XsltCapturingMessages : Example
1135
    {
1136

    
1137
        public override string testName
1138
        {
1139
            get { return "XsltCapturingMessages"; }
1140
        }
1141

    
1142
        public override void run(Uri samplesDir)
1143
        {
1144

    
1145
            // Create a Processor instance.
1146
            Processor processor = new Processor();
1147

    
1148
            // Create the XSLT Compiler
1149
            XsltCompiler compiler = processor.NewXsltCompiler();
1150

    
1151
            // Define a stylesheet that generates messages
1152
            String stylesheet =
1153
                "<xsl:stylesheet xmlns:xsl='http://www.w3.org/1999/XSL/Transform' version='2.0'>\n" +
1154
                "<xsl:template name='main'>\n" +
1155
                "  <xsl:message><a>starting</a></xsl:message>\n" +
1156
                "  <out><xsl:value-of select='current-date()'/></out>\n" +
1157
                "  <xsl:message><a>finishing</a></xsl:message>\n" +
1158
                "</xsl:template>\n" +
1159
                "</xsl:stylesheet>";
1160

    
1161
            compiler.BaseUri = new Uri("http://localhost/stylesheet");
1162
            XsltExecutable exec = compiler.Compile(new StringReader(stylesheet));
1163

    
1164

    
1165
            // Create a transformer for the stylesheet.
1166
            Xslt30Transformer transformer = exec.Load30();
1167

    
1168

    
1169
            // Create a Listener to which messages will be written
1170
            transformer.MessageListener = new UserMessageListener();
1171

    
1172
			// Create a serializer, with output to the standard output stream
1173
            Serializer serializer = processor.NewSerializer();
1174
            serializer.SetOutputWriter(Console.Out);
1175

    
1176
			// Transform the source XML and serialize the result document
1177
            transformer.CallTemplate(new QName("", "main"), serializer);
1178
        }
1179

    
1180
    }
1181

    
1182
    ///
1183
    /// Example user-written message listener
1184
    ///
1185

    
1186
    public class UserMessageListener : IMessageListener
1187
    {
1188

    
1189
        public void Message(XdmNode content, bool terminate, IXmlLocation location)
1190
        {
1191
            Console.Out.WriteLine("MESSAGE terminate=" + (terminate ? "yes" : "no") + " at " + DateTime.Now);
1192
            Console.Out.WriteLine("From instruction at line " + location.LineNumber +
1193
                    " of " + location.BaseUri);
1194
            Console.Out.WriteLine(">>" + content.StringValue);
1195
        }
1196
    }
1197

    
1198

    
1199
    /// <summary>
1200
    /// Run an XSLT transformation producing multiple output documents
1201
    /// </summary>
1202

    
1203
    public class XsltMultipleOutput : Example
1204
    {
1205

    
1206
        public override string testName
1207
        {
1208
            get { return "XsltMultipleOutput"; }
1209
        }
1210

    
1211
        public override void run(Uri samplesDir)
1212
        {
1213
            // Create a Processor instance.
1214
            Processor processor = new Processor();
1215
            processor.SetProperty("http://saxon.sf.net/feature/timing", "true");
1216

    
1217
            // Load the source document
1218
            XdmNode input = processor.NewDocumentBuilder().Build(new Uri(samplesDir, "data/othello.xml"));
1219

    
1220
            // Create a transformer for the stylesheet.
1221
            Xslt30Transformer transformer = processor.NewXsltCompiler().Compile(new Uri(samplesDir, "styles/play.xsl")).Load30();
1222

    
1223
            // Set the required stylesheet parameter
1224
            Dictionary<QName, XdmValue> parameters = new Dictionary<QName, XdmValue>();
1225
            parameters.Add(new QName("", "", "dir"), new XdmAtomicValue(samplesDir.ToString() + "play"));
1226
            transformer.SetStylesheetParameters(parameters);
1227

    
1228
			// Create a serializer, with output to the standard output stream
1229
            Serializer serializer = processor.NewSerializer();
1230
            serializer.SetOutputWriter(Console.Out);
1231

    
1232
			// Transform the source XML and serialize the result document
1233
            transformer.ApplyTemplates(input, serializer);
1234

    
1235
        }
1236

    
1237
    }
1238

    
1239

    
1240
    /// <summary>
1241
    /// Run an XSLT transformation using the id() function, with DTD validation
1242
    /// </summary>
1243

    
1244
    public class XsltUsingIdFunction : Example
1245
    {
1246

    
1247
        public override string testName
1248
        {
1249
            get { return "XsltUsingIdFunction"; }
1250
        }
1251

    
1252
        public override void run(Uri samplesDir)
1253
        {
1254
            // Create a Processor instance
1255
            Processor processor = new Processor();
1256

    
1257
            // Load the source document. The Microsoft .NET parser does not report attributes of type ID. The only
1258
            // way to use the function is therefore (a) to use a different parser, or (b) to use xml:id instead. We
1259
            // choose the latter course.
1260

    
1261
            String doc = "<!DOCTYPE table [" +
1262
                "<!ELEMENT table (row*)>" +
1263
                "<!ELEMENT row EMPTY>" +
1264
                "<!ATTLIST row xml:id ID #REQUIRED>" +
1265
                "<!ATTLIST row value CDATA #REQUIRED>]>" +
1266
                "<table><row xml:id='A123' value='green'/><row xml:id='Z789' value='blue'/></table>";
1267

    
1268
            DocumentBuilder builder = processor.NewDocumentBuilder();
1269
            builder.DtdValidation = true;
1270
            builder.BaseUri = samplesDir;
1271
            MemoryStream ms = new MemoryStream();
1272
            StreamWriter tw = new StreamWriter(ms);
1273
            tw.Write(doc);
1274
            tw.Flush();
1275
            Stream instr = new MemoryStream(ms.GetBuffer(), 0, (int)ms.Length);
1276
            XdmNode input = builder.Build(instr);
1277

    
1278
            // Define a stylesheet that uses the id() function
1279
            String stylesheet =
1280
                "<xsl:stylesheet xmlns:xsl='http://www.w3.org/1999/XSL/Transform' version='2.0'>\n" +
1281
                "<xsl:template match='/'>\n" +
1282
                "  <xsl:copy-of select=\"id('Z789')\"/>\n" +
1283
                "</xsl:template>\n" +
1284
                "</xsl:stylesheet>";
1285

    
1286
            XsltCompiler compiler = processor.NewXsltCompiler();
1287
            compiler.BaseUri = new Uri("http://localhost/stylesheet");
1288
            XsltExecutable exec = compiler.Compile(new StringReader(stylesheet));
1289

    
1290
            // Create a transformer for the stylesheet
1291
            Xslt30Transformer transformer = exec.Load30();
1292

    
1293
            // Set the destination
1294
            XdmDestination results = new XdmDestination();
1295

    
1296
            // Transform the XML
1297
            transformer.ApplyTemplates(input, results);
1298

    
1299
            // Show the result
1300
            Console.WriteLine(results.XdmNode.ToString());
1301

    
1302
        }
1303

    
1304
    }
1305

    
1306
    /// <summary>
1307
    /// Show a transformation using a user-written result document handler. This example
1308
    /// captures each of the result documents in a DOM, and creates a Hashtable that indexes
1309
    /// the DOM trees according to their absolute URI. On completion, it writes all the DOMs
1310
    /// to the standard output.
1311
    /// </summary>
1312

    
1313
    public class XsltUsingResultHandler : Example
1314
    {
1315

    
1316
        public override string testName
1317
        {
1318
            get { return "XsltUsingResultHandler"; }
1319
        }
1320

    
1321
        public override void run(Uri samplesDir)
1322
        {
1323
            // Create a Processor instance.
1324
            Processor processor = new Processor();
1325

    
1326
            // Load the source document
1327
            XdmNode input = processor.NewDocumentBuilder().Build(new Uri(samplesDir, "data/othello.xml"));
1328

    
1329
            // Define a stylesheet that splits the document up
1330
            String stylesheet =
1331
                "<xsl:stylesheet xmlns:xsl='http://www.w3.org/1999/XSL/Transform' version='2.0'>\n" +
1332
                "<xsl:template match='/'>\n" +
1333
                "  <xsl:for-each select='//ACT'>\n" +
1334
                "    <xsl:result-document href='{position()}.xml'>\n" +
1335
                "      <xsl:copy-of select='TITLE'/>\n" +
1336
                "    </xsl:result-document>\n" +
1337
                "  </xsl:for-each>\n" +
1338
                "</xsl:template>\n" +
1339
                "</xsl:stylesheet>";
1340

    
1341
            XsltCompiler compiler = processor.NewXsltCompiler();
1342
            compiler.BaseUri = new Uri("http://localhost/stylesheet");
1343
            XsltExecutable exec = compiler.Compile(new StringReader(stylesheet));
1344

    
1345
            // Create a transformer for the stylesheet.
1346
            Xslt30Transformer transformer = exec.Load30();
1347

    
1348
            // Establish the result document handler
1349
            Hashtable results = new Hashtable();
1350
            transformer.ResultDocumentHandler = new UserResultDocumentHandler(results);
1351

    
1352
            // Transform the source XML to a NullDestination (because we only want the secondary result files).
1353
            NullDestination destination = new NullDestination();
1354
            destination.BaseUri = samplesDir;
1355
            transformer.ApplyTemplates(input, destination);
1356

    
1357
            // Process the captured DOM results
1358
            foreach (DictionaryEntry entry in results)
1359
            {
1360
                string uri = (string)entry.Key;
1361
                Console.WriteLine("\nResult File " + uri);
1362
                DomDestination dom = (DomDestination)results[uri];
1363
                Console.Write(dom.XmlDocument.OuterXml);
1364
            }
1365

    
1366
        }
1367

    
1368
    }
1369

    
1370
    /// <summary>
1371
    /// Show a transformation using a registered collection
1372
    /// </summary>
1373

    
1374
    public class XsltUsingRegisteredCollection : Example
1375
    {
1376

    
1377
        public override string testName
1378
        {
1379
            get { return "XsltUsingRegisteredCollection"; }
1380
        }
1381

    
1382
        public override void run(Uri samplesDir)
1383
        {
1384
            // Create a Processor instance.
1385
            Processor processor = new Processor();
1386

    
1387
			// Define a stylesheet that uses the collection() function
1388
            String stylesheet =
1389
                "<xsl:stylesheet xmlns:xsl='http://www.w3.org/1999/XSL/Transform' version='2.0'>\n" +
1390
                "<xsl:template name='main'>\n" +
1391
                " <out>\n" +
1392
                "  <xsl:for-each select=\"collection('http://www.example.org/my-collection')\">\n" +
1393
                "    <document uri='{document-uri(.)}' nodes='{count(//*)}'/>\n" +
1394
                "  </xsl:for-each><zzz/>\n" +
1395
                "  <xsl:for-each select=\"collection('http://www.example.org/my-collection')\">\n" +
1396
                "    <document uri='{document-uri(.)}' nodes='{count(//*)}'/>\n" +
1397
                "  </xsl:for-each>\n" +
1398
                " </out>\n" +
1399
                "</xsl:template>\n" +
1400
                "</xsl:stylesheet>";
1401

    
1402
			// Register a named collection
1403
            Uri[] documentList = new Uri[2];
1404
            documentList[0] = new Uri(samplesDir, "data/othello.xml");
1405
            documentList[1] = new Uri(samplesDir, "data/books.xml");
1406
            processor.RegisterCollection(new Uri("http://www.example.org/my-collection"), documentList);
1407

    
1408
            XsltCompiler compiler = processor.NewXsltCompiler();
1409
            compiler.BaseUri = new Uri("http://localhost/stylesheet");
1410
            XsltExecutable exec = compiler.Compile(new StringReader(stylesheet));
1411

    
1412
            // Create a transformer for the stylesheet.
1413
            Xslt30Transformer transformer = exec.Load30();
1414

    
1415
            // Set the destination
1416
            XdmDestination results = new XdmDestination();
1417

    
1418
            // Transform the XML
1419
            transformer.CallTemplate(new QName("", "main"), results);
1420

    
1421
            // Show the result
1422
            Console.WriteLine(results.XdmNode.ToString());
1423

    
1424
        }
1425
    }
1426

    
1427
    /// <summary>
1428
	/// Show a transformation using a collection that maps to a directory
1429
    /// </summary>
1430

    
1431
    public class XsltUsingDirectoryCollection : Example
1432
    {
1433

    
1434
        public override string testName
1435
        {
1436
            get { return "XsltUsingDirectoryCollection"; }
1437
        }
1438

    
1439
        public override void run(Uri samplesDir)
1440
        {
1441
            // Create a Processor instance.
1442
            Processor processor = new Processor();
1443

    
1444
			// Define a stylesheet that uses the collection() function
1445
            String stylesheet =
1446
                "<xsl:stylesheet xmlns:xsl='http://www.w3.org/1999/XSL/Transform' version='2.0'>\n" +
1447
                "<xsl:template name='main'>\n" +
1448
                " <out>\n" +
1449
                "  <xsl:for-each select=\"collection('" + samplesDir + "?recurse=yes;select=*.xml;on-error=warning')\">\n" +
1450
                "    <document uri='{document-uri(.)}' nodes='{count(//*)}'/>\n" +
1451
                "  </xsl:for-each><zzz/>\n" +
1452
                " </out>\n" +
1453
                "</xsl:template>\n" +
1454
                "</xsl:stylesheet>";
1455

    
1456

    
1457
            XsltCompiler compiler = processor.NewXsltCompiler();
1458
            compiler.BaseUri = new Uri("http://localhost/stylesheet");
1459
            XsltExecutable exec = compiler.Compile(new StringReader(stylesheet));
1460

    
1461
            // Create a transformer for the stylesheet.
1462
            Xslt30Transformer transformer = exec.Load30();
1463
            
1464
            // Set the destination
1465
            XdmDestination results = new XdmDestination();
1466

    
1467
            // Transform the XML
1468
            transformer.CallTemplate(new QName("", "main"), results);
1469

    
1470
            // Show the result
1471
            Console.WriteLine(results.XdmNode.ToString());
1472

    
1473
        }
1474

    
1475
    }
1476

    
1477

    
1478

    
1479
    /// <summary>
1480
    /// Show a transformation using calls to extension functions
1481
    /// </summary>
1482

    
1483
    public class XsltIntegratedExtension : Example
1484
    {
1485

    
1486
        public override string testName
1487
        {
1488
            get { return "XsltIntegratedExtension"; }
1489
        }
1490

    
1491
        public override void run(Uri samplesDir)
1492
        {
1493

    
1494
            // Create a Processor instance.
1495
            Processor processor = new Processor();
1496

    
1497
            // Identify the Processor version
1498
            Console.WriteLine(processor.ProductVersion);
1499

    
1500
            // Set diagnostics
1501
            //processor.SetProperty("http://saxon.sf.net/feature/trace-external-functions", "true");
1502

    
1503
            // Create the stylesheet
1504
            String s = @"<xsl:transform version='2.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform'" +
1505
                @" xmlns:math='http://example.math.co.uk/demo'> " +
1506
                @" <xsl:template name='go'> " +
1507
                @" <out sqrt2='{math:sqrt(2.0e0)}' " +
1508
                @" sqrtEmpty='{math:sqrt(())}'/> " +
1509
                @" </xsl:template></xsl:transform>";
1510

    
1511
            // Register the integrated extension function math:sqrt
1512
            processor.RegisterExtensionFunction(new Sqrt());
1513

    
1514
            // Create a transformer for the stylesheet.
1515
            Xslt30Transformer transformer = processor.NewXsltCompiler().Compile(new StringReader(s)).Load30();
1516

    
1517
			// Create a serializer, with output to the standard output stream
1518
            Serializer serializer = processor.NewSerializer();
1519
            serializer.SetOutputWriter(Console.Out);
1520
            serializer.SetOutputProperty(Serializer.INDENT, "yes");
1521

    
1522
			// Transform the source XML and serialize the result document
1523
            transformer.CallTemplate(new QName("go"), serializer);
1524
        }
1525

    
1526
    }
1527

    
1528
    /// <summary>
1529
    /// Example extension function to compute a square root.
1530
    /// </summary>
1531

    
1532
    public class Sqrt : ExtensionFunctionDefinition
1533
    {
1534
        public override QName FunctionName
1535
        {
1536
            get
1537
            {
1538
                return new QName("http://example.math.co.uk/demo", "sqrt");
1539
            }
1540
        }
1541

    
1542
        public override int MinimumNumberOfArguments
1543
        {
1544
            get
1545
            {
1546
                return 1;
1547
            }
1548
        }
1549

    
1550
        public override int MaximumNumberOfArguments
1551
        {
1552
            get
1553
            {
1554
                return 1;
1555
            }
1556
        }
1557

    
1558
        public override XdmSequenceType[] ArgumentTypes
1559
        {
1560
            get
1561
            {
1562
                return new XdmSequenceType[]{
1563
                    new XdmSequenceType(XdmAtomicType.BuiltInAtomicType(QName.XS_DOUBLE), '?')
1564
                };
1565
            }
1566
        }
1567

    
1568
        public override XdmSequenceType ResultType(XdmSequenceType[] ArgumentTypes)
1569
        {
1570
            return new XdmSequenceType(XdmAtomicType.BuiltInAtomicType(QName.XS_DOUBLE), '?');
1571
        }
1572

    
1573
        public override bool TrustResultType
1574
        {
1575
            get
1576
            {
1577
                return true;
1578
            }
1579
        }
1580

    
1581

    
1582
        public override ExtensionFunctionCall MakeFunctionCall()
1583
        {
1584
            return new SqrtCall();
1585
        }
1586
    }
1587

    
1588
    internal class SqrtCall : ExtensionFunctionCall
1589
    {
1590

    
1591
        public override IXdmEnumerator<XdmItem> Call(IXdmEnumerator<XdmItem>[] arguments, DynamicContext context)
1592
        {
1593
            Boolean exists = arguments[0].MoveNext();
1594
            if (exists)
1595
            {
1596
                XdmAtomicValue arg = (XdmAtomicValue)arguments[0].Current;
1597
                double val = (double)arg.Value;
1598
                double sqrt = System.Math.Sqrt(val);
1599
                XdmAtomicValue result = new XdmAtomicValue(sqrt);
1600
                return (IXdmEnumerator<XdmItem>)((IXdmEnumerable<XdmItem>)result).GetEnumerator();
1601
            }
1602
            else
1603
            {
1604
                return EmptyEnumerator<XdmItem>.INSTANCE;
1605
            }
1606
        }
1607
    }
1608

    
1609
    /// <summary>
1610
    /// Example extension function to return the default namespace from the static context
1611
    /// </summary>
1612

    
1613
    public class DefaultNamespace : ExtensionFunctionDefinition
1614
    {
1615
        public override QName FunctionName
1616
        {
1617
            get
1618
            {
1619
                return new QName("http://example.env.co.uk/demo", "defaultNamespace");
1620
            }
1621
        }
1622

    
1623
        public override int MinimumNumberOfArguments
1624
        {
1625
            get
1626
            {
1627
                return 0;
1628
            }
1629
        }
1630

    
1631
        public override int MaximumNumberOfArguments
1632
        {
1633
            get
1634
            {
1635
                return 0;
1636
            }
1637
        }
1638

    
1639
        public override XdmSequenceType[] ArgumentTypes
1640
        {
1641
            get
1642
            {
1643
                return new XdmSequenceType[] { };
1644
            }
1645
        }
1646

    
1647
        public override bool DependsOnFocus
1648
        {
1649
            get
1650
            {
1651
                return true;
1652
                // actually it depends on the static context rather than the focus; but returning true is necessary
1653
                // to avoid the call being extracted to a global variable.
1654
            }
1655
        }
1656

    
1657
        public override XdmSequenceType ResultType(XdmSequenceType[] ArgumentTypes)
1658
        {
1659
            return new XdmSequenceType(XdmAtomicType.BuiltInAtomicType(QName.XS_STRING), '?');
1660
        }
1661

    
1662
        public override bool TrustResultType
1663
        {
1664
            get
1665
            {
1666
                return true;
1667
            }
1668
        }
1669

    
1670

    
1671
        public override ExtensionFunctionCall MakeFunctionCall()
1672
        {
1673
            return new DefaultNamespaceCall();
1674
        }
1675
    }
1676

    
1677
    internal class DefaultNamespaceCall : ExtensionFunctionCall
1678
    {
1679
        private string defaultNamespace;
1680

    
1681
        public override void SupplyStaticContext(StaticContext context)
1682
        {
1683
            defaultNamespace = context.GetNamespaceForPrefix("");
1684
        }
1685

    
1686

    
1687
        public override IXdmEnumerator<XdmItem> Call(IXdmEnumerator<XdmItem>[] arguments, DynamicContext context)
1688
        {
1689
            if (defaultNamespace != null)
1690
            {
1691
                return (IXdmEnumerator<XdmItem>)new XdmAtomicValue(defaultNamespace);
1692
            }
1693
            else
1694
            {
1695
                return EmptyEnumerator<XdmItem>.INSTANCE;
1696
            }
1697
        }
1698
    }
1699

    
1700
    /// <summary>
1701
    /// Show a query producing a document as its result and serializing this to a FileStream
1702
    /// </summary>
1703

    
1704
    public class XQueryToStream : Example
1705
    {
1706

    
1707
        public override string testName
1708
        {
1709
            get { return "XQueryToStream"; }
1710
        }
1711

    
1712
        public override void run(Uri samplesDir)
1713
        {
1714
            Processor processor = new Processor();
1715
            XQueryCompiler compiler = processor.NewXQueryCompiler();
1716
            compiler.BaseUri = samplesDir.ToString();
1717
            compiler.DeclareNamespace("saxon", "http://saxon.sf.net/");
1718
            XQueryExecutable exp = compiler.Compile("<saxon:example>{static-base-uri()}</saxon:example>");
1719
            XQueryEvaluator eval = exp.Load();
1720
            Serializer qout = processor.NewSerializer();
1721
            qout.SetOutputProperty(Serializer.METHOD, "xml");
1722
            qout.SetOutputProperty(Serializer.INDENT, "yes");
1723
            qout.SetOutputStream(new FileStream("testoutput.xml", FileMode.Create, FileAccess.Write));
1724
            Console.WriteLine("Output written to testoutput.xml");
1725
            eval.Run(qout);
1726
        }
1727

    
1728
    }
1729

    
1730
    /// <summary>
1731
    /// Show a query producing a single atomic value as its result and returning the value
1732
    /// to the C# application
1733
    /// </summary>
1734

    
1735
    public class XQueryToAtomicValue : Example
1736
    {
1737

    
1738
        public override string testName
1739
        {
1740
            get { return "XQueryToAtomicValue"; }
1741
        }
1742

    
1743
        public override void run(Uri samplesDir)
1744
        {
1745
            Processor processor = new Processor();
1746
            XQueryCompiler compiler = processor.NewXQueryCompiler();
1747
            XQueryExecutable exp = compiler.Compile("avg(for $i in 1 to 10 return $i * $i)");
1748
            XQueryEvaluator eval = exp.Load();
1749
            XdmAtomicValue result = (XdmAtomicValue)eval.EvaluateSingle();
1750
            Console.WriteLine("Result type: " + result.Value.GetType());
1751
            Console.WriteLine("Result value: " + (decimal)result.Value);
1752
        }
1753

    
1754
	}
1755

    
1756
	/// <summary>
1757
	/// Show a query producing a sequence as its result and returning the sequence
1758
	/// to the C# application in the form of an iterator. For each item in the
1759
	/// result, its string value is output.
1760
	/// </summary>
1761

    
1762
	public class XQueryToSequence : Example
1763
	{
1764

    
1765
		public override string testName
1766
		{
1767
			get { return "XQueryToSequence"; }
1768
		}
1769

    
1770
		public override void run(Uri samplesDir)
1771
		{
1772
			Processor processor = new Processor();
1773
			XQueryCompiler compiler = processor.NewXQueryCompiler();
1774
			XQueryExecutable exp = compiler.Compile("for $i in 1 to 10 return $i * $i");
1775
			XQueryEvaluator eval = exp.Load();
1776
			XdmValue value = eval.Evaluate();
1777
            IXdmEnumerator<XdmItem> e = (IXdmEnumerator<XdmItem>)value;
1778
			while (e.MoveNext())
1779
			{
1780
				XdmItem item = e.Current;
1781
				Console.WriteLine(item.ToString());
1782
			}
1783

    
1784
		}
1785

    
1786
	}
1787

    
1788
    /// <summary>
1789
    /// Show a query producing a DOM as its input and producing a DOM as its output
1790
    /// </summary>
1791

    
1792
    public class XQueryToDom : Example
1793
    {
1794

    
1795
        public override string testName
1796
        {
1797
            get { return "XQueryToDom"; }
1798
        }
1799

    
1800
        public override void run(Uri samplesDir)
1801
        {
1802
            Processor processor = new Processor();
1803

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

    
1808
            XQueryCompiler compiler = processor.NewXQueryCompiler();
1809
            XQueryExecutable exp = compiler.Compile("<doc>{reverse(/*/*)}</doc>");
1810
            XQueryEvaluator eval = exp.Load();
1811
            eval.ContextItem = indoc;
1812
            DomDestination qout = new DomDestination();
1813
            eval.Run(qout);
1814
            XmlDocument outdoc = qout.XmlDocument;
1815
            Console.WriteLine(outdoc.OuterXml);
1816
        }
1817

    
1818
    }
1819

    
1820
    /// <summary>
1821
    /// Show a query producing a Saxon tree as its input and producing a Saxon tree as its output
1822
    /// </summary>
1823

    
1824
    public class XQueryToXdm : Example
1825
    {
1826

    
1827
        public override string testName
1828
        {
1829
            get { return "XQueryToXdm"; }
1830
        }
1831

    
1832
        public override void run(Uri samplesDir)
1833
        {
1834
            Processor processor = new Processor();
1835

    
1836
            DocumentBuilder loader = processor.NewDocumentBuilder();
1837
            loader.BaseUri = new Uri(samplesDir, "data/books.xml");
1838
            XdmNode indoc = loader.Build(loader.BaseUri);
1839

    
1840
            XQueryCompiler compiler = processor.NewXQueryCompiler();
1841
            XQueryExecutable exp = compiler.Compile("<doc>{reverse(/*/*)}</doc>");
1842
            XQueryEvaluator eval = exp.Load();
1843
            eval.ContextItem = indoc;
1844
            XdmDestination qout = new XdmDestination();
1845
            eval.Run(qout);
1846
            XdmNode outdoc = qout.XdmNode;
1847
            Console.WriteLine(outdoc.OuterXml);
1848
        }
1849

    
1850
    }
1851

    
1852
    /// <summary>
1853
    /// Show a query making a direct call to a user-defined function defined in the query
1854
    /// </summary>
1855

    
1856
    public class XQueryCallFunction : Example
1857
    {
1858

    
1859
        public override string testName
1860
        {
1861
            get { return "XQueryCallFunction"; }
1862
        }
1863

    
1864
        public override void run(Uri samplesDir)
1865
        {
1866
            Processor processor = new Processor();
1867

    
1868
            XQueryCompiler qc = processor.NewXQueryCompiler();
1869
            XQueryExecutable exp1 = qc.Compile("declare namespace f='f.ns';" +
1870
                   "declare variable $z := 1 + xs:integer(doc-available('" + new Uri(samplesDir, "data/books.xml").ToString() + "'));" +
1871
                   "declare variable $p as xs:integer external;" +
1872
                   "declare function f:t1($v1 as xs:integer) { " +
1873
                   "   $v1 div $z + $p" +
1874
                   "};" +
1875
                   "10");
1876
            XQueryEvaluator ev = exp1.Load();
1877
            ev.SetExternalVariable(new QName("", "p"), new XdmAtomicValue(39));
1878
            XdmValue v1 = new XdmAtomicValue(10);
1879
            XdmValue result = ev.CallFunction(new QName("f.ns", "f:t1"), new XdmValue[] { v1 });
1880
            Console.WriteLine("First result (expected 44): " + result.ToString());
1881
            v1 = new XdmAtomicValue(20);
1882
            result = ev.CallFunction(new QName("f.ns", "f:t1"), new XdmValue[] { v1 });
1883
            Console.WriteLine("Second result (expected 49): " + result.ToString());
1884
        }
1885

    
1886
    }
1887

    
1888

    
1889
    /// <summary>
1890
    /// Show a query reading an input document using an XmlReader (the .NET XML parser)
1891
    /// </summary>
1892

    
1893
    public class XQueryFromXmlReader : Example
1894
    {
1895

    
1896
        public override string testName
1897
        {
1898
            get { return "XQueryFromXmlReader"; }
1899
        }
1900

    
1901
        public override void run(Uri samplesDir)
1902
        {
1903
            Processor processor = new Processor();
1904

    
1905
            String inputFileName = new Uri(samplesDir, "data/books.xml").ToString();
1906
            XmlTextReader reader = new XmlTextReader(inputFileName,
1907
                UriConnection.getReadableUriStream(new Uri(samplesDir, "data/books.xml")));
1908
            //new FileStream(inputFileName, FileMode.Open, FileAccess.Read));
1909
            reader.Normalization = true;
1910

    
1911
            // add a validating reader - not to perform validation, but to expand entity references
1912
            XmlValidatingReader validator = new XmlValidatingReader(reader);
1913
            validator.ValidationType = ValidationType.None;
1914

    
1915
            XdmNode doc = processor.NewDocumentBuilder().Build(validator);
1916

    
1917
            XQueryCompiler compiler = processor.NewXQueryCompiler();
1918
            XQueryExecutable exp = compiler.Compile("/");
1919
            XQueryEvaluator eval = exp.Load();
1920
            eval.ContextItem = doc;
1921
            Serializer qout = processor.NewSerializer();
1922
            qout.SetOutputProperty(Serializer.METHOD, "xml");
1923
            qout.SetOutputProperty(Serializer.INDENT, "yes");
1924
			qout.SetOutputStream(new FileStream("testoutput2.xml", FileMode.Create, FileAccess.Write));
1925
			Console.WriteLine("Output written to testoutput2.xml");
1926
            eval.Run(qout);
1927
        }
1928

    
1929
    }
1930

    
1931
    /// <summary>
1932
    /// Show a query producing a sequence as its result and returning the sequence
1933
    /// to the C# application in the form of an iterator. The sequence is then
1934
    /// output by serializing each item individually, with each item on a new line.
1935
    /// </summary>
1936

    
1937
    public class XQueryToSerializedSequence : Example
1938
    {
1939

    
1940
        public override string testName
1941
        {
1942
            get { return "XQueryToSerializedSequence"; }
1943
        }
1944

    
1945
        public override void run(Uri samplesDir)
1946
        {
1947
            Processor processor = new Processor();
1948
            String inputFileName = new Uri(samplesDir, "data/books.xml").ToString();
1949
            //XmlTextReader reader = new XmlTextReader(inputFileName,
1950
            //    new FileStream(inputFileName, FileMode.Open, FileAccess.Read));
1951
            XmlTextReader reader = new XmlTextReader(inputFileName,
1952
                UriConnection.getReadableUriStream(new Uri(samplesDir, "data/books.xml")));
1953
            reader.Normalization = true;
1954

    
1955
            // add a validating reader - not to perform validation, but to expand entity references
1956
            XmlValidatingReader validator = new XmlValidatingReader(reader);
1957
            validator.ValidationType = ValidationType.None;
1958

    
1959
            XdmNode doc = processor.NewDocumentBuilder().Build(reader);
1960

    
1961
            XQueryCompiler compiler = processor.NewXQueryCompiler();
1962
            XQueryExecutable exp = compiler.Compile("//ISBN");
1963
            XQueryEvaluator eval = exp.Load();
1964
            eval.ContextItem = doc;
1965

    
1966
            foreach (XdmNode node in eval)
1967
            {
1968
                Console.WriteLine(node.OuterXml);
1969
            }
1970
        }
1971

    
1972
    }
1973

    
1974
    /// <summary>
1975
    /// Show a query that takes a parameter (external variable) as input.
1976
    /// The query produces a single atomic value as its result and returns the value
1977
    /// to the C# application. 
1978
    /// </summary>
1979

    
1980
    public class XQueryUsingParameter : Example
1981
    {
1982

    
1983
        public override string testName
1984
        {
1985
            get { return "XQueryUsingParameter"; }
1986
        }
1987

    
1988
        public override void run(Uri samplesDir)
1989
        {
1990
            Processor processor = new Processor();
1991
            XQueryCompiler compiler = processor.NewXQueryCompiler();
1992
            compiler.DeclareNamespace("p", "http://saxon.sf.net/ns/p");
1993
            XQueryExecutable exp = compiler.Compile(
1994
                    "declare variable $p:in as xs:integer external; $p:in * $p:in");
1995
            XQueryEvaluator eval = exp.Load();
1996
            eval.SetExternalVariable(new QName("http://saxon.sf.net/ns/p", "p:in"), new XdmAtomicValue(12));
1997
            XdmAtomicValue result = (XdmAtomicValue)eval.EvaluateSingle();
1998
            Console.WriteLine("Result type: " + result.Value.GetType());
1999
            Console.WriteLine("Result value: " + (long)result.Value);
2000
        }
2001

    
2002
    }
2003

    
2004
    /// <summary>
2005
    /// Show a query consisting of two modules, using a QueryResolver to resolve
2006
    /// the "import module" declaration
2007
    /// </summary>
2008

    
2009
    public class XQueryMultiModule : Example
2010
    {
2011

    
2012
        public override string testName
2013
        {
2014
            get { return "XQueryMultiModule"; }
2015
        }
2016

    
2017
        public override void run(Uri samplesDir)
2018
        {
2019

    
2020
            String mod1 = "import module namespace m2 = 'http://www.example.com/module2';" +
2021
                          "m2:square(3)";
2022

    
2023
            String mod2 = "module namespace m2 = 'http://www.example.com/module2';" +
2024
                          "declare function m2:square($p) { $p * $p };";
2025

    
2026
            Processor processor = new Processor();
2027
            XQueryCompiler compiler = processor.NewXQueryCompiler();
2028

    
2029
            InlineModuleResolver resolver = new InlineModuleResolver();
2030
            resolver.AddModule(new Uri("http://www.example.com/module2"), mod2);
2031
            compiler.QueryResolver = resolver;
2032
            XQueryExecutable exp = compiler.Compile(mod1);
2033
            XQueryEvaluator eval = exp.Load();
2034

    
2035
            XdmAtomicValue result = (XdmAtomicValue)eval.EvaluateSingle();
2036
            Console.WriteLine("Result type: " + result.Value.GetType());
2037
            Console.WriteLine("Result value: " + (long)result.Value);
2038
        }
2039

    
2040
        // A simple QueryResolver designed to show that the actual query
2041
        // text can come from anywhere: in this case, the resolver maintains
2042
        // a simple mapping of module URIs onto strings.
2043

    
2044
        public class InlineModuleResolver : IQueryResolver
2045
        {
2046

    
2047
            private Hashtable modules = new Hashtable();
2048

    
2049
            public void AddModule(Uri moduleName, String moduleText)
2050
            {
2051
                modules.Add(moduleName, moduleText);
2052
            }
2053

    
2054
            public Uri[] GetModules(String moduleUri, Uri baseUri, String[] locationHints)
2055
            {
2056
                Uri[] result = { new Uri(moduleUri) };
2057
                return result;
2058
            }
2059

    
2060
            public Object GetEntity(Uri absoluteUri)
2061
            {
2062
                return modules[absoluteUri];
2063
            }
2064
        }
2065

    
2066
    }
2067

    
2068

    
2069
    public class UriConnection
2070
    {
2071

    
2072
        // Get a stream for reading from a file:// URI
2073

    
2074
        public static Stream getReadableUriStream(Uri uri)
2075
        {
2076
            WebRequest request = (WebRequest)WebRequest.Create(uri);
2077
            return request.GetResponse().GetResponseStream();
2078
        }
2079

    
2080
        // Get a stream for writing to a file:// URI
2081

    
2082
        public static Stream getWritableUriStream(Uri uri)
2083
        {
2084
            FileWebRequest request = (FileWebRequest)WebRequest.CreateDefault(uri);
2085
            request.Method = "POST";
2086
            return request.GetRequestStream();
2087
        }
2088
    }
2089

    
2090
    ///
2091
    /// A sample XmlResolver. In the case of a URI ending with ".txt", it returns the
2092
    /// URI itself, wrapped as an XML document. In the case of the URI "empty.xslt", it returns an empty
2093
    /// stylesheet. In all other cases, it returns null, which has the effect of delegating
2094
    /// processing to the standard XmlResolver.
2095
    ///
2096

    
2097
    public class UserXmlResolver : XmlUrlResolver
2098
    {
2099

    
2100
        public String Message = null;
2101

    
2102
        public override object GetEntity(Uri absoluteUri, String role, Type ofObjectToReturn)
2103
        {
2104
            if (Message != null)
2105
            {
2106
                Console.WriteLine(Message + absoluteUri + " (role=" + role + ")");
2107
            }
2108

    
2109
            if (absoluteUri.ToString().EndsWith(".txt"))
2110
            {
2111
                MemoryStream ms = new MemoryStream();
2112
                StreamWriter tw = new StreamWriter(ms);
2113
                tw.Write("<uri>");
2114
                tw.Write(absoluteUri);
2115
                tw.Write("</uri>");
2116
                tw.Flush();
2117
                return new MemoryStream(ms.GetBuffer(), 0, (int)ms.Length);
2118
            }
2119
            if (absoluteUri.ToString().EndsWith("empty.xslt"))
2120
            {
2121
                String ss = "<transform xmlns='http://www.w3.org/1999/XSL/Transform' version='2.0'/>";
2122
                MemoryStream ms = new MemoryStream();
2123
                StreamWriter tw = new StreamWriter(ms);
2124
                tw.Write(ss);
2125
                tw.Flush();
2126
                return new MemoryStream(ms.GetBuffer(), 0, (int)ms.Length);
2127
            }
2128
            else
2129
            {
2130
                return null;
2131
            }
2132
        }
2133
    }
2134

    
2135
    public class UserResultDocumentHandler : IResultDocumentHandler
2136
    {
2137

    
2138
        private Hashtable results;
2139

    
2140
        public UserResultDocumentHandler(Hashtable table)
2141
        {
2142
            this.results = table;
2143
        }
2144

    
2145
        public XmlDestination HandleResultDocument(string href, Uri baseUri)
2146
        {
2147
            DomDestination destination = new DomDestination();
2148
            results[href] = destination;
2149
            return destination;
2150
        }
2151

    
2152
    }
2153
}
2154

    
2155

    
2156
//
2157
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
2158
// you may not use this file except in compliance with the License. You may obtain a copy of the
2159
// License at http://www.mozilla.org/MPL/
2160
//
2161
// Software distributed under the License is distributed on an "AS IS" basis,
2162
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
2163
// See the License for the specific language governing rights and limitations under the License.
2164
//
2165
// The Original Code is: all this file.
2166
//
2167
// The Initial Developer of the Original Code is Michael H. Kay.
2168
//
2169
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
2170
//
2171
// Contributor(s): none.
2172
//
2173

    
2174

    
2175

    
2176

    
(2-2/5)