Project

Profile

Help

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

he / latest9.4 / hen / csource / samples / ExamplesHE.cs @ c3c84ba2

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

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

    
24

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

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

    
68
            Boolean ask = true;
69
            String test = "all";
70

    
71
            String samplesPath = null;
72
            Uri samplesDir;
73

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

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

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

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

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

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

    
194
    ///<summary>
195
    /// Each of the example programs is implemented as a subclass of the abstract class Example
196
    ///</summary> 
197

    
198

    
199
    public abstract class Example
200
    {
201
        /// <summary>
202
        /// Read-only property: the name of the test example
203
        /// </summary>
204
        public abstract String testName { get; }
205
        /// <summary>
206
        /// Entry point for running the example
207
        /// </summary>
208
        public abstract void run(Uri samplesDir);
209
    }
210

    
211
    /// <summary>
212
    /// XPath expression selecting from a source document supplied as a URI
213
    /// </summary>
214

    
215
    public class XPathSimple : Example
216
    {
217

    
218
        public override String testName
219
        {
220
            get { return "XPathSimple"; }
221
        }
222

    
223
        /// <summary>
224
        /// Run a transformation: simplest possible script
225
        /// </summary>
226

    
227
        public override void run(Uri samplesDir)
228
        {
229
            // Create a Processor instance.
230
            Processor processor = new Processor();
231

    
232
            // Load the source document
233
            XdmNode input = processor.NewDocumentBuilder().Build(new Uri(samplesDir, "data/books.xml"));
234

    
235
            // Create an XPath compiler
236
            XPathCompiler xpath = processor.NewXPathCompiler();
237

    
238
            // Enable caching, so each expression is only compiled once
239
            xpath.Caching = true;
240

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

    
250
    /// <summary>
251
    /// XPath expression using variables (and no source document)
252
    /// </summary>
253

    
254
    public class XPathVariables : Example
255
    {
256

    
257
        public override String testName
258
        {
259
            get { return "XPathVariables"; }
260
        }
261

    
262
        /// <summary>
263
        /// Run a transformation: simplest possible script
264
        /// </summary>
265

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

    
271
            // Create the XPath expression.
272
            XPathCompiler compiler = processor.NewXPathCompiler();
273
            compiler.DeclareVariable(new QName("", "a"));
274
            compiler.DeclareVariable(new QName("", "b"));
275
            XPathSelector selector = compiler.Compile("$a + $b").Load();
276

    
277
            // Set the values of the variables
278
            selector.SetVariable(new QName("", "a"), new XdmAtomicValue(2));
279
            selector.SetVariable(new QName("", "b"), new XdmAtomicValue(3));
280

    
281
            // Evaluate the XPath expression
282
            Console.WriteLine(selector.EvaluateSingle().ToString());
283
        }
284
    }
285

    
286
    /// <summary>
287
    /// XPath expression using variables without explicit declaration
288
    /// </summary>
289

    
290
    public class XPathUndeclaredVariables : Example
291
    {
292

    
293
        public override String testName
294
        {
295
            get { return "XPathUndeclaredVariables"; }
296
        }
297

    
298
        /// <summary>
299
        /// Execute an XPath expression containing undeclared variables
300
        /// </summary>
301

    
302
        public override void run(Uri samplesDir)
303
        {
304
            // Create a Processor instance.
305
            Processor processor = new Processor();
306

    
307
            // Create the XPath expression.
308
            XPathCompiler compiler = processor.NewXPathCompiler();
309
            compiler.AllowUndeclaredVariables = true;
310
            XPathExecutable expression = compiler.Compile("$a + $b");
311
            XPathSelector selector = expression.Load();
312

    
313
            // Set the values of the variables
314
            IEnumerator vars = expression.EnumerateExternalVariables();
315
            while (vars.MoveNext())
316
            {
317
                selector.SetVariable((QName)vars.Current, new XdmAtomicValue(10));
318
            }
319

    
320
            // Evaluate the XPath expression
321
            Console.WriteLine(selector.EvaluateSingle().ToString());
322
        }
323
    }
324

    
325
    /// <summary>
326
    /// XPath expression throwing a static error
327
    /// </summary>
328

    
329
    public class XPathWithStaticError : Example
330
    {
331

    
332
        public override String testName
333
        {
334
            get { return "XPathWithStaticError"; }
335
        }
336

    
337
        /// <summary>
338
        /// Execute an XPath expression that throws a dynamic error, and catch the error
339
        /// </summary>
340

    
341
        public override void run(Uri samplesDir)
342
        {
343
            // Create a Processor instance.
344
            Processor processor = new Processor();
345

    
346
            // Create the XPath expression.
347
            XPathCompiler compiler = processor.NewXPathCompiler();
348
            compiler.AllowUndeclaredVariables = true;
349
            XPathExecutable expression = compiler.Compile("1 + unknown()");
350
            XPathSelector selector = expression.Load();
351

    
352
            // Evaluate the XPath expression
353
            Console.WriteLine(selector.EvaluateSingle().ToString());
354
        }
355
    }
356

    
357
    /// <summary>
358
    /// XPath expression throwing a dynamic error
359
    /// </summary>
360

    
361
    public class XPathWithDynamicError : Example
362
    {
363

    
364
        public override String testName
365
        {
366
            get { return "XPathWithDynamicError"; }
367
        }
368

    
369
        /// <summary>
370
        /// Execute an XPath expression that throws a dynamic error
371
        /// </summary>
372

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

    
378
            // Create the XPath expression.
379
            XPathCompiler compiler = processor.NewXPathCompiler();
380
            compiler.AllowUndeclaredVariables = true;
381
            XPathExecutable expression = compiler.Compile("$a gt $b");
382
            XPathSelector selector = expression.Load();
383

    
384
            // Set the values of the variables
385
            selector.SetVariable(new QName("", "a"), new XdmAtomicValue(10));
386
            selector.SetVariable(new QName("", "b"), new XdmAtomicValue("Paris"));
387

    
388
            // Evaluate the XPath expression
389
            Console.WriteLine(selector.EvaluateSingle().ToString());
390
        }
391
    }
392

    
393
    /// <summary>
394
    /// XSLT 2.0 transformation with source document and stylesheet supplied as URIs
395
    /// </summary>
396

    
397
    public class XsltSimple1 : Example
398
    {
399

    
400
        public override String testName
401
        {
402
            get { return "XsltSimple1"; }
403
        }
404

    
405
        /// <summary>
406
        /// Run a transformation: simplest possible script
407
        /// </summary>
408

    
409
        public override void run(Uri samplesDir)
410
        {
411
            // Create a Processor instance.
412
            Processor processor = new Processor();
413

    
414
            // Load the source document
415
            XdmNode input = processor.NewDocumentBuilder().Build(new Uri(samplesDir, "data/books.xml"));
416

    
417
            // Create a transformer for the stylesheet.
418
            XsltTransformer transformer = processor.NewXsltCompiler().Compile(new Uri(samplesDir, "styles/books.xsl")).Load();
419

    
420
            // Set the root node of the source document to be the initial context node
421
            transformer.InitialContextNode = input;
422

    
423
            // Create a serializer, with output to the standard output stream
424
            Serializer serializer = new Serializer();
425
            serializer.SetOutputWriter(Console.Out);
426

    
427
            // Transform the source XML and serialize the result document
428
            transformer.Run(serializer);
429
        }
430
    }
431

    
432
    /// <summary>
433
    /// Run a transformation, sending the serialized output to a file
434
    /// </summary>
435

    
436
    public class XsltSimple2 : Example
437
    {
438

    
439
        public override String testName
440
        {
441
            get { return "XsltSimple2"; }
442
        }
443

    
444
        /// <summary>
445
        /// Run the transformation, sending the serialized output to a file
446
        /// </summary>
447

    
448

    
449
        public override void run(Uri samplesDir)
450
        {
451
            // Create a Processor instance.
452
            Processor processor = new Processor();
453

    
454
            // Load the source document
455
            XdmNode input = processor.NewDocumentBuilder().Build(new Uri(samplesDir, "data/books.xml"));
456

    
457
            // Create a transformer for the stylesheet.
458
            XsltTransformer transformer = processor.NewXsltCompiler().Compile(new Uri(samplesDir, "styles/identity.xsl")).Load();
459

    
460
            // Set the root node of the source document to be the initial context node
461
            transformer.InitialContextNode = input;
462

    
463
            // Create a serializer
464
            String outfile = "OutputFromXsltSimple2.xml";
465
            Serializer serializer = new Serializer();
466
            serializer.SetOutputStream(new FileStream(outfile, FileMode.Create, FileAccess.Write));
467

    
468
            // Transform the source XML to System.out.
469
            transformer.Run(serializer);
470

    
471
            Console.WriteLine("\nOutput written to " + outfile + "\n");
472
        }
473
    }
474

    
475
    /// <summary>
476
    /// XSLT 2.0 transformation with source document and stylesheet supplied as URIs
477
    /// </summary>
478

    
479
    public class XsltSimple3 : Example
480
    {
481

    
482
        public override String testName
483
        {
484
            get { return "XsltSimple3"; }
485
        }
486

    
487
        /// <summary>
488
        /// Run a transformation: supply input as a file
489
        /// </summary>
490

    
491
        public override void run(Uri samplesDir)
492
        {
493
            if (samplesDir.Scheme != Uri.UriSchemeFile)
494
            {
495
                Console.WriteLine("Supplied URI must be a file directory");
496
            }
497
            String dir = samplesDir.AbsolutePath;
498
            String sourceFile = dir + "data/books.xml";
499
            String styleFile = dir + "styles/books.xsl";
500

    
501
            // Create a Processor instance.
502
            Processor processor = new Processor();
503

    
504
            // Load the source document
505

    
506
            DocumentBuilder builder = processor.NewDocumentBuilder();
507
            builder.BaseUri = new Uri(samplesDir, "data/books.xml");
508

    
509
            XdmNode input = builder.Build(File.OpenRead(sourceFile));
510

    
511
            // Create a transformer for the stylesheet.
512
            XsltCompiler compiler = processor.NewXsltCompiler();
513
            compiler.BaseUri = new Uri(samplesDir, "styles/books.xsl");
514
            XsltTransformer transformer = compiler.Compile(File.OpenRead(styleFile)).Load();
515

    
516
            // Set the root node of the source document to be the initial context node
517
            transformer.InitialContextNode = input;
518

    
519
            // Create a serializer, with output to the standard output stream
520
            Serializer serializer = new Serializer();
521
            serializer.SetOutputWriter(Console.Out);
522

    
523
            // Transform the source XML and serialize the result document
524
            transformer.Run(serializer);
525
        }
526
    }
527

    
528

    
529
    /// <summary>
530
    /// XSLT 2.0 transformation showing stripping of whitespace controlled by the stylesheet
531
    /// </summary>
532

    
533
    public class XsltStripSpace : Example
534
    {
535

    
536
        public override String testName
537
        {
538
            get { return "XsltStripSpace"; }
539
        }
540

    
541
        /// <summary>
542
        /// Run a transformation: simplest possible script
543
        /// </summary>
544

    
545
        public override void run(Uri samplesDir)
546
        {
547
            Processor processor = new Processor();
548

    
549
            // Load the source document
550
            DocumentBuilder builder = processor.NewDocumentBuilder();
551
            builder.BaseUri = samplesDir;
552

    
553
            String doc = "<doc>  <a>  <b>text</b>  </a>  <a/>  </doc>";
554
            MemoryStream ms = new MemoryStream();
555
            StreamWriter tw = new StreamWriter(ms);
556
            tw.Write(doc);
557
            tw.Flush();
558
            Stream instr = new MemoryStream(ms.GetBuffer(), 0, (int)ms.Length);
559
            XdmNode input = builder.Build(instr);
560

    
561
            // Create a transformer for the stylesheet.
562
            String stylesheet =
563
                "<xsl:stylesheet xmlns:xsl='http://www.w3.org/1999/XSL/Transform' version='2.0'>" +
564
                "<xsl:strip-space elements='*'/>" +
565
                "<xsl:template match='/'>" +
566
                "  <xsl:copy-of select='.'/>" +
567
                "</xsl:template>" +
568
                "</xsl:stylesheet>";
569

    
570
            XsltCompiler compiler = processor.NewXsltCompiler();
571
            compiler.BaseUri = samplesDir;
572
            XsltTransformer transformer = compiler.Compile(new XmlTextReader(new StringReader(stylesheet))).Load();
573

    
574
            // Set the root node of the source document to be the initial context node
575
            transformer.InitialContextNode = input;
576

    
577
            // Create a serializer
578
            Serializer serializer = new Serializer();
579
            serializer.SetOutputWriter(Console.Out);
580

    
581
            // Transform the source XML to System.out.
582
            transformer.Run(serializer);
583
        }
584
    }
585

    
586

    
587
    /// <summary>
588
    /// Run a transformation, compiling the stylesheet once and using it to transform two different source documents
589
    /// </summary>
590

    
591
    public class XsltReuseExecutable : Example
592
    {
593

    
594
        public override String testName
595
        {
596
            get { return "XsltReuseExecutable"; }
597
        }
598

    
599
        /// <summary>
600
        /// Show that a stylesheet can be compiled once (into an XsltExecutable) and run many times
601
        /// </summary>
602
        /// <param name="fileNames">
603
        /// 1. first source document
604
        /// 2. second source document
605
        /// 3. stylesheet used to transform both documents
606
        /// </param>
607

    
608
        public override void run(Uri samplesDir)
609
        {
610
            // Create a Processor instance.
611
            Processor processor = new Processor();
612

    
613
            // Create a compiled stylesheet
614
            XsltExecutable templates = processor.NewXsltCompiler().Compile(new Uri(samplesDir, "styles/summarize.xsl"));
615

    
616
            // Note: we could actually use the same XsltTransformer in this case.
617
            // But in principle, the two transformations could be done in parallel in separate threads.
618

    
619
            String sourceFile1 = "data/books.xml";
620
            String sourceFile2 = "data/othello.xml";
621

    
622
            // Do the first transformation
623
            Console.WriteLine("\n\n----- transform of " + sourceFile1 + " -----");
624
            XsltTransformer transformer1 = templates.Load();
625
            transformer1.InitialContextNode = processor.NewDocumentBuilder().Build(new Uri(samplesDir, sourceFile1));
626
            transformer1.Run(new Serializer());     // default destination is Console.Out
627

    
628
            // Do the second transformation
629
            Console.WriteLine("\n\n----- transform of " + sourceFile2 + " -----");
630
            XsltTransformer transformer2 = templates.Load();
631
            transformer2.InitialContextNode = processor.NewDocumentBuilder().Build(new Uri(samplesDir, sourceFile2));
632
            transformer2.Run(new Serializer());     // default destination is Console.Out    
633
        }
634
    }
635

    
636
    /// <summary>
637
    /// Show that the XsltTransformer is serially reusable; run a transformation twice using the same stylesheet
638
    /// and the same input document but with different parameters.
639
    /// </summary>
640

    
641
    public class XsltReuseTransformer : Example
642
    {
643

    
644
        public override String testName
645
        {
646
            get { return "XsltReuseTransformer"; }
647
        }
648

    
649
        /// <summary>
650
        /// Show that the XsltTransformer is serially reusable (we run it twice with different parameter settings)
651
        /// </summary>
652

    
653
        public override void run(Uri samplesDir)
654
        {
655
            // Create a Processor instance.
656
            Processor processor = new Processor();
657

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

    
661
            // Compile the stylesheet
662
            XsltExecutable exec = processor.NewXsltCompiler().Compile(new Uri(samplesDir, "styles/summarize.xsl"));
663

    
664
            // Create a transformer 
665
            XsltTransformer transformer = exec.Load();
666

    
667
            // Run it once        
668
            transformer.SetParameter(new QName("", "", "include-attributes"), new XdmAtomicValue(false));
669
            transformer.InitialContextNode = input;
670
            XdmDestination results = new XdmDestination();
671
            transformer.Run(results);
672
            Console.WriteLine("1: " + results.XdmNode.OuterXml);
673

    
674
            // Run it again        
675
            transformer.SetParameter(new QName("", "", "include-attributes"), new XdmAtomicValue(true));
676
            transformer.InitialContextNode = input;
677
            results.Reset();
678
            transformer.Run(results);
679
            Console.WriteLine("2: " + results.XdmNode.OuterXml);
680
        }
681
    }
682

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

    
687
    public class XsltFilterChain : Example
688
    {
689

    
690
        public override String testName
691
        {
692
            get { return "XsltFilterChain"; }
693
        }
694

    
695
        /// <summary>
696
        /// Run the test
697
        /// </summary>
698

    
699
        public override void run(Uri samplesDir)
700
        {
701
            // Create a Processor instance.
702
            Processor processor = new Processor();
703

    
704
            // Load the source document
705
            XdmNode input = processor.NewDocumentBuilder().Build(new Uri(samplesDir, "data/books.xml"));
706

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

    
710
            // Compile all three stylesheets
711
            XsltTransformer transformer1 = compiler.Compile(new Uri(samplesDir, "styles/identity.xsl")).Load();
712
            XsltTransformer transformer2 = compiler.Compile(new Uri(samplesDir, "styles/books.xsl")).Load();
713
            XsltTransformer transformer3 = compiler.Compile(new Uri(samplesDir, "styles/summarize.xsl")).Load();
714

    
715
            // Now run them in series
716
            transformer1.InitialContextNode = input;
717
            XdmDestination results1 = new XdmDestination();
718
            transformer1.Run(results1);
719
            //Console.WriteLine("After phase 1:");
720
            //Console.WriteLine(results1.XdmNode.OuterXml);
721

    
722
            transformer2.InitialContextNode = results1.XdmNode;
723
            XdmDestination results2 = new XdmDestination();
724
            transformer2.Run(results2);
725
            //Console.WriteLine("After phase 2:");
726
            //Console.WriteLine(results2.XdmNode.OuterXml);
727

    
728
            transformer3.InitialContextNode = results2.XdmNode;
729
            //TextWriterDestination results3 = new TextWriterDestination(new XmlTextWriter(Console.Out));
730
            XdmDestination results3 = new XdmDestination();
731
            transformer3.Run(results3);
732
            Console.WriteLine("After phase 3:");
733
            Console.WriteLine(results3.XdmNode.OuterXml);
734
        }
735
    }
736

    
737
    /// <summary>
738
    /// Transform from an XDM tree to an XDM tree
739
    /// </summary>
740

    
741
    public class XsltXdmToXdm : Example
742
    {
743

    
744
        public override String testName
745
        {
746
            get { return "XsltXdmToXdm"; }
747
        }
748

    
749
        /// <summary>
750
        /// Transform from an XDM tree to an XDM tree
751
        /// </summary>
752

    
753
        public override void run(Uri samplesDir)
754
        {
755
            // Create a Processor instance.
756
            Processor processor = new Processor();
757

    
758
            // Load the source document
759
            XdmNode input = processor.NewDocumentBuilder().Build(new Uri(samplesDir, "data/books.xml"));
760

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

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

    
767
            // Run the transformation
768
            transformer.InitialContextNode = input;
769
            XdmDestination result = new XdmDestination();
770
            transformer.Run(result);
771

    
772
            // Serialize the result so we can see that it worked
773
            StringWriter sw = new StringWriter();
774
            result.XdmNode.WriteTo(new XmlTextWriter(sw));
775
            Console.WriteLine(sw.ToString());
776

    
777
            // Note: we don't do 
778
            //   result.XdmNode.WriteTo(new XmlTextWriter(Console.Out));
779
            // because that results in the Console.out stream being closed, 
780
            // with subsequent attempts to write to it being rejected.
781
        }
782
    }
783

    
784
    /// <summary>
785
    /// Run an XSLT transformation from an Xdm tree, starting at a node that is not the document node
786
    /// </summary>
787

    
788
    public class XsltXdmElementToXdm : Example
789
    {
790

    
791
        public override String testName
792
        {
793
            get { return "XsltXdmElementToXdm"; }
794
        }
795

    
796
        /// <summary>
797
        /// Run an XSLT transformation from an Xdm tree, starting at a node that is not the document node
798
        /// </summary>
799
        /// <param name="fileNames">
800
        /// 1. The source document
801
        /// 2. The stylesheet
802
        /// </param>
803

    
804
        public override void run(Uri samplesDir)
805
        {
806
            // Create a Processor instance.
807
            Processor processor = new Processor();
808

    
809
            // Load the source document
810
            XdmNode input = processor.NewDocumentBuilder().Build(new Uri(samplesDir, "data/othello.xml"));
811

    
812
            // Navigate to the first grandchild
813
            XPathSelector eval = processor.NewXPathCompiler().Compile("/PLAY/FM[1]").Load();
814
            eval.ContextItem = input;
815
            input = (XdmNode)eval.EvaluateSingle();
816

    
817
            // Create an XSLT compiler
818
            XsltCompiler compiler = processor.NewXsltCompiler();
819

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

    
823
            // Run the transformation
824
            transformer.InitialContextNode = input;
825
            XdmDestination result = new XdmDestination();
826
            transformer.Run(result);
827

    
828
            // Serialize the result so we can see that it worked
829
            Console.WriteLine(result.XdmNode.OuterXml);
830
        }
831
    }
832

    
833
    /// <summary>
834
    /// Run a transformation from a DOM (System.Xml.Document) input to a DOM output
835
    /// </summary>
836

    
837
    public class XsltDomToDom : Example
838
    {
839

    
840
        public override String testName
841
        {
842
            get { return "XsltDomToDom"; }
843
        }
844

    
845
        /// <summary>
846
        /// Run a transformation from a DOM (System.Xml.Document) input to a DOM output
847
        /// </summary>
848

    
849
        public override void run(Uri samplesDir)
850
        {
851

    
852
            // Create a Processor instance.
853
            Processor processor = new Processor();
854

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

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

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

    
866
            // Run the transformation
867
            transformer.InitialContextNode = input;
868
            DomDestination result = new DomDestination();
869
            transformer.Run(result);
870

    
871
            // Serialize the result so we can see that it worked
872
            Console.WriteLine(result.XmlDocument.OuterXml);
873
        }
874
    }
875

    
876

    
877
    /// <summary>
878
    /// Run a transformation driven by an xml-stylesheet processing instruction in the source document
879
    /// </summary>
880

    
881
    public class XsltProcessingInstruction : Example
882
    {
883

    
884
        public override string testName
885
        {
886
            get { return "XsltProcessingInstruction"; }
887
        }
888

    
889
        /// <summary>
890
        /// Run a transformation driven by an xml-stylesheet processing instruction in the source document
891
        /// </summary>
892
        /// <param name="fileNames">
893
        /// 1. The source document
894
        /// </param>
895

    
896
        public override void run(Uri samplesDir)
897
        {
898
            // Create a Processor instance.
899
            Processor processor = new Processor();
900
            XsltExecutable exec;
901

    
902
            // Load the source document
903
            XdmNode input = processor.NewDocumentBuilder().Build(new Uri(samplesDir, "data/books.xml"));
904
            //Console.WriteLine("=============== source document ===============");
905
            //Console.WriteLine(input.OuterXml);
906
            //Console.WriteLine("=========== end of source document ============");
907

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

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

    
914
            XPathSelector eval = processor.NewXPathCompiler().Compile(path).Load();
915
            eval.ContextItem = input;
916
            XdmAtomicValue hrefval = (XdmAtomicValue)eval.EvaluateSingle();
917
            String href = (hrefval == null ? null : hrefval.ToString());
918

    
919
            if (href == null || href == "")
920
            {
921
                Console.WriteLine("No suitable xml-stylesheet processing instruction found");
922
                return;
923

    
924
            }
925
            else if (href[0] == '#')
926
            {
927

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

    
930
                Console.WriteLine("Locating embedded stylesheet with href = " + href);
931
                String idpath = "id('" + href.Substring(1) + "')";
932
                eval = processor.NewXPathCompiler().Compile(idpath).Load();
933
                eval.ContextItem = input;
934
                XdmNode node = (XdmNode)eval.EvaluateSingle();
935
                if (node == null)
936
                {
937
                    Console.WriteLine("No element found with ID " + href.Substring(1));
938
                    return;
939
                }
940
                exec = processor.NewXsltCompiler().Compile(node);
941

    
942
            }
943
            else
944
            {
945

    
946
                // The stylesheet is in an external document
947

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

    
950
                // Fetch and compile the referenced stylesheet
951
                exec = processor.NewXsltCompiler().Compile(new Uri(input.BaseUri, href.ToString()));
952
            }
953

    
954
            // Create a transformer 
955
            XsltTransformer transformer = exec.Load();
956

    
957
            // Run it       
958
            transformer.InitialContextNode = input;
959
            XdmDestination results = new XdmDestination();
960
            transformer.Run(results);
961
            Console.WriteLine("1: " + results.XdmNode.OuterXml);
962

    
963
        }
964
    }
965

    
966
    /// <summary>
967
    /// Run an XSLT transformation setting serialization properties from the calling application
968
    /// </summary>
969

    
970
    public class XsltSettingOutputProperties : Example
971
    {
972

    
973
        public override string testName
974
        {
975
            get { return "XsltSettingOutputProperties"; }
976
        }
977

    
978
        /// <summary>
979
        /// Run an XSLT transformation setting serialization properties from the calling application
980
        /// </summary>
981

    
982
        public override void run(Uri samplesDir)
983
        {
984
            // Create a Processor instance.
985
            Processor processor = new Processor();
986

    
987
            // Load the source document
988
            XdmNode input = processor.NewDocumentBuilder().Build(new Uri(samplesDir, "data/books.xml"));
989

    
990
            // Create a transformer for the stylesheet.
991
            XsltTransformer transformer = processor.NewXsltCompiler().Compile(new Uri(samplesDir, "styles/summarize.xsl")).Load();
992

    
993
            // Set the root node of the source document to be the initial context node
994
            transformer.InitialContextNode = input;
995

    
996
            // Create a serializer, with output to the standard output stream
997
            Serializer serializer = new Serializer();
998
            serializer.SetOutputProperty(Serializer.METHOD, "xml");
999
            serializer.SetOutputProperty(Serializer.INDENT, "no");
1000
            serializer.SetOutputWriter(Console.Out);
1001

    
1002
            // Transform the source XML and serialize the result document
1003
            transformer.Run(serializer);
1004
        }
1005

    
1006
    }
1007

    
1008
    /// <summary>
1009
    /// Run an XSLT transformation making use of an XmlResolver to resolve URIs at document build time, at stylesheet compile time 
1010
    /// and at transformation run-time
1011
    /// </summary>
1012

    
1013
    public class XsltUsingSourceResolver : Example
1014
    {
1015

    
1016
        public override string testName
1017
        {
1018
            get { return "XsltUsingSourceResolver"; }
1019
        }
1020

    
1021
        /// <summary>
1022
        /// Run an XSLT transformation making use of an XmlResolver to resolve URIs both at compile time and at run-time
1023
        /// </summary>
1024

    
1025
        public override void run(Uri samplesDir)
1026
        {
1027

    
1028
            // Create a Processor instance.
1029
            Processor processor = new Processor();
1030

    
1031
            // Load the source document
1032
            DocumentBuilder builder = processor.NewDocumentBuilder();
1033
            UserXmlResolver buildTimeResolver = new UserXmlResolver();
1034
            buildTimeResolver.Message = "** Calling build-time XmlResolver: ";
1035
            builder.XmlResolver = buildTimeResolver;
1036
            builder.BaseUri = samplesDir;
1037

    
1038
            String doc = "<!DOCTYPE doc [<!ENTITY e SYSTEM 'flamingo.txt'>]><doc>&e;</doc>";
1039
            MemoryStream ms = new MemoryStream();
1040
            StreamWriter tw = new StreamWriter(ms);
1041
            tw.Write(doc);
1042
            tw.Flush();
1043
            Stream instr = new MemoryStream(ms.GetBuffer(), 0, (int)ms.Length);
1044
            XdmNode input = builder.Build(instr);
1045

    
1046
            // Create a transformer for the stylesheet.
1047
            String stylesheet =
1048
                "<xsl:stylesheet xmlns:xsl='http://www.w3.org/1999/XSL/Transform' version='2.0'>" +
1049
                "<xsl:import href='empty.xslt'/>" +
1050
                "<xsl:template match='/'>" +
1051
                "<out note=\"{doc('heron.txt')}\" ><xsl:copy-of select='.'/></out>" +
1052
                "</xsl:template>" +
1053
                "</xsl:stylesheet>";
1054

    
1055
            XsltCompiler compiler = processor.NewXsltCompiler();
1056
            UserXmlResolver compileTimeResolver = new UserXmlResolver();
1057
            compileTimeResolver.Message = "** Calling compile-time XmlResolver: ";
1058
            compiler.XmlResolver = compileTimeResolver;
1059
            compiler.BaseUri = samplesDir;
1060
            XsltTransformer transformer = compiler.Compile(new XmlTextReader(new StringReader(stylesheet))).Load();
1061

    
1062
            // Set the root node of the source document to be the initial context node
1063
            transformer.InitialContextNode = input;
1064

    
1065
            // Set the user-written XmlResolver
1066
            UserXmlResolver runTimeResolver = new UserXmlResolver();
1067
            runTimeResolver.Message = "** Calling transformation-time XmlResolver: ";
1068
            transformer.InputXmlResolver = runTimeResolver;
1069

    
1070
            // Create a serializer
1071
            Serializer serializer = new Serializer();
1072
            serializer.SetOutputWriter(Console.Out);
1073

    
1074
            // Transform the source XML to System.out.
1075
            transformer.Run(serializer);
1076

    
1077
        }
1078
    }
1079

    
1080
    /// <summary>
1081
    /// Run an XSLT transformation displaying compile-time errors to the console
1082
    /// </summary>
1083

    
1084
    public class XsltDisplayingErrors : Example
1085
    {
1086

    
1087
        public override string testName
1088
        {
1089
            get { return "XsltDisplayingErrors"; }
1090
        }
1091

    
1092
        /// <summary>
1093
        /// Run an XSLT transformation displaying compile-time errors to the console
1094
        /// </summary>
1095

    
1096
        public override void run(Uri samplesDir)
1097
        {
1098
            // Create a Processor instance.
1099
            Processor processor = new Processor();
1100

    
1101
            // Create the XSLT Compiler
1102
            XsltCompiler compiler = processor.NewXsltCompiler();
1103

    
1104

    
1105
            // Define a stylesheet containing errors
1106
            String stylesheet =
1107
                "<xsl:stylesheet xmlns:xsl='http://www.w3.org/1999/XSL/Transform' version='1.0'>\n" +
1108
                "<xsl:template name='eee:template'>\n" +
1109
                "  <xsl:value-of select='32'/>\n" +
1110
                "</xsl:template>\n" +
1111
                "<xsl:template name='main'>\n" +
1112
                "  <xsl:value-of select='$var'/>\n" +
1113
                "</xsl:template>\n" +
1114
                "</xsl:stylesheet>";
1115

    
1116

    
1117
            // Attempt to compile the stylesheet and display the errors
1118
            try
1119
            {
1120
                compiler.BaseUri = new Uri("http://localhost/stylesheet");
1121
                compiler.Compile(new XmlTextReader(new StringReader(stylesheet)));
1122
                Console.WriteLine("Stylesheet compilation succeeded");
1123
            }
1124
            catch (Exception)
1125
            {
1126
                Console.WriteLine("Stylesheet compilation failed");
1127
            }
1128

    
1129

    
1130
        }
1131
    }
1132

    
1133
    /// <summary>
1134
    /// Run an XSLT transformation capturing compile-time errors within the application
1135
    /// </summary>
1136

    
1137
    public class XsltCapturingErrors : Example
1138
    {
1139

    
1140
        public override string testName
1141
        {
1142
            get { return "XsltCapturingErrors"; }
1143
        }
1144

    
1145
        /// <summary>
1146
        /// Run an XSLT transformation capturing compile-time errors within the application
1147
        /// </summary>
1148

    
1149
        public override void run(Uri samplesDir)
1150
        {
1151
            // Create a Processor instance.
1152
            Processor processor = new Processor();
1153

    
1154
            // Create the XSLT Compiler
1155
            XsltCompiler compiler = processor.NewXsltCompiler();
1156

    
1157
            // Create a list to hold the error information
1158
            compiler.ErrorList = new ArrayList();
1159

    
1160
            // Define a stylesheet containing errors
1161
            String stylesheet =
1162
                "<xsl:stylesheet xmlns:xsl='http://www.w3.org/1999/XSL/Transform' version='1.0'>\n" +
1163
                "<xsl:template name='fff:template'>\n" +
1164
                "  <xsl:value-of select='32'/>\n" +
1165
                "</xsl:template>\n" +
1166
                "<xsl:template name='main'>\n" +
1167
                "  <xsl:value-of select='$var'/>\n" +
1168
                "</xsl:template>\n" +
1169
                "</xsl:stylesheet>";
1170

    
1171

    
1172
            // Attempt to compile the stylesheet and display the errors
1173
            try
1174
            {
1175
                compiler.BaseUri = new Uri("http://localhost/stylesheet");
1176
                compiler.Compile(new StringReader(stylesheet));
1177
                Console.WriteLine("Stylesheet compilation succeeded");
1178
            }
1179
            catch (Exception)
1180
            {
1181
                Console.WriteLine("Stylesheet compilation failed with " + compiler.ErrorList.Count + " errors");
1182
                foreach (StaticError error in compiler.ErrorList)
1183
                {
1184
                    Console.WriteLine("At line " + error.LineNumber + ": " + error.Message);
1185
                }
1186
            }
1187
        }
1188
    }
1189

    
1190
    /// <summary>
1191
    /// Run an XSLT transformation capturing run-time messages within the application
1192
    /// </summary>
1193

    
1194
    public class XsltCapturingMessages : Example
1195
    {
1196

    
1197
        public override string testName
1198
        {
1199
            get { return "XsltCapturingMessages"; }
1200
        }
1201

    
1202
        /// <summary>
1203
        /// Run an XSLT transformation capturing run-time messages within the application
1204
        /// </summary>
1205

    
1206
        public override void run(Uri samplesDir)
1207
        {
1208

    
1209
            // Create a Processor instance.
1210
            Processor processor = new Processor();
1211

    
1212
            // Create the XSLT Compiler
1213
            XsltCompiler compiler = processor.NewXsltCompiler();
1214

    
1215
            // Define a stylesheet that generates messages
1216
            String stylesheet =
1217
                "<xsl:stylesheet xmlns:xsl='http://www.w3.org/1999/XSL/Transform' version='2.0'>\n" +
1218
                "<xsl:template name='main'>\n" +
1219
                "  <xsl:message><a>starting</a></xsl:message>\n" +
1220
                "  <out><xsl:value-of select='current-date()'/></out>\n" +
1221
                "  <xsl:message><a>finishing</a></xsl:message>\n" +
1222
                "</xsl:template>\n" +
1223
                "</xsl:stylesheet>";
1224

    
1225
            compiler.BaseUri = new Uri("http://localhost/stylesheet");
1226
            XsltExecutable exec = compiler.Compile(new StringReader(stylesheet));
1227

    
1228

    
1229
            // Create a transformer for the stylesheet.
1230
            XsltTransformer transformer = exec.Load();
1231

    
1232
            // Set the name of the initial template
1233
            transformer.InitialTemplate = new QName("", "main");
1234

    
1235
            // Create a Listener to which messages will be written
1236
            transformer.MessageListener = new UserMessageListener();
1237

    
1238
            // Create a serializer
1239
            Serializer serializer = new Serializer();
1240
            serializer.SetOutputWriter(Console.Out);
1241

    
1242
            // Transform the source XML to System.out.
1243
            transformer.Run(serializer);
1244
        }
1245

    
1246
    }
1247

    
1248
    ///
1249
    /// Example user-written message listener
1250
    ///
1251

    
1252
    public class UserMessageListener : IMessageListener
1253
    {
1254

    
1255
        public void Message(XdmNode content, bool terminate, IXmlLocation location)
1256
        {
1257
            Console.Out.WriteLine("MESSAGE terminate=" + (terminate ? "yes" : "no") + " at " + DateTime.Now);
1258
            Console.Out.WriteLine("From instruction at line " + location.LineNumber +
1259
                    " of " + location.BaseUri);
1260
            Console.Out.WriteLine(">>" + content.StringValue);
1261
        }
1262
    }
1263

    
1264

    
1265
    /// <summary>
1266
    /// Run an XSLT transformation producing multiple output documents
1267
    /// </summary>
1268

    
1269
    public class XsltMultipleOutput : Example
1270
    {
1271

    
1272
        public override string testName
1273
        {
1274
            get { return "XsltMultipleOutput"; }
1275
        }
1276

    
1277
        /// <summary>
1278
        /// Run an XSLT transformation producing multiple output documents
1279
        /// </summary>
1280

    
1281
        public override void run(Uri samplesDir)
1282
        {
1283
            // Create a Processor instance.
1284
            Processor processor = new Processor();
1285
            processor.SetProperty("http://saxon.sf.net/feature/timing", "true");
1286

    
1287
            // Load the source document
1288
            XdmNode input = processor.NewDocumentBuilder().Build(new Uri(samplesDir, "data/othello.xml"));
1289

    
1290
            // Create a transformer for the stylesheet.
1291
            XsltTransformer transformer = processor.NewXsltCompiler().Compile(new Uri(samplesDir, "styles/play.xsl")).Load();
1292

    
1293
            // Set the root node of the source document to be the initial context node
1294
            transformer.InitialContextNode = input;
1295

    
1296
            // Set the required stylesheet parameter
1297
            transformer.SetParameter(new QName("", "", "dir"), new XdmAtomicValue(samplesDir.ToString() + "play"));
1298

    
1299
            // Create a serializer
1300
            Serializer serializer = new Serializer();
1301
            serializer.SetOutputWriter(Console.Out);
1302

    
1303
            // Transform the source XML to System.out.
1304
            transformer.Run(serializer);
1305

    
1306
        }
1307

    
1308
    }
1309

    
1310

    
1311
    /// <summary>
1312
    /// Run an XSLT transformation using the id() function, with DTD validation
1313
    /// </summary>
1314

    
1315
    public class XsltUsingIdFunction : Example
1316
    {
1317

    
1318
        public override string testName
1319
        {
1320
            get { return "XsltUsingIdFunction"; }
1321
        }
1322

    
1323
        /// <summary>
1324
        /// Run an XSLT transformation using the id() function, with DTD validation
1325
        /// </summary>
1326

    
1327
        public override void run(Uri samplesDir)
1328
        {
1329
            // Create a Processor instance
1330
            Processor processor = new Processor();
1331

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

    
1336
            String doc = "<!DOCTYPE table [" +
1337
                "<!ELEMENT table (row*)>" +
1338
                "<!ELEMENT row EMPTY>" +
1339
                "<!ATTLIST row xml:id ID #REQUIRED>" +
1340
                "<!ATTLIST row value CDATA #REQUIRED>]>" +
1341
                "<table><row xml:id='A123' value='green'/><row xml:id='Z789' value='blue'/></table>";
1342

    
1343
            DocumentBuilder builder = processor.NewDocumentBuilder();
1344
            builder.DtdValidation = true;
1345
            builder.BaseUri = samplesDir;
1346
            MemoryStream ms = new MemoryStream();
1347
            StreamWriter tw = new StreamWriter(ms);
1348
            tw.Write(doc);
1349
            tw.Flush();
1350
            Stream instr = new MemoryStream(ms.GetBuffer(), 0, (int)ms.Length);
1351
            XdmNode input = builder.Build(instr);
1352

    
1353
            // Define a stylesheet that uses the id() function
1354
            String stylesheet =
1355
                "<xsl:stylesheet xmlns:xsl='http://www.w3.org/1999/XSL/Transform' version='2.0'>\n" +
1356
                "<xsl:template match='/'>\n" +
1357
                "  <xsl:copy-of select=\"id('Z789')\"/>\n" +
1358
                "</xsl:template>\n" +
1359
                "</xsl:stylesheet>";
1360

    
1361
            XsltCompiler compiler = processor.NewXsltCompiler();
1362
            compiler.BaseUri = new Uri("http://localhost/stylesheet");
1363
            XsltExecutable exec = compiler.Compile(new StringReader(stylesheet));
1364

    
1365
            //Create a transformer for the stylesheet
1366
            XsltTransformer transformer = exec.Load();
1367

    
1368
            // Set the root node of the source document to be the initial context node
1369
            transformer.InitialContextNode = input;
1370

    
1371
            //Set the destination
1372
            XdmDestination results = new XdmDestination();
1373

    
1374
            // Transform the XML
1375
            transformer.Run(results);
1376

    
1377
            // Show the result
1378
            Console.WriteLine(results.XdmNode.ToString());
1379

    
1380
        }
1381

    
1382
    }
1383

    
1384
    /// <summary>
1385
    /// Show a transformation using a user-written result document handler. This example
1386
    /// captures each of the result documents in a DOM, and creates a Hashtable that indexes
1387
    /// the DOM trees according to their absolute URI. On completion, it writes all the DOMs
1388
    /// to the standard output.
1389
    /// </summary>
1390

    
1391
    public class XsltUsingResultHandler : Example
1392
    {
1393

    
1394
        public override string testName
1395
        {
1396
            get { return "XsltUsingResultHandler"; }
1397
        }
1398

    
1399
        /// <summary>
1400
        /// Show a transformation using a user-written result document handler. This example
1401
        /// captures each of the result documents in a DOM, and creates a Hashtable that indexes
1402
        /// the DOM trees according to their absolute URI. On completion, it writes all the DOMs
1403
        /// to the standard output.
1404
        /// </summary>
1405

    
1406
        public override void run(Uri samplesDir)
1407
        {
1408
            // Create a Processor instance.
1409
            Processor processor = new Processor();
1410

    
1411
            // Load the source document
1412
            XdmNode input = processor.NewDocumentBuilder().Build(new Uri(samplesDir, "data/othello.xml"));
1413

    
1414
            // Define a stylesheet that splits the document up
1415
            String stylesheet =
1416
                "<xsl:stylesheet xmlns:xsl='http://www.w3.org/1999/XSL/Transform' version='2.0'>\n" +
1417
                "<xsl:template match='/'>\n" +
1418
                "  <xsl:for-each select='//ACT'>\n" +
1419
                "    <xsl:result-document href='{position()}.xml'>\n" +
1420
                "      <xsl:copy-of select='TITLE'/>\n" +
1421
                "    </xsl:result-document>\n" +
1422
                "  </xsl:for-each>\n" +
1423
                "</xsl:template>\n" +
1424
                "</xsl:stylesheet>";
1425

    
1426
            XsltCompiler compiler = processor.NewXsltCompiler();
1427
            compiler.BaseUri = new Uri("http://localhost/stylesheet");
1428
            XsltExecutable exec = compiler.Compile(new StringReader(stylesheet));
1429

    
1430
            // Create a transformer for the stylesheet.
1431
            XsltTransformer transformer = exec.Load();
1432

    
1433
            // Set the root node of the source document to be the initial context node
1434
            transformer.InitialContextNode = input;
1435

    
1436
            // Establish the result document handler
1437
            Hashtable results = new Hashtable();
1438
            transformer.ResultDocumentHandler = new UserResultDocumentHandler(results);
1439

    
1440
            // Transform the source XML to a NullDestination (because we only want the secondary result files).
1441
            transformer.Run(new NullDestination());
1442

    
1443
            // Process the captured DOM results
1444
            foreach (DictionaryEntry entry in results)
1445
            {
1446
                string uri = (string)entry.Key;
1447
                Console.WriteLine("\nResult File " + uri);
1448
                DomDestination dom = (DomDestination)results[uri];
1449
                Console.Write(dom.XmlDocument.OuterXml);
1450
            }
1451

    
1452
        }
1453

    
1454
    }
1455

    
1456
    /// <summary>
1457
    /// Show a transformation using a registered collection
1458
    /// </summary>
1459

    
1460
    public class XsltUsingRegisteredCollection : Example
1461
    {
1462

    
1463
        public override string testName
1464
        {
1465
            get { return "XsltUsingRegisteredCollection"; }
1466
        }
1467

    
1468
        /// <summary>
1469
        /// Show a transformation using a registered collection
1470
        /// </summary>
1471

    
1472
        public override void run(Uri samplesDir)
1473
        {
1474
            // Create a Processor instance.
1475
            Processor processor = new Processor();
1476

    
1477
            // Load the source document
1478
            XdmNode input = processor.NewDocumentBuilder().Build(new Uri(samplesDir, "data/othello.xml"));
1479

    
1480
            // Define a stylesheet that splits the document up
1481
            String stylesheet =
1482
                "<xsl:stylesheet xmlns:xsl='http://www.w3.org/1999/XSL/Transform' version='2.0'>\n" +
1483
                "<xsl:template name='main'>\n" +
1484
                " <out>\n" +
1485
                "  <xsl:for-each select=\"collection('http://www.example.org/my-collection')\">\n" +
1486
                "    <document uri='{document-uri(.)}' nodes='{count(//*)}'/>\n" +
1487
                "  </xsl:for-each><zzz/>\n" +
1488
                "  <xsl:for-each select=\"collection('http://www.example.org/my-collection')\">\n" +
1489
                "    <document uri='{document-uri(.)}' nodes='{count(//*)}'/>\n" +
1490
                "  </xsl:for-each>\n" +
1491
                " </out>\n" +
1492
                "</xsl:template>\n" +
1493
                "</xsl:stylesheet>";
1494

    
1495
            Uri[] documentList = new Uri[2];
1496
            documentList[0] = new Uri(samplesDir, "data/othello.xml");
1497
            documentList[1] = new Uri(samplesDir, "data/books.xml");
1498
            processor.RegisterCollection(new Uri("http://www.example.org/my-collection"), documentList);
1499

    
1500
            XsltCompiler compiler = processor.NewXsltCompiler();
1501
            compiler.BaseUri = new Uri("http://localhost/stylesheet");
1502
            XsltExecutable exec = compiler.Compile(new StringReader(stylesheet));
1503

    
1504
            // Create a transformer for the stylesheet.
1505
            XsltTransformer transformer = exec.Load();
1506

    
1507
            // Set the root node of the source document to be the initial context node
1508
            transformer.InitialTemplate = new QName("", "main");
1509

    
1510
            //Set the destination
1511
            XdmDestination results = new XdmDestination();
1512

    
1513
            // Transform the XML
1514
            transformer.Run(results);
1515

    
1516
            // Show the result
1517
            Console.WriteLine(results.XdmNode.ToString());
1518

    
1519
        }
1520
    }
1521

    
1522
    /// <summary>
1523
    /// Show a transformation using a registered collection
1524
    /// </summary>
1525

    
1526
    public class XsltUsingDirectoryCollection : Example
1527
    {
1528

    
1529
        public override string testName
1530
        {
1531
            get { return "XsltUsingDirectoryCollection"; }
1532
        }
1533

    
1534
        /// <summary>
1535
        /// Show a transformation using a collection that maps to a directory
1536
        /// </summary>
1537

    
1538
        public override void run(Uri samplesDir)
1539
        {
1540
            // Create a Processor instance.
1541
            Processor processor = new Processor();
1542

    
1543
            // Load the source document
1544
            XdmNode input = processor.NewDocumentBuilder().Build(new Uri(samplesDir, "data/othello.xml"));
1545

    
1546
            // Define a stylesheet that splits the document up
1547
            String stylesheet =
1548
                "<xsl:stylesheet xmlns:xsl='http://www.w3.org/1999/XSL/Transform' version='2.0'>\n" +
1549
                "<xsl:template name='main'>\n" +
1550
                " <out>\n" +
1551
                "  <xsl:for-each select=\"collection('" + samplesDir + "?recurse=yes;select=*.xml;on-error=warning')\">\n" +
1552
                "    <document uri='{document-uri(.)}' nodes='{count(//*)}'/>\n" +
1553
                "  </xsl:for-each><zzz/>\n" +
1554
                " </out>\n" +
1555
                "</xsl:template>\n" +
1556
                "</xsl:stylesheet>";
1557

    
1558

    
1559
            XsltCompiler compiler = processor.NewXsltCompiler();
1560
            compiler.BaseUri = new Uri("http://localhost/stylesheet");
1561
            XsltExecutable exec = compiler.Compile(new StringReader(stylesheet));
1562

    
1563
            // Create a transformer for the stylesheet.
1564
            XsltTransformer transformer = exec.Load();
1565

    
1566
            // Set the root node of the source document to be the initial context node
1567
            transformer.InitialTemplate = new QName("", "main");
1568

    
1569
            //Set the destination
1570
            XdmDestination results = new XdmDestination();
1571

    
1572
            // Transform the XML
1573
            transformer.Run(results);
1574

    
1575
            // Show the result
1576
            Console.WriteLine(results.XdmNode.ToString());
1577

    
1578
        }
1579

    
1580
    }
1581

    
1582

    
1583

    
1584
    /// <summary>
1585
    /// Show a transformation using calls to extension functions
1586
    /// </summary>
1587

    
1588
    public class XsltIntegratedExtension : Example
1589
    {
1590

    
1591
        public override string testName
1592
        {
1593
            get { return "XsltIntegratedExtension"; }
1594
        }
1595

    
1596
        /// <summary>
1597
        /// Show a transformation using calls to extension functions
1598
        /// </summary>
1599

    
1600
        public override void run(Uri samplesDir)
1601
        {
1602

    
1603
            // Create a Processor instance.
1604
            Processor processor = new Processor();
1605

    
1606
            // Identify the Processor version
1607
            Console.WriteLine(processor.ProductVersion);
1608

    
1609
            // Set diagnostics
1610
            //processor.SetProperty("http://saxon.sf.net/feature/trace-external-functions", "true");
1611

    
1612
            // Create the stylesheet
1613
            String s = @"<xsl:transform version='2.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform'" +
1614
                @" xmlns:math='http://example.math.co.uk/demo'> " +
1615
                @" <xsl:template name='go'> " +
1616
                @" <out sqrt2='{math:sqrt(2.0e0)}' " +
1617
                @" sqrtEmpty='{math:sqrt(())}'/> " +
1618
                @" </xsl:template></xsl:transform>";
1619

    
1620
            // Register the integrated extension function math:sqrt
1621

    
1622
            processor.RegisterExtensionFunction(new Sqrt());
1623

    
1624
            // Create a transformer for the stylesheet.
1625
            XsltTransformer transformer = processor.NewXsltCompiler().Compile(new StringReader(s)).Load();
1626

    
1627
            // Set the root node of the source document to be the initial context node
1628
            transformer.InitialTemplate = new QName("go");
1629

    
1630
            // Create a serializer
1631
            Serializer serializer = new Serializer();
1632
            serializer.SetOutputWriter(Console.Out);
1633
            serializer.SetOutputProperty(Serializer.INDENT, "yes");
1634

    
1635
            // Transform the source XML to System.out.
1636
            transformer.Run(serializer);
1637
        }
1638

    
1639
    }
1640

    
1641
    /// <summary>
1642
    /// Example extension function to compute a square root.
1643
    /// </summary>
1644

    
1645
    public class Sqrt : ExtensionFunctionDefinition
1646
    {
1647
        public override QName FunctionName
1648
        {
1649
            get
1650
            {
1651
                return new QName("http://example.math.co.uk/demo", "sqrt");
1652
            }
1653
        }
1654

    
1655
        public override int MinimumNumberOfArguments
1656
        {
1657
            get
1658
            {
1659
                return 1;
1660
            }
1661
        }
1662

    
1663
        public override int MaximumNumberOfArguments
1664
        {
1665
            get
1666
            {
1667
                return 1;
1668
            }
1669
        }
1670

    
1671
        public override XdmSequenceType[] ArgumentTypes
1672
        {
1673
            get
1674
            {
1675
                return new XdmSequenceType[]{
1676
                    new XdmSequenceType(XdmAtomicType.BuiltInAtomicType(QName.XS_DOUBLE), '?')
1677
                };
1678
            }
1679
        }
1680

    
1681
        public override XdmSequenceType ResultType(XdmSequenceType[] ArgumentTypes)
1682
        {
1683
            return new XdmSequenceType(XdmAtomicType.BuiltInAtomicType(QName.XS_DOUBLE), '?');
1684
        }
1685

    
1686
        public override bool TrustResultType
1687
        {
1688
            get
1689
            {
1690
                return true;
1691
            }
1692
        }
1693

    
1694

    
1695
        public override ExtensionFunctionCall MakeFunctionCall()
1696
        {
1697
            return new SqrtCall();
1698
        }
1699
    }
1700

    
1701
    internal class SqrtCall : ExtensionFunctionCall
1702
    {
1703
        public override IXdmEnumerator Call(IXdmEnumerator[] arguments, DynamicContext context)
1704
        {
1705
            Boolean exists = arguments[0].MoveNext();
1706
            if (exists)
1707
            {
1708
                XdmAtomicValue arg = (XdmAtomicValue)arguments[0].Current;
1709
                double val = (double)arg.Value;
1710
                double sqrt = System.Math.Sqrt(val);
1711
                XdmAtomicValue result = new XdmAtomicValue(sqrt);
1712
                return (IXdmEnumerator)result.GetEnumerator();
1713
            }
1714
            else
1715
            {
1716
                return EmptyEnumerator.INSTANCE;
1717
            }
1718
        }
1719

    
1720
    }
1721

    
1722
    /// <summary>
1723
    /// Example extension function to return the default namespace from the static context
1724
    /// </summary>
1725

    
1726
    public class DefaultNamespace : ExtensionFunctionDefinition
1727
    {
1728
        public override QName FunctionName
1729
        {
1730
            get
1731
            {
1732
                return new QName("http://example.env.co.uk/demo", "defaultNamespace");
1733
            }
1734
        }
1735

    
1736
        public override int MinimumNumberOfArguments
1737
        {
1738
            get
1739
            {
1740
                return 0;
1741
            }
1742
        }
1743

    
1744
        public override int MaximumNumberOfArguments
1745
        {
1746
            get
1747
            {
1748
                return 0;
1749
            }
1750
        }
1751

    
1752
        public override XdmSequenceType[] ArgumentTypes
1753
        {
1754
            get
1755
            {
1756
                return new XdmSequenceType[] { };
1757
            }
1758
        }
1759

    
1760
        public override bool DependsOnFocus
1761
        {
1762
            get
1763
            {
1764
                return true;
1765
                // actually it depends on the static context rather than the focus; but returning true is necessary
1766
                // to avoid the call being extracted to a global variable.
1767
            }
1768
        }
1769

    
1770
        public override XdmSequenceType ResultType(XdmSequenceType[] ArgumentTypes)
1771
        {
1772
            return new XdmSequenceType(XdmAtomicType.BuiltInAtomicType(QName.XS_STRING), '?');
1773
        }
1774

    
1775
        public override bool TrustResultType
1776
        {
1777
            get
1778
            {
1779
                return true;
1780
            }
1781
        }
1782

    
1783

    
1784
        public override ExtensionFunctionCall MakeFunctionCall()
1785
        {
1786
            return new DefaultNamespaceCall();
1787
        }
1788
    }
1789

    
1790
    internal class DefaultNamespaceCall : ExtensionFunctionCall
1791
    {
1792
        private string defaultNamespace;
1793

    
1794
        public override void SupplyStaticContext(StaticContext context)
1795
        {
1796
            defaultNamespace = context.GetNamespaceForPrefix("");
1797
        }
1798

    
1799
        public override IXdmEnumerator Call(IXdmEnumerator[] arguments, DynamicContext context)
1800
        {
1801
            if (defaultNamespace != null)
1802
            {
1803
                return (IXdmEnumerator)new XdmAtomicValue(defaultNamespace).GetEnumerator();
1804
            }
1805
            else
1806
            {
1807
                return EmptyEnumerator.INSTANCE;
1808
            }
1809
        }
1810

    
1811
    }
1812

    
1813
    /// <summary>
1814
    /// Show a query producing a document as its result and serializing this to a FileStream
1815
    /// </summary>
1816

    
1817
    public class XQueryToStream : Example
1818
    {
1819

    
1820
        public override string testName
1821
        {
1822
            get { return "XQueryToStream"; }
1823
        }
1824

    
1825
        /// <summary>
1826
        /// Show a query producing a document as its result and serializing this to a FileStream
1827
        /// </summary>
1828

    
1829
        public override void run(Uri samplesDir)
1830
        {
1831
            Processor processor = new Processor();
1832
            XQueryCompiler compiler = processor.NewXQueryCompiler();
1833
            compiler.BaseUri = samplesDir.ToString();
1834
            compiler.DeclareNamespace("saxon", "http://saxon.sf.net/");
1835
            XQueryExecutable exp = compiler.Compile("<saxon:example>{static-base-uri()}</saxon:example>");
1836
            XQueryEvaluator eval = exp.Load();
1837
            Serializer qout = new Serializer();
1838
            qout.SetOutputProperty(Serializer.METHOD, "xml");
1839
            qout.SetOutputProperty(Serializer.INDENT, "yes");
1840
            qout.SetOutputStream(new FileStream("testoutput.xml", FileMode.Create, FileAccess.Write));
1841
            Console.WriteLine("Output written to testoutput.xml");
1842
            eval.Run(qout);
1843
        }
1844

    
1845
    }
1846

    
1847
    /// <summary>
1848
    /// Show a query producing a single atomic value as its result and returning the value
1849
    /// to the C# application
1850
    /// </summary>
1851

    
1852
    public class XQueryToAtomicValue : Example
1853
    {
1854

    
1855
        public override string testName
1856
        {
1857
            get { return "XQueryToAtomicValue"; }
1858
        }
1859

    
1860
        /// <summary>
1861
        /// Show a query producing a single atomic value as its result and returning the value
1862
        /// to the C# application
1863
        /// </summary>
1864

    
1865
        public override void run(Uri samplesDir)
1866
        {
1867
            Processor processor = new Processor();
1868
            XQueryCompiler compiler = processor.NewXQueryCompiler();
1869
            XQueryExecutable exp = compiler.Compile("avg(for $i in 1 to 10 return $i * $i)");
1870
            XQueryEvaluator eval = exp.Load();
1871
            XdmAtomicValue result = (XdmAtomicValue)eval.EvaluateSingle();
1872
            Console.WriteLine("Result type: " + result.Value.GetType());
1873
            Console.WriteLine("Result value: " + (decimal)result.Value);
1874
        }
1875

    
1876
    }
1877

    
1878
    /// <summary>
1879
    /// Show a query producing a DOM as its input and producing a DOM as its output
1880
    /// </summary>
1881

    
1882
    public class XQueryToDom : Example
1883
    {
1884

    
1885
        public override string testName
1886
        {
1887
            get { return "XQueryToDom"; }
1888
        }
1889

    
1890
        /// <summary>
1891
        /// Show a query producing a DOM as its input and producing a DOM as its output
1892
        /// </summary>
1893

    
1894
        public override void run(Uri samplesDir)
1895
        {
1896
            Processor processor = new Processor();
1897

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

    
1902
            XQueryCompiler compiler = processor.NewXQueryCompiler();
1903
            XQueryExecutable exp = compiler.Compile("<doc>{reverse(/*/*)}</doc>");
1904
            XQueryEvaluator eval = exp.Load();
1905
            eval.ContextItem = indoc;
1906
            DomDestination qout = new DomDestination();
1907
            eval.Run(qout);
1908
            XmlDocument outdoc = qout.XmlDocument;
1909
            Console.WriteLine(outdoc.OuterXml);
1910
        }
1911

    
1912
    }
1913

    
1914
    /// <summary>
1915
    /// Show a query producing a Saxon tree as its input and producing a Saxon tree as its output
1916
    /// </summary>
1917

    
1918
    public class XQueryToXdm : Example
1919
    {
1920

    
1921
        public override string testName
1922
        {
1923
            get { return "XQueryToXdm"; }
1924
        }
1925

    
1926
        /// <summary>
1927
        /// Show a query producing a Saxon tree as its input and producing a Saxon tree as its output
1928
        /// </summary>
1929

    
1930
        public override void run(Uri samplesDir)
1931
        {
1932
            Processor processor = new Processor();
1933

    
1934
            DocumentBuilder loader = processor.NewDocumentBuilder();
1935
            loader.BaseUri = new Uri(samplesDir, "data/books.xml");
1936
            XdmNode indoc = loader.Build(loader.BaseUri);
1937

    
1938
            XQueryCompiler compiler = processor.NewXQueryCompiler();
1939
            XQueryExecutable exp = compiler.Compile("<doc>{reverse(/*/*)}</doc>");
1940
            XQueryEvaluator eval = exp.Load();
1941
            eval.ContextItem = indoc;
1942
            XdmDestination qout = new XdmDestination();
1943
            eval.Run(qout);
1944
            XdmNode outdoc = qout.XdmNode;
1945
            Console.WriteLine(outdoc.OuterXml);
1946
        }
1947

    
1948
    }
1949

    
1950
    /// <summary>
1951
    /// Show a query making a direct call to a user-defined function defined in the query
1952
    /// </summary>
1953

    
1954
    public class XQueryCallFunction : Example
1955
    {
1956

    
1957
        public override string testName
1958
        {
1959
            get { return "XQueryCallFunction"; }
1960
        }
1961

    
1962
        /// <summary>
1963
        /// Show a direct call on a user-defined function defined within the query
1964
        /// </summary>
1965

    
1966
        public override void run(Uri samplesDir)
1967
        {
1968
            Processor processor = new Processor();
1969

    
1970
            XQueryCompiler qc = processor.NewXQueryCompiler();
1971
            XQueryExecutable exp1 = qc.Compile("declare namespace f='f.ns';" +
1972
                   "declare variable $z := 1 + xs:integer(doc-available('" + new Uri(samplesDir, "data/books.xml").ToString() + "'));" +
1973
                   "declare variable $p as xs:integer external;" +
1974
                   "declare function f:t1($v1 as xs:integer) { " +
1975
                   "   $v1 div $z + $p" +
1976
                   "};" +
1977
                   "10");
1978
            XQueryEvaluator ev = exp1.Load();
1979
            ev.SetExternalVariable(new QName("", "p"), new XdmAtomicValue(39));
1980
            XdmValue v1 = new XdmAtomicValue(10);
1981
            XdmValue result = ev.CallFunction(new QName("f.ns", "f:t1"), new XdmValue[] { v1 });
1982
            Console.WriteLine("First result (expected 44): " + result.ToString());
1983
            v1 = new XdmAtomicValue(20);
1984
            result = ev.CallFunction(new QName("f.ns", "f:t1"), new XdmValue[] { v1 });
1985
            Console.WriteLine("Second result (expected 49): " + result.ToString());
1986
        }
1987

    
1988
    }
1989

    
1990

    
1991

    
1992
    /// <summary>
1993
    /// Show a query producing a sequence as its result and returning the sequence
1994
    /// to the C# application in the form of an iterator. For each item in the
1995
    /// result, its string value is output.
1996
    /// </summary>
1997

    
1998
    public class XQueryToSequence : Example
1999
    {
2000

    
2001
        public override string testName
2002
        {
2003
            get { return "XQueryToSequence"; }
2004
        }
2005

    
2006
        /// <summary>
2007
        /// Show a query producing a sequence as its result and returning the sequence
2008
        /// to the C# application in the form of an iterator. For each item in the
2009
        /// result, its string value is output.
2010
        /// </summary>
2011

    
2012
        public override void run(Uri samplesDir)
2013
        {
2014
            Processor processor = new Processor();
2015
            XQueryCompiler compiler = processor.NewXQueryCompiler();
2016
            XQueryExecutable exp = compiler.Compile("for $i in 1 to 10 return $i * $i");
2017
            XQueryEvaluator eval = exp.Load();
2018
            XdmValue value = eval.Evaluate();
2019
            IEnumerator e = value.GetEnumerator();
2020
            while (e.MoveNext())
2021
            {
2022
                XdmItem item = (XdmItem)e.Current;
2023
                Console.WriteLine(item.ToString());
2024
            }
2025

    
2026
        }
2027

    
2028
    }
2029

    
2030
    /// <summary>
2031
    /// Show a query reading an input document using an XmlReader (the .NET XML parser)
2032
    /// </summary>
2033

    
2034
    public class XQueryFromXmlReader : Example
2035
    {
2036

    
2037
        public override string testName
2038
        {
2039
            get { return "XQueryFromXmlReader"; }
2040
        }
2041

    
2042
        /// <summary>
2043
        /// Show a query reading an input document using an XmlReader (the .NET XML parser)
2044
        /// </summary>
2045

    
2046
        public override void run(Uri samplesDir)
2047
        {
2048
            Processor processor = new Processor();
2049

    
2050
            String inputFileName = new Uri(samplesDir, "data/books.xml").ToString();
2051
            XmlTextReader reader = new XmlTextReader(inputFileName,
2052
                UriConnection.getReadableUriStream(new Uri(samplesDir, "data/books.xml")));
2053
            //new FileStream(inputFileName, FileMode.Open, FileAccess.Read));
2054
            reader.Normalization = true;
2055

    
2056
            // add a validating reader - not to perform validation, but to expand entity references
2057
            XmlValidatingReader validator = new XmlValidatingReader(reader);
2058
            validator.ValidationType = ValidationType.None;
2059

    
2060
            XdmNode doc = processor.NewDocumentBuilder().Build(validator);
2061

    
2062
            XQueryCompiler compiler = processor.NewXQueryCompiler();
2063
            XQueryExecutable exp = compiler.Compile("/");
2064
            XQueryEvaluator eval = exp.Load();
2065
            eval.ContextItem = doc;
2066
            Serializer qout = new Serializer();
2067
            qout.SetOutputProperty(Serializer.METHOD, "xml");
2068
            qout.SetOutputProperty(Serializer.INDENT, "yes");
2069
            qout.SetOutputStream(new FileStream("testoutput2.xml", FileMode.Create, FileAccess.Write));
2070
            eval.Run(qout);
2071
        }
2072

    
2073
    }
2074

    
2075
    /// <summary>
2076
    /// Show a query producing a sequence as its result and returning the sequence
2077
    /// to the C# application in the form of an iterator. The sequence is then
2078
    /// output by serializing each item individually, with each item on a new line.
2079
    /// </summary>
2080

    
2081
    public class XQueryToSerializedSequence : Example
2082
    {
2083

    
2084
        public override string testName
2085
        {
2086
            get { return "XQueryToSerializedSequence"; }
2087
        }
2088

    
2089
        /// <summary>
2090
        /// Show a query producing a sequence as its result and returning the sequence
2091
        /// to the C# application in the form of an iterator. The sequence is then
2092
        /// output by serializing each item individually, with each item on a new line.
2093
        /// </summary>
2094

    
2095
        public override void run(Uri samplesDir)
2096
        {
2097
            Processor processor = new Processor();
2098
            String inputFileName = new Uri(samplesDir, "data/books.xml").ToString();
2099
            //XmlTextReader reader = new XmlTextReader(inputFileName,
2100
            //    new FileStream(inputFileName, FileMode.Open, FileAccess.Read));
2101
            XmlTextReader reader = new XmlTextReader(inputFileName,
2102
                UriConnection.getReadableUriStream(new Uri(samplesDir, "data/books.xml")));
2103
            reader.Normalization = true;
2104

    
2105
            // add a validating reader - not to perform validation, but to expand entity references
2106
            XmlValidatingReader validator = new XmlValidatingReader(reader);
2107
            validator.ValidationType = ValidationType.None;
2108

    
2109
            XdmNode doc = processor.NewDocumentBuilder().Build(reader);
2110

    
2111
            XQueryCompiler compiler = processor.NewXQueryCompiler();
2112
            XQueryExecutable exp = compiler.Compile("//ISBN");
2113
            XQueryEvaluator eval = exp.Load();
2114
            eval.ContextItem = doc;
2115

    
2116
            foreach (XdmNode node in eval)
2117
            {
2118
                Console.WriteLine(node.OuterXml);
2119
            }
2120
        }
2121

    
2122
    }
2123

    
2124
    /// <summary>
2125
    /// Show a query that takes a parameter (external variable) as input.
2126
    /// The query produces a single atomic value as its result and returns the value
2127
    /// to the C# application. 
2128
    /// </summary>
2129

    
2130
    public class XQueryUsingParameter : Example
2131
    {
2132

    
2133
        public override string testName
2134
        {
2135
            get { return "XQueryUsingParameter"; }
2136
        }
2137

    
2138
        /// <summary>
2139
        /// Show a query that takes a parameter (external variable) as input.
2140
        /// The query produces a single atomic value as its result and returns the value
2141
        /// to the C# application. 
2142
        /// </summary>
2143

    
2144
        public override void run(Uri samplesDir)
2145
        {
2146
            Processor processor = new Processor();
2147
            XQueryCompiler compiler = processor.NewXQueryCompiler();
2148
            compiler.DeclareNamespace("p", "http://saxon.sf.net/ns/p");
2149
            XQueryExecutable exp = compiler.Compile(
2150
                    "declare variable $p:in as xs:integer external; $p:in * $p:in");
2151
            XQueryEvaluator eval = exp.Load();
2152
            eval.SetExternalVariable(new QName("http://saxon.sf.net/ns/p", "p:in"), new XdmAtomicValue(12));
2153
            XdmAtomicValue result = (XdmAtomicValue)eval.EvaluateSingle();
2154
            Console.WriteLine("Result type: " + result.Value.GetType());
2155
            Console.WriteLine("Result value: " + (long)result.Value);
2156
        }
2157

    
2158
    }
2159

    
2160
    /// <summary>
2161
    /// Show a query consisting of two modules, using a QueryResolver to resolve
2162
    /// the "import module" declaration
2163
    /// </summary>
2164

    
2165
    public class XQueryMultiModule : Example
2166
    {
2167

    
2168
        public override string testName
2169
        {
2170
            get { return "XQueryMultiModule"; }
2171
        }
2172

    
2173
        /// <summary>
2174
        /// Show a query consisting of two modules, using a QueryResolver to resolve
2175
        /// the "import module" declaration
2176
        /// </summary>
2177

    
2178
        public override void run(Uri samplesDir)
2179
        {
2180

    
2181
            String mod1 = "import module namespace m2 = 'http://www.example.com/module2';" +
2182
                          "m2:square(3)";
2183

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

    
2187
            Processor processor = new Processor();
2188
            XQueryCompiler compiler = processor.NewXQueryCompiler();
2189

    
2190
            InlineModuleResolver resolver = new InlineModuleResolver();
2191
            resolver.AddModule(new Uri("http://www.example.com/module2"), mod2);
2192
            compiler.QueryResolver = resolver;
2193
            XQueryExecutable exp = compiler.Compile(mod1);
2194
            XQueryEvaluator eval = exp.Load();
2195

    
2196
            XdmAtomicValue result = (XdmAtomicValue)eval.EvaluateSingle();
2197
            Console.WriteLine("Result type: " + result.Value.GetType());
2198
            Console.WriteLine("Result value: " + (long)result.Value);
2199
        }
2200

    
2201
        // A simple QueryResolver designed to show that the actual query
2202
        // text can come from anywhere: in this case, the resolver maintains
2203
        // a simple mapping of module URIs onto strings.
2204

    
2205
        public class InlineModuleResolver : IQueryResolver
2206
        {
2207

    
2208
            private Hashtable modules = new Hashtable();
2209

    
2210
            public void AddModule(Uri moduleName, String moduleText)
2211
            {
2212
                modules.Add(moduleName, moduleText);
2213
            }
2214

    
2215
            public Uri[] GetModules(String moduleUri, Uri baseUri, String[] locationHints)
2216
            {
2217
                Uri[] result = { new Uri(moduleUri) };
2218
                return result;
2219
            }
2220

    
2221
            public Object GetEntity(Uri absoluteUri)
2222
            {
2223
                return modules[absoluteUri];
2224
            }
2225
        }
2226

    
2227
    }
2228

    
2229

    
2230
    public class UriConnection
2231
    {
2232

    
2233
        // Get a stream for reading from a file:// URI
2234

    
2235
        public static Stream getReadableUriStream(Uri uri)
2236
        {
2237
            WebRequest request = (WebRequest)WebRequest.Create(uri);
2238
            return request.GetResponse().GetResponseStream();
2239
        }
2240

    
2241
        // Get a stream for writing to a file:// URI
2242

    
2243
        public static Stream getWritableUriStream(Uri uri)
2244
        {
2245
            FileWebRequest request = (FileWebRequest)WebRequest.CreateDefault(uri);
2246
            request.Method = "POST";
2247
            return request.GetRequestStream();
2248
        }
2249
    }
2250

    
2251
    ///
2252
    /// A sample XmlResolver. In the case of a URI ending with ".txt", it returns the
2253
    /// URI itself, wrapped as an XML document. In the case of the URI "empty.xslt", it returns an empty
2254
    /// stylesheet. In all other cases, it returns null, which has the effect of delegating
2255
    /// processing to the standard XmlResolver.
2256
    ///
2257

    
2258
    public class UserXmlResolver : XmlUrlResolver
2259
    {
2260

    
2261
        public String Message = null;
2262

    
2263
        public override object GetEntity(Uri absoluteUri, String role, Type ofObjectToReturn)
2264
        {
2265
            if (Message != null)
2266
            {
2267
                Console.WriteLine(Message + absoluteUri + " (role=" + role + ")");
2268
            }
2269

    
2270
            if (absoluteUri.ToString().EndsWith(".txt"))
2271
            {
2272
                MemoryStream ms = new MemoryStream();
2273
                StreamWriter tw = new StreamWriter(ms);
2274
                tw.Write("<uri>");
2275
                tw.Write(absoluteUri);
2276
                tw.Write("</uri>");
2277
                tw.Flush();
2278
                return new MemoryStream(ms.GetBuffer(), 0, (int)ms.Length);
2279
            }
2280
            if (absoluteUri.ToString().EndsWith("empty.xslt"))
2281
            {
2282
                String ss = "<transform xmlns='http://www.w3.org/1999/XSL/Transform' version='2.0'/>";
2283
                MemoryStream ms = new MemoryStream();
2284
                StreamWriter tw = new StreamWriter(ms);
2285
                tw.Write(ss);
2286
                tw.Flush();
2287
                return new MemoryStream(ms.GetBuffer(), 0, (int)ms.Length);
2288
            }
2289
            else
2290
            {
2291
                return null;
2292
            }
2293
        }
2294
    }
2295

    
2296
    public class UserResultDocumentHandler : IResultDocumentHandler
2297
    {
2298

    
2299
        private Hashtable results;
2300

    
2301
        public UserResultDocumentHandler(Hashtable table)
2302
        {
2303
            this.results = table;
2304
        }
2305

    
2306
        public XmlDestination HandleResultDocument(string href, Uri baseUri)
2307
        {
2308
            DomDestination destination = new DomDestination();
2309
            results[href] = destination;
2310
            return destination;
2311
        }
2312

    
2313
    }
2314
}
2315

    
2316

    
2317
//
2318
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
2319
// you may not use this file except in compliance with the License. You may obtain a copy of the
2320
// License at http://www.mozilla.org/MPL/
2321
//
2322
// Software distributed under the License is distributed on an "AS IS" basis,
2323
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
2324
// See the License for the specific language governing rights and limitations under the License.
2325
//
2326
// The Original Code is: all this file.
2327
//
2328
// The Initial Developer of the Original Code is Michael H. Kay.
2329
//
2330
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
2331
//
2332
// Contributor(s): none.
2333
//
2334

    
2335

    
2336

    
2337

    
(3-3/14)