Project

Profile

Help

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

he / tags / 9.6.0.7 / hen / csource / samples / ExamplesHE.cs @ aa733b18

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
        /// Evaluate an XPath expression against a source document
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 returning effective boolean value
252
    /// </summary>
253

    
254
    public class XPathSimple2 : Example
255
    {
256

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

    
262
        /// <summary>
263
        /// Evaluate an XPath expression against a source document, returning its
264
        /// effective boolean value
265
        /// </summary>
266

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

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

    
275
            // Create an XPath compiler
276
            XPathCompiler xpath = processor.NewXPathCompiler();
277

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

    
286
        }
287
    }
288

    
289

    
290
    /// <summary>
291
    /// XPath expression using variables (and no source document)
292
    /// </summary>
293

    
294
    public class XPathVariables : Example
295
    {
296

    
297
        public override String testName
298
        {
299
            get { return "XPathVariables"; }
300
        }
301

    
302
        /// <summary>
303
        /// Evaluate an XPath expression using variables
304
        /// </summary>
305

    
306
        public override void run(Uri samplesDir)
307
        {
308
            // Create a Processor instance.
309
            Processor processor = new Processor();
310

    
311
            // Create the XPath expression.
312
            XPathCompiler compiler = processor.NewXPathCompiler();
313
            compiler.DeclareVariable(new QName("", "a"));
314
            compiler.DeclareVariable(new QName("", "b"));
315
            XPathSelector selector = compiler.Compile("$a + $b").Load();
316

    
317
            // Set the values of the variables
318
            selector.SetVariable(new QName("", "a"), new XdmAtomicValue(2));
319
            selector.SetVariable(new QName("", "b"), new XdmAtomicValue(3));
320

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

    
326
    /// <summary>
327
    /// XPath expression using variables without explicit declaration
328
    /// </summary>
329

    
330
    public class XPathUndeclaredVariables : Example
331
    {
332

    
333
        public override String testName
334
        {
335
            get { return "XPathUndeclaredVariables"; }
336
        }
337

    
338
        /// <summary>
339
        /// Execute an XPath expression containing undeclared variables
340
        /// </summary>
341

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

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

    
353
            // Set the values of the variables
354
            IEnumerator vars = expression.EnumerateExternalVariables();
355
            while (vars.MoveNext())
356
            {
357
                selector.SetVariable((QName)vars.Current, new XdmAtomicValue(10));
358
            }
359

    
360
            // Evaluate the XPath expression
361
            Console.WriteLine(selector.EvaluateSingle().ToString());
362
        }
363
    }
364

    
365
    /// <summary>
366
    /// XPath expression throwing a static error
367
    /// </summary>
368

    
369
    public class XPathWithStaticError : Example
370
    {
371

    
372
        public override String testName
373
        {
374
            get { return "XPathWithStaticError"; }
375
        }
376

    
377
        /// <summary>
378
        /// Execute an XPath expression that throws a dynamic error, and catch the error
379
        /// </summary>
380

    
381
        public override void run(Uri samplesDir)
382
        {
383
            // Create a Processor instance.
384
            Processor processor = new Processor();
385

    
386
            // Create the XPath expression.
387
            XPathCompiler compiler = processor.NewXPathCompiler();
388
            compiler.AllowUndeclaredVariables = true;
389
            XPathExecutable expression = compiler.Compile("1 + unknown()");
390
            XPathSelector selector = expression.Load();
391

    
392
            // Evaluate the XPath expression
393
            Console.WriteLine(selector.EvaluateSingle().ToString());
394
        }
395
    }
396

    
397
    /// <summary>
398
    /// XPath expression throwing a dynamic error
399
    /// </summary>
400

    
401
    public class XPathWithDynamicError : Example
402
    {
403

    
404
        public override String testName
405
        {
406
            get { return "XPathWithDynamicError"; }
407
        }
408

    
409
        /// <summary>
410
        /// Execute an XPath expression that throws a dynamic error
411
        /// </summary>
412

    
413
        public override void run(Uri samplesDir)
414
        {
415
            // Create a Processor instance.
416
            Processor processor = new Processor();
417

    
418
            // Create the XPath expression.
419
            XPathCompiler compiler = processor.NewXPathCompiler();
420
            compiler.AllowUndeclaredVariables = true;
421
            XPathExecutable expression = compiler.Compile("$a gt $b");
422
            XPathSelector selector = expression.Load();
423

    
424
            // Set the values of the variables
425
            selector.SetVariable(new QName("", "a"), new XdmAtomicValue(10));
426
            selector.SetVariable(new QName("", "b"), new XdmAtomicValue("Paris"));
427

    
428
            // Evaluate the XPath expression
429
            Console.WriteLine(selector.EvaluateSingle().ToString());
430
        }
431
    }
432

    
433
    /// <summary>
434
    /// XSLT 2.0 transformation with source document and stylesheet supplied as URIs
435
    /// </summary>
436

    
437
    public class XsltSimple1 : Example
438
    {
439

    
440
        public override String testName
441
        {
442
            get { return "XsltSimple1"; }
443
        }
444

    
445
        /// <summary>
446
        /// Run a transformation: simplest possible script
447
        /// </summary>
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/books.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, with output to the standard output stream
464
            Serializer serializer = new Serializer();
465
            serializer.SetOutputWriter(Console.Out);
466

    
467
            // Transform the source XML and serialize the result document
468
            transformer.Run(serializer);
469
        }
470
    }
471

    
472
    /// <summary>
473
    /// Run a transformation, sending the serialized output to a file
474
    /// </summary>
475

    
476
    public class XsltSimple2 : Example
477
    {
478

    
479
        public override String testName
480
        {
481
            get { return "XsltSimple2"; }
482
        }
483

    
484
        /// <summary>
485
        /// Run the transformation, sending the serialized output to a file
486
        /// </summary>
487

    
488

    
489
        public override void run(Uri samplesDir)
490
        {
491
            // Create a Processor instance.
492
            Processor processor = new Processor();
493

    
494
            // Load the source document
495
            XdmNode input = processor.NewDocumentBuilder().Build(new Uri(samplesDir, "data/books.xml"));
496

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

    
500
            // Set the root node of the source document to be the initial context node
501
            transformer.InitialContextNode = input;
502

    
503
            // Create a serializer
504
            String outfile = "OutputFromXsltSimple2.xml";
505
            Serializer serializer = new Serializer();
506
            serializer.SetOutputStream(new FileStream(outfile, FileMode.Create, FileAccess.Write));
507

    
508
            // Transform the source XML to System.out.
509
            transformer.Run(serializer);
510

    
511
            Console.WriteLine("\nOutput written to " + outfile + "\n");
512
        }
513
    }
514

    
515
    /// <summary>
516
    /// XSLT 2.0 transformation with source document and stylesheet supplied as URIs
517
    /// </summary>
518

    
519
    public class XsltSimple3 : Example
520
    {
521

    
522
        public override String testName
523
        {
524
            get { return "XsltSimple3"; }
525
        }
526

    
527
        /// <summary>
528
        /// Run a transformation: supply input as a file
529
        /// </summary>
530

    
531
        public override void run(Uri samplesDir)
532
        {
533
            if (samplesDir.Scheme != Uri.UriSchemeFile)
534
            {
535
                Console.WriteLine("Supplied URI must be a file directory");
536
            }
537
            String dir = samplesDir.AbsolutePath;
538
            String sourceFile = dir + "data/books.xml";
539
            String styleFile = dir + "styles/books.xsl";
540

    
541
            // Create a Processor instance.
542
            Processor processor = new Processor();
543

    
544
            // Load the source document
545

    
546
            DocumentBuilder builder = processor.NewDocumentBuilder();
547
            builder.BaseUri = new Uri(samplesDir, "data/books.xml");
548

    
549
            XdmNode input = builder.Build(File.OpenRead(sourceFile));
550

    
551
            // Create a transformer for the stylesheet.
552
            XsltCompiler compiler = processor.NewXsltCompiler();
553
            compiler.BaseUri = new Uri(samplesDir, "styles/books.xsl");
554
            XsltTransformer transformer = compiler.Compile(File.OpenRead(styleFile)).Load();
555

    
556
            // Set the root node of the source document to be the initial context node
557
            transformer.InitialContextNode = input;
558

    
559
            // Create a serializer, with output to the standard output stream
560
            Serializer serializer = new Serializer();
561
            serializer.SetOutputWriter(Console.Out);
562

    
563
            // Transform the source XML and serialize the result document
564
            transformer.Run(serializer);
565
        }
566
    }
567

    
568

    
569
    /// <summary>
570
    /// XSLT 2.0 transformation showing stripping of whitespace controlled by the stylesheet
571
    /// </summary>
572

    
573
    public class XsltStripSpace : Example
574
    {
575

    
576
        public override String testName
577
        {
578
            get { return "XsltStripSpace"; }
579
        }
580

    
581
        /// <summary>
582
        /// Run a transformation: simplest possible script
583
        /// </summary>
584

    
585
        public override void run(Uri samplesDir)
586
        {
587
            Processor processor = new Processor();
588

    
589
            // Load the source document
590
            DocumentBuilder builder = processor.NewDocumentBuilder();
591
            builder.BaseUri = samplesDir;
592

    
593
            String doc = "<doc>  <a>  <b>text</b>  </a>  <a/>  </doc>";
594
            MemoryStream ms = new MemoryStream();
595
            StreamWriter tw = new StreamWriter(ms);
596
            tw.Write(doc);
597
            tw.Flush();
598
            Stream instr = new MemoryStream(ms.GetBuffer(), 0, (int)ms.Length);
599
            XdmNode input = builder.Build(instr);
600

    
601
            // Create a transformer for the stylesheet.
602
            String stylesheet =
603
                "<xsl:stylesheet xmlns:xsl='http://www.w3.org/1999/XSL/Transform' version='2.0'>" +
604
                "<xsl:strip-space elements='*'/>" +
605
                "<xsl:template match='/'>" +
606
                "  <xsl:copy-of select='.'/>" +
607
                "</xsl:template>" +
608
                "</xsl:stylesheet>";
609

    
610
            XsltCompiler compiler = processor.NewXsltCompiler();
611
            compiler.BaseUri = samplesDir;
612
            XsltTransformer transformer = compiler.Compile(new XmlTextReader(new StringReader(stylesheet))).Load();
613

    
614
            // Set the root node of the source document to be the initial context node
615
            transformer.InitialContextNode = input;
616

    
617
            // Create a serializer
618
            Serializer serializer = new Serializer();
619
            serializer.SetOutputWriter(Console.Out);
620

    
621
            // Transform the source XML to System.out.
622
            transformer.Run(serializer);
623
        }
624
    }
625

    
626

    
627
    /// <summary>
628
    /// Run a transformation, compiling the stylesheet once and using it to transform two different source documents
629
    /// </summary>
630

    
631
    public class XsltReuseExecutable : Example
632
    {
633

    
634
        public override String testName
635
        {
636
            get { return "XsltReuseExecutable"; }
637
        }
638

    
639
        /// <summary>
640
        /// Show that a stylesheet can be compiled once (into an XsltExecutable) and run many times
641
        /// </summary>
642
        /// <param name="fileNames">
643
        /// 1. first source document
644
        /// 2. second source document
645
        /// 3. stylesheet used to transform both documents
646
        /// </param>
647

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

    
653
            // Create a compiled stylesheet
654
            XsltExecutable templates = processor.NewXsltCompiler().Compile(new Uri(samplesDir, "styles/summarize.xsl"));
655

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

    
659
            String sourceFile1 = "data/books.xml";
660
            String sourceFile2 = "data/othello.xml";
661

    
662
            // Do the first transformation
663
            Console.WriteLine("\n\n----- transform of " + sourceFile1 + " -----");
664
            XsltTransformer transformer1 = templates.Load();
665
            transformer1.InitialContextNode = processor.NewDocumentBuilder().Build(new Uri(samplesDir, sourceFile1));
666
            transformer1.Run(new Serializer());     // default destination is Console.Out
667

    
668
            // Do the second transformation
669
            Console.WriteLine("\n\n----- transform of " + sourceFile2 + " -----");
670
            XsltTransformer transformer2 = templates.Load();
671
            transformer2.InitialContextNode = processor.NewDocumentBuilder().Build(new Uri(samplesDir, sourceFile2));
672
            transformer2.Run(new Serializer());     // default destination is Console.Out    
673
        }
674
    }
675

    
676
    /// <summary>
677
    /// Show that the XsltTransformer is serially reusable; run a transformation twice using the same stylesheet
678
    /// and the same input document but with different parameters.
679
    /// </summary>
680

    
681
    public class XsltReuseTransformer : Example
682
    {
683

    
684
        public override String testName
685
        {
686
            get { return "XsltReuseTransformer"; }
687
        }
688

    
689
        /// <summary>
690
        /// Show that the XsltTransformer is serially reusable (we run it twice with different parameter settings)
691
        /// </summary>
692

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

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

    
701
            // Compile the stylesheet
702
            XsltExecutable exec = processor.NewXsltCompiler().Compile(new Uri(samplesDir, "styles/summarize.xsl"));
703

    
704
            // Create a transformer 
705
            XsltTransformer transformer = exec.Load();
706

    
707
            // Run it once        
708
            transformer.SetParameter(new QName("", "", "include-attributes"), new XdmAtomicValue(false));
709
            transformer.InitialContextNode = input;
710
            XdmDestination results = new XdmDestination();
711
            transformer.Run(results);
712
            Console.WriteLine("1: " + results.XdmNode.OuterXml);
713

    
714
            // Run it again        
715
            transformer.SetParameter(new QName("", "", "include-attributes"), new XdmAtomicValue(true));
716
            transformer.InitialContextNode = input;
717
            results.Reset();
718
            transformer.Run(results);
719
            Console.WriteLine("2: " + results.XdmNode.OuterXml);
720
        }
721
    }
722

    
723
    /// <summary>
724
    /// Run a sequence of transformations in a pipeline, each one acting as a filter
725
    /// </summary>
726

    
727
    public class XsltFilterChain : Example
728
    {
729

    
730
        public override String testName
731
        {
732
            get { return "XsltFilterChain"; }
733
        }
734

    
735
        /// <summary>
736
        /// Run the test
737
        /// </summary>
738

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

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

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

    
750
            // Compile all three stylesheets
751
            XsltTransformer transformer1 = compiler.Compile(new Uri(samplesDir, "styles/identity.xsl")).Load();
752
            XsltTransformer transformer2 = compiler.Compile(new Uri(samplesDir, "styles/books.xsl")).Load();
753
            XsltTransformer transformer3 = compiler.Compile(new Uri(samplesDir, "styles/summarize.xsl")).Load();
754

    
755
            // Now run them in series
756
            transformer1.InitialContextNode = input;
757
            XdmDestination results1 = new XdmDestination();
758
            transformer1.Run(results1);
759
            //Console.WriteLine("After phase 1:");
760
            //Console.WriteLine(results1.XdmNode.OuterXml);
761

    
762
            transformer2.InitialContextNode = results1.XdmNode;
763
            XdmDestination results2 = new XdmDestination();
764
            transformer2.Run(results2);
765
            //Console.WriteLine("After phase 2:");
766
            //Console.WriteLine(results2.XdmNode.OuterXml);
767

    
768
            transformer3.InitialContextNode = results2.XdmNode;
769
            //TextWriterDestination results3 = new TextWriterDestination(new XmlTextWriter(Console.Out));
770
            XdmDestination results3 = new XdmDestination();
771
            transformer3.Run(results3);
772
            Console.WriteLine("After phase 3:");
773
            Console.WriteLine(results3.XdmNode.OuterXml);
774
        }
775
    }
776

    
777
    /// <summary>
778
    /// Transform from an XDM tree to an XDM tree
779
    /// </summary>
780

    
781
    public class XsltXdmToXdm : Example
782
    {
783

    
784
        public override String testName
785
        {
786
            get { return "XsltXdmToXdm"; }
787
        }
788

    
789
        /// <summary>
790
        /// Transform from an XDM tree to an XDM tree
791
        /// </summary>
792

    
793
        public override void run(Uri samplesDir)
794
        {
795
            // Create a Processor instance.
796
            Processor processor = new Processor();
797

    
798
            // Load the source document
799
            XdmNode input = processor.NewDocumentBuilder().Build(new Uri(samplesDir, "data/books.xml"));
800

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

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

    
807
            // Run the transformation
808
            transformer.InitialContextNode = input;
809
            XdmDestination result = new XdmDestination();
810
            transformer.Run(result);
811

    
812
            // Serialize the result so we can see that it worked
813
            StringWriter sw = new StringWriter();
814
            result.XdmNode.WriteTo(new XmlTextWriter(sw));
815
            Console.WriteLine(sw.ToString());
816

    
817
            // Note: we don't do 
818
            //   result.XdmNode.WriteTo(new XmlTextWriter(Console.Out));
819
            // because that results in the Console.out stream being closed, 
820
            // with subsequent attempts to write to it being rejected.
821
        }
822
    }
823

    
824
    /// <summary>
825
    /// Run an XSLT transformation from an Xdm tree, starting at a node that is not the document node
826
    /// </summary>
827

    
828
    public class XsltXdmElementToXdm : Example
829
    {
830

    
831
        public override String testName
832
        {
833
            get { return "XsltXdmElementToXdm"; }
834
        }
835

    
836
        /// <summary>
837
        /// Run an XSLT transformation from an Xdm tree, starting at a node that is not the document node
838
        /// </summary>
839
        /// <param name="fileNames">
840
        /// 1. The source document
841
        /// 2. The stylesheet
842
        /// </param>
843

    
844
        public override void run(Uri samplesDir)
845
        {
846
            // Create a Processor instance.
847
            Processor processor = new Processor();
848

    
849
            // Load the source document
850
            XdmNode input = processor.NewDocumentBuilder().Build(new Uri(samplesDir, "data/othello.xml"));
851

    
852
            // Navigate to the first grandchild
853
            XPathSelector eval = processor.NewXPathCompiler().Compile("/PLAY/FM[1]").Load();
854
            eval.ContextItem = input;
855
            input = (XdmNode)eval.EvaluateSingle();
856

    
857
            // Create an XSLT compiler
858
            XsltCompiler compiler = processor.NewXsltCompiler();
859

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

    
863
            // Run the transformation
864
            transformer.InitialContextNode = input;
865
            XdmDestination result = new XdmDestination();
866
            transformer.Run(result);
867

    
868
            // Serialize the result so we can see that it worked
869
            Console.WriteLine(result.XdmNode.OuterXml);
870
        }
871
    }
872

    
873
    /// <summary>
874
    /// Run a transformation from a DOM (System.Xml.Document) input to a DOM output
875
    /// </summary>
876

    
877
    public class XsltDomToDom : Example
878
    {
879

    
880
        public override String testName
881
        {
882
            get { return "XsltDomToDom"; }
883
        }
884

    
885
        /// <summary>
886
        /// Run a transformation from a DOM (System.Xml.Document) input to a DOM output
887
        /// </summary>
888

    
889
        public override void run(Uri samplesDir)
890
        {
891

    
892
            // Create a Processor instance.
893
            Processor processor = new Processor();
894

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

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

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

    
906
            // Run the transformation
907
            transformer.InitialContextNode = input;
908
            DomDestination result = new DomDestination();
909
            transformer.Run(result);
910

    
911
            // Serialize the result so we can see that it worked
912
            Console.WriteLine(result.XmlDocument.OuterXml);
913
        }
914
    }
915

    
916

    
917
    /// <summary>
918
    /// Run a transformation driven by an xml-stylesheet processing instruction in the source document
919
    /// </summary>
920

    
921
    public class XsltProcessingInstruction : Example
922
    {
923

    
924
        public override string testName
925
        {
926
            get { return "XsltProcessingInstruction"; }
927
        }
928

    
929
        /// <summary>
930
        /// Run a transformation driven by an xml-stylesheet processing instruction in the source document
931
        /// </summary>
932
        /// <param name="fileNames">
933
        /// 1. The source document
934
        /// </param>
935

    
936
        public override void run(Uri samplesDir)
937
        {
938
            // Create a Processor instance.
939
            Processor processor = new Processor();
940
            XsltExecutable exec;
941

    
942
            // Load the source document
943
            XdmNode input = processor.NewDocumentBuilder().Build(new Uri(samplesDir, "data/books.xml"));
944
            //Console.WriteLine("=============== source document ===============");
945
            //Console.WriteLine(input.OuterXml);
946
            //Console.WriteLine("=========== end of source document ============");
947

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

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

    
954
            XPathSelector eval = processor.NewXPathCompiler().Compile(path).Load();
955
            eval.ContextItem = input;
956
            XdmAtomicValue hrefval = (XdmAtomicValue)eval.EvaluateSingle();
957
            String href = (hrefval == null ? null : hrefval.ToString());
958

    
959
            if (href == null || href == "")
960
            {
961
                Console.WriteLine("No suitable xml-stylesheet processing instruction found");
962
                return;
963

    
964
            }
965
            else if (href[0] == '#')
966
            {
967

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

    
970
                Console.WriteLine("Locating embedded stylesheet with href = " + href);
971
                String idpath = "id('" + href.Substring(1) + "')";
972
                eval = processor.NewXPathCompiler().Compile(idpath).Load();
973
                eval.ContextItem = input;
974
                XdmNode node = (XdmNode)eval.EvaluateSingle();
975
                if (node == null)
976
                {
977
                    Console.WriteLine("No element found with ID " + href.Substring(1));
978
                    return;
979
                }
980
                exec = processor.NewXsltCompiler().Compile(node);
981

    
982
            }
983
            else
984
            {
985

    
986
                // The stylesheet is in an external document
987

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

    
990
                // Fetch and compile the referenced stylesheet
991
                exec = processor.NewXsltCompiler().Compile(new Uri(input.BaseUri, href.ToString()));
992
            }
993

    
994
            // Create a transformer 
995
            XsltTransformer transformer = exec.Load();
996

    
997
            // Run it       
998
            transformer.InitialContextNode = input;
999
            XdmDestination results = new XdmDestination();
1000
            transformer.Run(results);
1001
            Console.WriteLine("1: " + results.XdmNode.OuterXml);
1002

    
1003
        }
1004
    }
1005

    
1006
    /// <summary>
1007
    /// Run an XSLT transformation setting serialization properties from the calling application
1008
    /// </summary>
1009

    
1010
    public class XsltSettingOutputProperties : Example
1011
    {
1012

    
1013
        public override string testName
1014
        {
1015
            get { return "XsltSettingOutputProperties"; }
1016
        }
1017

    
1018
        /// <summary>
1019
        /// Run an XSLT transformation setting serialization properties from the calling application
1020
        /// </summary>
1021

    
1022
        public override void run(Uri samplesDir)
1023
        {
1024
            // Create a Processor instance.
1025
            Processor processor = new Processor();
1026

    
1027
            // Load the source document
1028
            XdmNode input = processor.NewDocumentBuilder().Build(new Uri(samplesDir, "data/books.xml"));
1029

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

    
1033
            // Set the root node of the source document to be the initial context node
1034
            transformer.InitialContextNode = input;
1035

    
1036
            // Create a serializer, with output to the standard output stream
1037
            Serializer serializer = new Serializer();
1038
            serializer.SetOutputProperty(Serializer.METHOD, "xml");
1039
            serializer.SetOutputProperty(Serializer.INDENT, "no");
1040
            serializer.SetOutputWriter(Console.Out);
1041

    
1042
            // Transform the source XML and serialize the result document
1043
            transformer.Run(serializer);
1044
        }
1045

    
1046
    }
1047

    
1048
    /// <summary>
1049
    /// Run an XSLT transformation making use of an XmlResolver to resolve URIs at document build time, at stylesheet compile time 
1050
    /// and at transformation run-time
1051
    /// </summary>
1052

    
1053
    public class XsltUsingSourceResolver : Example
1054
    {
1055

    
1056
        public override string testName
1057
        {
1058
            get { return "XsltUsingSourceResolver"; }
1059
        }
1060

    
1061
        /// <summary>
1062
        /// Run an XSLT transformation making use of an XmlResolver to resolve URIs both at compile time and at run-time
1063
        /// </summary>
1064

    
1065
        public override void run(Uri samplesDir)
1066
        {
1067

    
1068
            // Create a Processor instance.
1069
            Processor processor = new Processor();
1070

    
1071
            // Load the source document
1072
            DocumentBuilder builder = processor.NewDocumentBuilder();
1073
            UserXmlResolver buildTimeResolver = new UserXmlResolver();
1074
            buildTimeResolver.Message = "** Calling build-time XmlResolver: ";
1075
            builder.XmlResolver = buildTimeResolver;
1076
            builder.BaseUri = samplesDir;
1077

    
1078
            String doc = "<!DOCTYPE doc [<!ENTITY e SYSTEM 'flamingo.txt'>]><doc>&e;</doc>";
1079
            MemoryStream ms = new MemoryStream();
1080
            StreamWriter tw = new StreamWriter(ms);
1081
            tw.Write(doc);
1082
            tw.Flush();
1083
            Stream instr = new MemoryStream(ms.GetBuffer(), 0, (int)ms.Length);
1084
            XdmNode input = builder.Build(instr);
1085

    
1086
            // Create a transformer for the stylesheet.
1087
            String stylesheet =
1088
                "<xsl:stylesheet xmlns:xsl='http://www.w3.org/1999/XSL/Transform' version='2.0'>" +
1089
                "<xsl:import href='empty.xslt'/>" +
1090
                "<xsl:template match='/'>" +
1091
                "<out note=\"{doc('heron.txt')}\" ><xsl:copy-of select='.'/></out>" +
1092
                "</xsl:template>" +
1093
                "</xsl:stylesheet>";
1094

    
1095
            XsltCompiler compiler = processor.NewXsltCompiler();
1096
            UserXmlResolver compileTimeResolver = new UserXmlResolver();
1097
            compileTimeResolver.Message = "** Calling compile-time XmlResolver: ";
1098
            compiler.XmlResolver = compileTimeResolver;
1099
            compiler.BaseUri = samplesDir;
1100
            XsltTransformer transformer = compiler.Compile(new XmlTextReader(new StringReader(stylesheet))).Load();
1101

    
1102
            // Set the root node of the source document to be the initial context node
1103
            transformer.InitialContextNode = input;
1104

    
1105
            // Set the user-written XmlResolver
1106
            UserXmlResolver runTimeResolver = new UserXmlResolver();
1107
            runTimeResolver.Message = "** Calling transformation-time XmlResolver: ";
1108
            transformer.InputXmlResolver = runTimeResolver;
1109

    
1110
            // Create a serializer
1111
            Serializer serializer = new Serializer();
1112
            serializer.SetOutputWriter(Console.Out);
1113

    
1114
            // Transform the source XML to System.out.
1115
            transformer.Run(serializer);
1116

    
1117
        }
1118
    }
1119

    
1120
    /// <summary>
1121
    /// Run an XSLT transformation displaying compile-time errors to the console
1122
    /// </summary>
1123

    
1124
    public class XsltDisplayingErrors : Example
1125
    {
1126

    
1127
        public override string testName
1128
        {
1129
            get { return "XsltDisplayingErrors"; }
1130
        }
1131

    
1132
        /// <summary>
1133
        /// Run an XSLT transformation displaying compile-time errors to the console
1134
        /// </summary>
1135

    
1136
        public override void run(Uri samplesDir)
1137
        {
1138
            // Create a Processor instance.
1139
            Processor processor = new Processor();
1140

    
1141
            // Create the XSLT Compiler
1142
            XsltCompiler compiler = processor.NewXsltCompiler();
1143

    
1144

    
1145
            // Define a stylesheet containing errors
1146
            String stylesheet =
1147
                "<xsl:stylesheet xmlns:xsl='http://www.w3.org/1999/XSL/Transform' version='1.0'>\n" +
1148
                "<xsl:template name='eee:template'>\n" +
1149
                "  <xsl:value-of select='32'/>\n" +
1150
                "</xsl:template>\n" +
1151
                "<xsl:template name='main'>\n" +
1152
                "  <xsl:value-of select='$var'/>\n" +
1153
                "</xsl:template>\n" +
1154
                "</xsl:stylesheet>";
1155

    
1156

    
1157
            // Attempt to compile the stylesheet and display the errors
1158
            try
1159
            {
1160
                compiler.BaseUri = new Uri("http://localhost/stylesheet");
1161
                compiler.Compile(new XmlTextReader(new StringReader(stylesheet)));
1162
                Console.WriteLine("Stylesheet compilation succeeded");
1163
            }
1164
            catch (Exception)
1165
            {
1166
                Console.WriteLine("Stylesheet compilation failed");
1167
            }
1168

    
1169

    
1170
        }
1171
    }
1172

    
1173
    /// <summary>
1174
    /// Run an XSLT transformation capturing compile-time errors within the application
1175
    /// </summary>
1176

    
1177
    public class XsltCapturingErrors : Example
1178
    {
1179

    
1180
        public override string testName
1181
        {
1182
            get { return "XsltCapturingErrors"; }
1183
        }
1184

    
1185
        /// <summary>
1186
        /// Run an XSLT transformation capturing compile-time errors within the application
1187
        /// </summary>
1188

    
1189
        public override void run(Uri samplesDir)
1190
        {
1191
            // Create a Processor instance.
1192
            Processor processor = new Processor();
1193

    
1194
            // Create the XSLT Compiler
1195
            XsltCompiler compiler = processor.NewXsltCompiler();
1196

    
1197
            // Create a list to hold the error information
1198
            compiler.ErrorList = new ArrayList();
1199

    
1200
            // Define a stylesheet containing errors
1201
            String stylesheet =
1202
                "<xsl:stylesheet xmlns:xsl='http://www.w3.org/1999/XSL/Transform' version='1.0'>\n" +
1203
                "<xsl:template name='fff:template'>\n" +
1204
                "  <xsl:value-of select='32'/>\n" +
1205
                "</xsl:template>\n" +
1206
                "<xsl:template name='main'>\n" +
1207
                "  <xsl:value-of select='$var'/>\n" +
1208
                "</xsl:template>\n" +
1209
                "</xsl:stylesheet>";
1210

    
1211

    
1212
            // Attempt to compile the stylesheet and display the errors
1213
            try
1214
            {
1215
                compiler.BaseUri = new Uri("http://localhost/stylesheet");
1216
                compiler.Compile(new StringReader(stylesheet));
1217
                Console.WriteLine("Stylesheet compilation succeeded");
1218
            }
1219
            catch (Exception)
1220
            {
1221
                Console.WriteLine("Stylesheet compilation failed with " + compiler.ErrorList.Count + " errors");
1222
                foreach (StaticError error in compiler.ErrorList)
1223
                {
1224
                    Console.WriteLine("At line " + error.LineNumber + ": " + error.Message);
1225
                }
1226
            }
1227
        }
1228
    }
1229

    
1230
    /// <summary>
1231
    /// Run an XSLT transformation capturing run-time messages within the application
1232
    /// </summary>
1233

    
1234
    public class XsltCapturingMessages : Example
1235
    {
1236

    
1237
        public override string testName
1238
        {
1239
            get { return "XsltCapturingMessages"; }
1240
        }
1241

    
1242
        /// <summary>
1243
        /// Run an XSLT transformation capturing run-time messages within the application
1244
        /// </summary>
1245

    
1246
        public override void run(Uri samplesDir)
1247
        {
1248

    
1249
            // Create a Processor instance.
1250
            Processor processor = new Processor();
1251

    
1252
            // Create the XSLT Compiler
1253
            XsltCompiler compiler = processor.NewXsltCompiler();
1254

    
1255
            // Define a stylesheet that generates messages
1256
            String stylesheet =
1257
                "<xsl:stylesheet xmlns:xsl='http://www.w3.org/1999/XSL/Transform' version='2.0'>\n" +
1258
                "<xsl:template name='main'>\n" +
1259
                "  <xsl:message><a>starting</a></xsl:message>\n" +
1260
                "  <out><xsl:value-of select='current-date()'/></out>\n" +
1261
                "  <xsl:message><a>finishing</a></xsl:message>\n" +
1262
                "</xsl:template>\n" +
1263
                "</xsl:stylesheet>";
1264

    
1265
            compiler.BaseUri = new Uri("http://localhost/stylesheet");
1266
            XsltExecutable exec = compiler.Compile(new StringReader(stylesheet));
1267

    
1268

    
1269
            // Create a transformer for the stylesheet.
1270
            XsltTransformer transformer = exec.Load();
1271

    
1272
            // Set the name of the initial template
1273
            transformer.InitialTemplate = new QName("", "main");
1274

    
1275
            // Create a Listener to which messages will be written
1276
            transformer.MessageListener = new UserMessageListener();
1277

    
1278
            // Create a serializer
1279
            Serializer serializer = new Serializer();
1280
            serializer.SetOutputWriter(Console.Out);
1281

    
1282
            // Transform the source XML to System.out.
1283
            transformer.Run(serializer);
1284
        }
1285

    
1286
    }
1287

    
1288
    ///
1289
    /// Example user-written message listener
1290
    ///
1291

    
1292
    public class UserMessageListener : IMessageListener
1293
    {
1294

    
1295
        public void Message(XdmNode content, bool terminate, IXmlLocation location)
1296
        {
1297
            Console.Out.WriteLine("MESSAGE terminate=" + (terminate ? "yes" : "no") + " at " + DateTime.Now);
1298
            Console.Out.WriteLine("From instruction at line " + location.LineNumber +
1299
                    " of " + location.BaseUri);
1300
            Console.Out.WriteLine(">>" + content.StringValue);
1301
        }
1302
    }
1303

    
1304

    
1305
    /// <summary>
1306
    /// Run an XSLT transformation producing multiple output documents
1307
    /// </summary>
1308

    
1309
    public class XsltMultipleOutput : Example
1310
    {
1311

    
1312
        public override string testName
1313
        {
1314
            get { return "XsltMultipleOutput"; }
1315
        }
1316

    
1317
        /// <summary>
1318
        /// Run an XSLT transformation producing multiple output documents
1319
        /// </summary>
1320

    
1321
        public override void run(Uri samplesDir)
1322
        {
1323
            // Create a Processor instance.
1324
            Processor processor = new Processor();
1325
            processor.SetProperty("http://saxon.sf.net/feature/timing", "true");
1326

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

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

    
1333
            // Set the root node of the source document to be the initial context node
1334
            transformer.InitialContextNode = input;
1335

    
1336
            // Set the required stylesheet parameter
1337
            transformer.SetParameter(new QName("", "", "dir"), new XdmAtomicValue(samplesDir.ToString() + "play"));
1338

    
1339
            // Create a serializer
1340
            Serializer serializer = new Serializer();
1341
            serializer.SetOutputWriter(Console.Out);
1342

    
1343
            // Transform the source XML to System.out.
1344
            transformer.Run(serializer);
1345

    
1346
        }
1347

    
1348
    }
1349

    
1350

    
1351
    /// <summary>
1352
    /// Run an XSLT transformation using the id() function, with DTD validation
1353
    /// </summary>
1354

    
1355
    public class XsltUsingIdFunction : Example
1356
    {
1357

    
1358
        public override string testName
1359
        {
1360
            get { return "XsltUsingIdFunction"; }
1361
        }
1362

    
1363
        /// <summary>
1364
        /// Run an XSLT transformation using the id() function, with DTD validation
1365
        /// </summary>
1366

    
1367
        public override void run(Uri samplesDir)
1368
        {
1369
            // Create a Processor instance
1370
            Processor processor = new Processor();
1371

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

    
1376
            String doc = "<!DOCTYPE table [" +
1377
                "<!ELEMENT table (row*)>" +
1378
                "<!ELEMENT row EMPTY>" +
1379
                "<!ATTLIST row xml:id ID #REQUIRED>" +
1380
                "<!ATTLIST row value CDATA #REQUIRED>]>" +
1381
                "<table><row xml:id='A123' value='green'/><row xml:id='Z789' value='blue'/></table>";
1382

    
1383
            DocumentBuilder builder = processor.NewDocumentBuilder();
1384
            builder.DtdValidation = true;
1385
            builder.BaseUri = samplesDir;
1386
            MemoryStream ms = new MemoryStream();
1387
            StreamWriter tw = new StreamWriter(ms);
1388
            tw.Write(doc);
1389
            tw.Flush();
1390
            Stream instr = new MemoryStream(ms.GetBuffer(), 0, (int)ms.Length);
1391
            XdmNode input = builder.Build(instr);
1392

    
1393
            // Define a stylesheet that uses the id() function
1394
            String stylesheet =
1395
                "<xsl:stylesheet xmlns:xsl='http://www.w3.org/1999/XSL/Transform' version='2.0'>\n" +
1396
                "<xsl:template match='/'>\n" +
1397
                "  <xsl:copy-of select=\"id('Z789')\"/>\n" +
1398
                "</xsl:template>\n" +
1399
                "</xsl:stylesheet>";
1400

    
1401
            XsltCompiler compiler = processor.NewXsltCompiler();
1402
            compiler.BaseUri = new Uri("http://localhost/stylesheet");
1403
            XsltExecutable exec = compiler.Compile(new StringReader(stylesheet));
1404

    
1405
            //Create a transformer for the stylesheet
1406
            XsltTransformer transformer = exec.Load();
1407

    
1408
            // Set the root node of the source document to be the initial context node
1409
            transformer.InitialContextNode = input;
1410

    
1411
            //Set the destination
1412
            XdmDestination results = new XdmDestination();
1413

    
1414
            // Transform the XML
1415
            transformer.Run(results);
1416

    
1417
            // Show the result
1418
            Console.WriteLine(results.XdmNode.ToString());
1419

    
1420
        }
1421

    
1422
    }
1423

    
1424
    /// <summary>
1425
    /// Show a transformation using a user-written result document handler. This example
1426
    /// captures each of the result documents in a DOM, and creates a Hashtable that indexes
1427
    /// the DOM trees according to their absolute URI. On completion, it writes all the DOMs
1428
    /// to the standard output.
1429
    /// </summary>
1430

    
1431
    public class XsltUsingResultHandler : Example
1432
    {
1433

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

    
1439
        /// <summary>
1440
        /// Show a transformation using a user-written result document handler. This example
1441
        /// captures each of the result documents in a DOM, and creates a Hashtable that indexes
1442
        /// the DOM trees according to their absolute URI. On completion, it writes all the DOMs
1443
        /// to the standard output.
1444
        /// </summary>
1445

    
1446
        public override void run(Uri samplesDir)
1447
        {
1448
            // Create a Processor instance.
1449
            Processor processor = new Processor();
1450

    
1451
            // Load the source document
1452
            XdmNode input = processor.NewDocumentBuilder().Build(new Uri(samplesDir, "data/othello.xml"));
1453

    
1454
            // Define a stylesheet that splits the document up
1455
            String stylesheet =
1456
                "<xsl:stylesheet xmlns:xsl='http://www.w3.org/1999/XSL/Transform' version='2.0'>\n" +
1457
                "<xsl:template match='/'>\n" +
1458
                "  <xsl:for-each select='//ACT'>\n" +
1459
                "    <xsl:result-document href='{position()}.xml'>\n" +
1460
                "      <xsl:copy-of select='TITLE'/>\n" +
1461
                "    </xsl:result-document>\n" +
1462
                "  </xsl:for-each>\n" +
1463
                "</xsl:template>\n" +
1464
                "</xsl:stylesheet>";
1465

    
1466
            XsltCompiler compiler = processor.NewXsltCompiler();
1467
            compiler.BaseUri = new Uri("http://localhost/stylesheet");
1468
            XsltExecutable exec = compiler.Compile(new StringReader(stylesheet));
1469

    
1470
            // Create a transformer for the stylesheet.
1471
            XsltTransformer transformer = exec.Load();
1472

    
1473
            // Set the root node of the source document to be the initial context node
1474
            transformer.InitialContextNode = input;
1475

    
1476
            // Establish the result document handler
1477
            Hashtable results = new Hashtable();
1478
            transformer.ResultDocumentHandler = new UserResultDocumentHandler(results);
1479

    
1480
            // Transform the source XML to a NullDestination (because we only want the secondary result files).
1481
            transformer.Run(new NullDestination());
1482

    
1483
            // Process the captured DOM results
1484
            foreach (DictionaryEntry entry in results)
1485
            {
1486
                string uri = (string)entry.Key;
1487
                Console.WriteLine("\nResult File " + uri);
1488
                DomDestination dom = (DomDestination)results[uri];
1489
                Console.Write(dom.XmlDocument.OuterXml);
1490
            }
1491

    
1492
        }
1493

    
1494
    }
1495

    
1496
    /// <summary>
1497
    /// Show a transformation using a registered collection
1498
    /// </summary>
1499

    
1500
    public class XsltUsingRegisteredCollection : Example
1501
    {
1502

    
1503
        public override string testName
1504
        {
1505
            get { return "XsltUsingRegisteredCollection"; }
1506
        }
1507

    
1508
        /// <summary>
1509
        /// Show a transformation using a registered collection
1510
        /// </summary>
1511

    
1512
        public override void run(Uri samplesDir)
1513
        {
1514
            // Create a Processor instance.
1515
            Processor processor = new Processor();
1516

    
1517
            // Load the source document
1518
            XdmNode input = processor.NewDocumentBuilder().Build(new Uri(samplesDir, "data/othello.xml"));
1519

    
1520
            // Define a stylesheet that splits the document up
1521
            String stylesheet =
1522
                "<xsl:stylesheet xmlns:xsl='http://www.w3.org/1999/XSL/Transform' version='2.0'>\n" +
1523
                "<xsl:template name='main'>\n" +
1524
                " <out>\n" +
1525
                "  <xsl:for-each select=\"collection('http://www.example.org/my-collection')\">\n" +
1526
                "    <document uri='{document-uri(.)}' nodes='{count(//*)}'/>\n" +
1527
                "  </xsl:for-each><zzz/>\n" +
1528
                "  <xsl:for-each select=\"collection('http://www.example.org/my-collection')\">\n" +
1529
                "    <document uri='{document-uri(.)}' nodes='{count(//*)}'/>\n" +
1530
                "  </xsl:for-each>\n" +
1531
                " </out>\n" +
1532
                "</xsl:template>\n" +
1533
                "</xsl:stylesheet>";
1534

    
1535
            Uri[] documentList = new Uri[2];
1536
            documentList[0] = new Uri(samplesDir, "data/othello.xml");
1537
            documentList[1] = new Uri(samplesDir, "data/books.xml");
1538
            processor.RegisterCollection(new Uri("http://www.example.org/my-collection"), documentList);
1539

    
1540
            XsltCompiler compiler = processor.NewXsltCompiler();
1541
            compiler.BaseUri = new Uri("http://localhost/stylesheet");
1542
            XsltExecutable exec = compiler.Compile(new StringReader(stylesheet));
1543

    
1544
            // Create a transformer for the stylesheet.
1545
            XsltTransformer transformer = exec.Load();
1546

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

    
1550
            //Set the destination
1551
            XdmDestination results = new XdmDestination();
1552

    
1553
            // Transform the XML
1554
            transformer.Run(results);
1555

    
1556
            // Show the result
1557
            Console.WriteLine(results.XdmNode.ToString());
1558

    
1559
        }
1560
    }
1561

    
1562
    /// <summary>
1563
    /// Show a transformation using a registered collection
1564
    /// </summary>
1565

    
1566
    public class XsltUsingDirectoryCollection : Example
1567
    {
1568

    
1569
        public override string testName
1570
        {
1571
            get { return "XsltUsingDirectoryCollection"; }
1572
        }
1573

    
1574
        /// <summary>
1575
        /// Show a transformation using a collection that maps to a directory
1576
        /// </summary>
1577

    
1578
        public override void run(Uri samplesDir)
1579
        {
1580
            // Create a Processor instance.
1581
            Processor processor = new Processor();
1582

    
1583
            // Load the source document
1584
            XdmNode input = processor.NewDocumentBuilder().Build(new Uri(samplesDir, "data/othello.xml"));
1585

    
1586
            // Define a stylesheet that splits the document up
1587
            String stylesheet =
1588
                "<xsl:stylesheet xmlns:xsl='http://www.w3.org/1999/XSL/Transform' version='2.0'>\n" +
1589
                "<xsl:template name='main'>\n" +
1590
                " <out>\n" +
1591
                "  <xsl:for-each select=\"collection('" + samplesDir + "?recurse=yes;select=*.xml;on-error=warning')\">\n" +
1592
                "    <document uri='{document-uri(.)}' nodes='{count(//*)}'/>\n" +
1593
                "  </xsl:for-each><zzz/>\n" +
1594
                " </out>\n" +
1595
                "</xsl:template>\n" +
1596
                "</xsl:stylesheet>";
1597

    
1598

    
1599
            XsltCompiler compiler = processor.NewXsltCompiler();
1600
            compiler.BaseUri = new Uri("http://localhost/stylesheet");
1601
            XsltExecutable exec = compiler.Compile(new StringReader(stylesheet));
1602

    
1603
            // Create a transformer for the stylesheet.
1604
            XsltTransformer transformer = exec.Load();
1605

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

    
1609
            //Set the destination
1610
            XdmDestination results = new XdmDestination();
1611

    
1612
            // Transform the XML
1613
            transformer.Run(results);
1614

    
1615
            // Show the result
1616
            Console.WriteLine(results.XdmNode.ToString());
1617

    
1618
        }
1619

    
1620
    }
1621

    
1622

    
1623

    
1624
    /// <summary>
1625
    /// Show a transformation using calls to extension functions
1626
    /// </summary>
1627

    
1628
    public class XsltIntegratedExtension : Example
1629
    {
1630

    
1631
        public override string testName
1632
        {
1633
            get { return "XsltIntegratedExtension"; }
1634
        }
1635

    
1636
        /// <summary>
1637
        /// Show a transformation using calls to extension functions
1638
        /// </summary>
1639

    
1640
        public override void run(Uri samplesDir)
1641
        {
1642

    
1643
            // Create a Processor instance.
1644
            Processor processor = new Processor();
1645

    
1646
            // Identify the Processor version
1647
            Console.WriteLine(processor.ProductVersion);
1648

    
1649
            // Set diagnostics
1650
            //processor.SetProperty("http://saxon.sf.net/feature/trace-external-functions", "true");
1651

    
1652
            // Create the stylesheet
1653
            String s = @"<xsl:transform version='2.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform'" +
1654
                @" xmlns:math='http://example.math.co.uk/demo'> " +
1655
                @" <xsl:template name='go'> " +
1656
                @" <out sqrt2='{math:sqrt(2.0e0)}' " +
1657
                @" sqrtEmpty='{math:sqrt(())}'/> " +
1658
                @" </xsl:template></xsl:transform>";
1659

    
1660
            // Register the integrated extension function math:sqrt
1661

    
1662
            processor.RegisterExtensionFunction(new Sqrt());
1663

    
1664
            // Create a transformer for the stylesheet.
1665
            XsltTransformer transformer = processor.NewXsltCompiler().Compile(new StringReader(s)).Load();
1666

    
1667
            // Set the root node of the source document to be the initial context node
1668
            transformer.InitialTemplate = new QName("go");
1669

    
1670
            // Create a serializer
1671
            Serializer serializer = new Serializer();
1672
            serializer.SetOutputWriter(Console.Out);
1673
            serializer.SetOutputProperty(Serializer.INDENT, "yes");
1674

    
1675
            // Transform the source XML to System.out.
1676
            transformer.Run(serializer);
1677
        }
1678

    
1679
    }
1680

    
1681
    /// <summary>
1682
    /// Example extension function to compute a square root.
1683
    /// </summary>
1684

    
1685
    public class Sqrt : ExtensionFunctionDefinition
1686
    {
1687
        public override QName FunctionName
1688
        {
1689
            get
1690
            {
1691
                return new QName("http://example.math.co.uk/demo", "sqrt");
1692
            }
1693
        }
1694

    
1695
        public override int MinimumNumberOfArguments
1696
        {
1697
            get
1698
            {
1699
                return 1;
1700
            }
1701
        }
1702

    
1703
        public override int MaximumNumberOfArguments
1704
        {
1705
            get
1706
            {
1707
                return 1;
1708
            }
1709
        }
1710

    
1711
        public override XdmSequenceType[] ArgumentTypes
1712
        {
1713
            get
1714
            {
1715
                return new XdmSequenceType[]{
1716
                    new XdmSequenceType(XdmAtomicType.BuiltInAtomicType(QName.XS_DOUBLE), '?')
1717
                };
1718
            }
1719
        }
1720

    
1721
        public override XdmSequenceType ResultType(XdmSequenceType[] ArgumentTypes)
1722
        {
1723
            return new XdmSequenceType(XdmAtomicType.BuiltInAtomicType(QName.XS_DOUBLE), '?');
1724
        }
1725

    
1726
        public override bool TrustResultType
1727
        {
1728
            get
1729
            {
1730
                return true;
1731
            }
1732
        }
1733

    
1734

    
1735
        public override ExtensionFunctionCall MakeFunctionCall()
1736
        {
1737
            return new SqrtCall();
1738
        }
1739
    }
1740

    
1741
    internal class SqrtCall : ExtensionFunctionCall
1742
    {
1743
        public override IXdmEnumerator Call(IXdmEnumerator[] arguments, DynamicContext context)
1744
        {
1745
            Boolean exists = arguments[0].MoveNext();
1746
            if (exists)
1747
            {
1748
                XdmAtomicValue arg = (XdmAtomicValue)arguments[0].Current;
1749
                double val = (double)arg.Value;
1750
                double sqrt = System.Math.Sqrt(val);
1751
                XdmAtomicValue result = new XdmAtomicValue(sqrt);
1752
                return (IXdmEnumerator)result.GetEnumerator();
1753
            }
1754
            else
1755
            {
1756
                return EmptyEnumerator.INSTANCE;
1757
            }
1758
        }
1759

    
1760
    }
1761

    
1762
    /// <summary>
1763
    /// Example extension function to return the default namespace from the static context
1764
    /// </summary>
1765

    
1766
    public class DefaultNamespace : ExtensionFunctionDefinition
1767
    {
1768
        public override QName FunctionName
1769
        {
1770
            get
1771
            {
1772
                return new QName("http://example.env.co.uk/demo", "defaultNamespace");
1773
            }
1774
        }
1775

    
1776
        public override int MinimumNumberOfArguments
1777
        {
1778
            get
1779
            {
1780
                return 0;
1781
            }
1782
        }
1783

    
1784
        public override int MaximumNumberOfArguments
1785
        {
1786
            get
1787
            {
1788
                return 0;
1789
            }
1790
        }
1791

    
1792
        public override XdmSequenceType[] ArgumentTypes
1793
        {
1794
            get
1795
            {
1796
                return new XdmSequenceType[] { };
1797
            }
1798
        }
1799

    
1800
        public override bool DependsOnFocus
1801
        {
1802
            get
1803
            {
1804
                return true;
1805
                // actually it depends on the static context rather than the focus; but returning true is necessary
1806
                // to avoid the call being extracted to a global variable.
1807
            }
1808
        }
1809

    
1810
        public override XdmSequenceType ResultType(XdmSequenceType[] ArgumentTypes)
1811
        {
1812
            return new XdmSequenceType(XdmAtomicType.BuiltInAtomicType(QName.XS_STRING), '?');
1813
        }
1814

    
1815
        public override bool TrustResultType
1816
        {
1817
            get
1818
            {
1819
                return true;
1820
            }
1821
        }
1822

    
1823

    
1824
        public override ExtensionFunctionCall MakeFunctionCall()
1825
        {
1826
            return new DefaultNamespaceCall();
1827
        }
1828
    }
1829

    
1830
    internal class DefaultNamespaceCall : ExtensionFunctionCall
1831
    {
1832
        private string defaultNamespace;
1833

    
1834
        public override void SupplyStaticContext(StaticContext context)
1835
        {
1836
            defaultNamespace = context.GetNamespaceForPrefix("");
1837
        }
1838

    
1839
        public override IXdmEnumerator Call(IXdmEnumerator[] arguments, DynamicContext context)
1840
        {
1841
            if (defaultNamespace != null)
1842
            {
1843
                return (IXdmEnumerator)new XdmAtomicValue(defaultNamespace).GetEnumerator();
1844
            }
1845
            else
1846
            {
1847
                return EmptyEnumerator.INSTANCE;
1848
            }
1849
        }
1850

    
1851
    }
1852

    
1853
    /// <summary>
1854
    /// Show a query producing a document as its result and serializing this to a FileStream
1855
    /// </summary>
1856

    
1857
    public class XQueryToStream : Example
1858
    {
1859

    
1860
        public override string testName
1861
        {
1862
            get { return "XQueryToStream"; }
1863
        }
1864

    
1865
        /// <summary>
1866
        /// Show a query producing a document as its result and serializing this to a FileStream
1867
        /// </summary>
1868

    
1869
        public override void run(Uri samplesDir)
1870
        {
1871
            Processor processor = new Processor();
1872
            XQueryCompiler compiler = processor.NewXQueryCompiler();
1873
            compiler.BaseUri = samplesDir.ToString();
1874
            compiler.DeclareNamespace("saxon", "http://saxon.sf.net/");
1875
            XQueryExecutable exp = compiler.Compile("<saxon:example>{static-base-uri()}</saxon:example>");
1876
            XQueryEvaluator eval = exp.Load();
1877
            Serializer qout = new Serializer();
1878
            qout.SetOutputProperty(Serializer.METHOD, "xml");
1879
            qout.SetOutputProperty(Serializer.INDENT, "yes");
1880
            qout.SetOutputStream(new FileStream("testoutput.xml", FileMode.Create, FileAccess.Write));
1881
            Console.WriteLine("Output written to testoutput.xml");
1882
            eval.Run(qout);
1883
        }
1884

    
1885
    }
1886

    
1887
    /// <summary>
1888
    /// Show a query producing a single atomic value as its result and returning the value
1889
    /// to the C# application
1890
    /// </summary>
1891

    
1892
    public class XQueryToAtomicValue : Example
1893
    {
1894

    
1895
        public override string testName
1896
        {
1897
            get { return "XQueryToAtomicValue"; }
1898
        }
1899

    
1900
        /// <summary>
1901
        /// Show a query producing a single atomic value as its result and returning the value
1902
        /// to the C# application
1903
        /// </summary>
1904

    
1905
        public override void run(Uri samplesDir)
1906
        {
1907
            Processor processor = new Processor();
1908
            XQueryCompiler compiler = processor.NewXQueryCompiler();
1909
            XQueryExecutable exp = compiler.Compile("avg(for $i in 1 to 10 return $i * $i)");
1910
            XQueryEvaluator eval = exp.Load();
1911
            XdmAtomicValue result = (XdmAtomicValue)eval.EvaluateSingle();
1912
            Console.WriteLine("Result type: " + result.Value.GetType());
1913
            Console.WriteLine("Result value: " + (decimal)result.Value);
1914
        }
1915

    
1916
    }
1917

    
1918
    /// <summary>
1919
    /// Show a query producing a DOM as its input and producing a DOM as its output
1920
    /// </summary>
1921

    
1922
    public class XQueryToDom : Example
1923
    {
1924

    
1925
        public override string testName
1926
        {
1927
            get { return "XQueryToDom"; }
1928
        }
1929

    
1930
        /// <summary>
1931
        /// Show a query producing a DOM as its input and producing a DOM as its output
1932
        /// </summary>
1933

    
1934
        public override void run(Uri samplesDir)
1935
        {
1936
            Processor processor = new Processor();
1937

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

    
1942
            XQueryCompiler compiler = processor.NewXQueryCompiler();
1943
            XQueryExecutable exp = compiler.Compile("<doc>{reverse(/*/*)}</doc>");
1944
            XQueryEvaluator eval = exp.Load();
1945
            eval.ContextItem = indoc;
1946
            DomDestination qout = new DomDestination();
1947
            eval.Run(qout);
1948
            XmlDocument outdoc = qout.XmlDocument;
1949
            Console.WriteLine(outdoc.OuterXml);
1950
        }
1951

    
1952
    }
1953

    
1954
    /// <summary>
1955
    /// Show a query producing a Saxon tree as its input and producing a Saxon tree as its output
1956
    /// </summary>
1957

    
1958
    public class XQueryToXdm : Example
1959
    {
1960

    
1961
        public override string testName
1962
        {
1963
            get { return "XQueryToXdm"; }
1964
        }
1965

    
1966
        /// <summary>
1967
        /// Show a query producing a Saxon tree as its input and producing a Saxon tree as its output
1968
        /// </summary>
1969

    
1970
        public override void run(Uri samplesDir)
1971
        {
1972
            Processor processor = new Processor();
1973

    
1974
            DocumentBuilder loader = processor.NewDocumentBuilder();
1975
            loader.BaseUri = new Uri(samplesDir, "data/books.xml");
1976
            XdmNode indoc = loader.Build(loader.BaseUri);
1977

    
1978
            XQueryCompiler compiler = processor.NewXQueryCompiler();
1979
            XQueryExecutable exp = compiler.Compile("<doc>{reverse(/*/*)}</doc>");
1980
            XQueryEvaluator eval = exp.Load();
1981
            eval.ContextItem = indoc;
1982
            XdmDestination qout = new XdmDestination();
1983
            eval.Run(qout);
1984
            XdmNode outdoc = qout.XdmNode;
1985
            Console.WriteLine(outdoc.OuterXml);
1986
        }
1987

    
1988
    }
1989

    
1990
    /// <summary>
1991
    /// Show a query making a direct call to a user-defined function defined in the query
1992
    /// </summary>
1993

    
1994
    public class XQueryCallFunction : Example
1995
    {
1996

    
1997
        public override string testName
1998
        {
1999
            get { return "XQueryCallFunction"; }
2000
        }
2001

    
2002
        /// <summary>
2003
        /// Show a direct call on a user-defined function defined within the query
2004
        /// </summary>
2005

    
2006
        public override void run(Uri samplesDir)
2007
        {
2008
            Processor processor = new Processor();
2009

    
2010
            XQueryCompiler qc = processor.NewXQueryCompiler();
2011
            XQueryExecutable exp1 = qc.Compile("declare namespace f='f.ns';" +
2012
                   "declare variable $z := 1 + xs:integer(doc-available('" + new Uri(samplesDir, "data/books.xml").ToString() + "'));" +
2013
                   "declare variable $p as xs:integer external;" +
2014
                   "declare function f:t1($v1 as xs:integer) { " +
2015
                   "   $v1 div $z + $p" +
2016
                   "};" +
2017
                   "10");
2018
            XQueryEvaluator ev = exp1.Load();
2019
            ev.SetExternalVariable(new QName("", "p"), new XdmAtomicValue(39));
2020
            XdmValue v1 = new XdmAtomicValue(10);
2021
            XdmValue result = ev.CallFunction(new QName("f.ns", "f:t1"), new XdmValue[] { v1 });
2022
            Console.WriteLine("First result (expected 44): " + result.ToString());
2023
            v1 = new XdmAtomicValue(20);
2024
            result = ev.CallFunction(new QName("f.ns", "f:t1"), new XdmValue[] { v1 });
2025
            Console.WriteLine("Second result (expected 49): " + result.ToString());
2026
        }
2027

    
2028
    }
2029

    
2030

    
2031

    
2032
    /// <summary>
2033
    /// Show a query producing a sequence as its result and returning the sequence
2034
    /// to the C# application in the form of an iterator. For each item in the
2035
    /// result, its string value is output.
2036
    /// </summary>
2037

    
2038
    public class XQueryToSequence : Example
2039
    {
2040

    
2041
        public override string testName
2042
        {
2043
            get { return "XQueryToSequence"; }
2044
        }
2045

    
2046
        /// <summary>
2047
        /// Show a query producing a sequence as its result and returning the sequence
2048
        /// to the C# application in the form of an iterator. For each item in the
2049
        /// result, its string value is output.
2050
        /// </summary>
2051

    
2052
        public override void run(Uri samplesDir)
2053
        {
2054
            Processor processor = new Processor();
2055
            XQueryCompiler compiler = processor.NewXQueryCompiler();
2056
            XQueryExecutable exp = compiler.Compile("for $i in 1 to 10 return $i * $i");
2057
            XQueryEvaluator eval = exp.Load();
2058
            XdmValue value = eval.Evaluate();
2059
            IEnumerator e = value.GetEnumerator();
2060
            while (e.MoveNext())
2061
            {
2062
                XdmItem item = (XdmItem)e.Current;
2063
                Console.WriteLine(item.ToString());
2064
            }
2065

    
2066
        }
2067

    
2068
    }
2069

    
2070
    /// <summary>
2071
    /// Show a query reading an input document using an XmlReader (the .NET XML parser)
2072
    /// </summary>
2073

    
2074
    public class XQueryFromXmlReader : Example
2075
    {
2076

    
2077
        public override string testName
2078
        {
2079
            get { return "XQueryFromXmlReader"; }
2080
        }
2081

    
2082
        /// <summary>
2083
        /// Show a query reading an input document using an XmlReader (the .NET XML parser)
2084
        /// </summary>
2085

    
2086
        public override void run(Uri samplesDir)
2087
        {
2088
            Processor processor = new Processor();
2089

    
2090
            String inputFileName = new Uri(samplesDir, "data/books.xml").ToString();
2091
            XmlTextReader reader = new XmlTextReader(inputFileName,
2092
                UriConnection.getReadableUriStream(new Uri(samplesDir, "data/books.xml")));
2093
            //new FileStream(inputFileName, FileMode.Open, FileAccess.Read));
2094
            reader.Normalization = true;
2095

    
2096
            // add a validating reader - not to perform validation, but to expand entity references
2097
            XmlValidatingReader validator = new XmlValidatingReader(reader);
2098
            validator.ValidationType = ValidationType.None;
2099

    
2100
            XdmNode doc = processor.NewDocumentBuilder().Build(validator);
2101

    
2102
            XQueryCompiler compiler = processor.NewXQueryCompiler();
2103
            XQueryExecutable exp = compiler.Compile("/");
2104
            XQueryEvaluator eval = exp.Load();
2105
            eval.ContextItem = doc;
2106
            Serializer qout = new Serializer();
2107
            qout.SetOutputProperty(Serializer.METHOD, "xml");
2108
            qout.SetOutputProperty(Serializer.INDENT, "yes");
2109
            qout.SetOutputStream(new FileStream("testoutput2.xml", FileMode.Create, FileAccess.Write));
2110
            eval.Run(qout);
2111
        }
2112

    
2113
    }
2114

    
2115
    /// <summary>
2116
    /// Show a query producing a sequence as its result and returning the sequence
2117
    /// to the C# application in the form of an iterator. The sequence is then
2118
    /// output by serializing each item individually, with each item on a new line.
2119
    /// </summary>
2120

    
2121
    public class XQueryToSerializedSequence : Example
2122
    {
2123

    
2124
        public override string testName
2125
        {
2126
            get { return "XQueryToSerializedSequence"; }
2127
        }
2128

    
2129
        /// <summary>
2130
        /// Show a query producing a sequence as its result and returning the sequence
2131
        /// to the C# application in the form of an iterator. The sequence is then
2132
        /// output by serializing each item individually, with each item on a new line.
2133
        /// </summary>
2134

    
2135
        public override void run(Uri samplesDir)
2136
        {
2137
            Processor processor = new Processor();
2138
            String inputFileName = new Uri(samplesDir, "data/books.xml").ToString();
2139
            //XmlTextReader reader = new XmlTextReader(inputFileName,
2140
            //    new FileStream(inputFileName, FileMode.Open, FileAccess.Read));
2141
            XmlTextReader reader = new XmlTextReader(inputFileName,
2142
                UriConnection.getReadableUriStream(new Uri(samplesDir, "data/books.xml")));
2143
            reader.Normalization = true;
2144

    
2145
            // add a validating reader - not to perform validation, but to expand entity references
2146
            XmlValidatingReader validator = new XmlValidatingReader(reader);
2147
            validator.ValidationType = ValidationType.None;
2148

    
2149
            XdmNode doc = processor.NewDocumentBuilder().Build(reader);
2150

    
2151
            XQueryCompiler compiler = processor.NewXQueryCompiler();
2152
            XQueryExecutable exp = compiler.Compile("//ISBN");
2153
            XQueryEvaluator eval = exp.Load();
2154
            eval.ContextItem = doc;
2155

    
2156
            foreach (XdmNode node in eval)
2157
            {
2158
                Console.WriteLine(node.OuterXml);
2159
            }
2160
        }
2161

    
2162
    }
2163

    
2164
    /// <summary>
2165
    /// Show a query that takes a parameter (external variable) as input.
2166
    /// The query produces a single atomic value as its result and returns the value
2167
    /// to the C# application. 
2168
    /// </summary>
2169

    
2170
    public class XQueryUsingParameter : Example
2171
    {
2172

    
2173
        public override string testName
2174
        {
2175
            get { return "XQueryUsingParameter"; }
2176
        }
2177

    
2178
        /// <summary>
2179
        /// Show a query that takes a parameter (external variable) as input.
2180
        /// The query produces a single atomic value as its result and returns the value
2181
        /// to the C# application. 
2182
        /// </summary>
2183

    
2184
        public override void run(Uri samplesDir)
2185
        {
2186
            Processor processor = new Processor();
2187
            XQueryCompiler compiler = processor.NewXQueryCompiler();
2188
            compiler.DeclareNamespace("p", "http://saxon.sf.net/ns/p");
2189
            XQueryExecutable exp = compiler.Compile(
2190
                    "declare variable $p:in as xs:integer external; $p:in * $p:in");
2191
            XQueryEvaluator eval = exp.Load();
2192
            eval.SetExternalVariable(new QName("http://saxon.sf.net/ns/p", "p:in"), new XdmAtomicValue(12));
2193
            XdmAtomicValue result = (XdmAtomicValue)eval.EvaluateSingle();
2194
            Console.WriteLine("Result type: " + result.Value.GetType());
2195
            Console.WriteLine("Result value: " + (long)result.Value);
2196
        }
2197

    
2198
    }
2199

    
2200
    /// <summary>
2201
    /// Show a query consisting of two modules, using a QueryResolver to resolve
2202
    /// the "import module" declaration
2203
    /// </summary>
2204

    
2205
    public class XQueryMultiModule : Example
2206
    {
2207

    
2208
        public override string testName
2209
        {
2210
            get { return "XQueryMultiModule"; }
2211
        }
2212

    
2213
        /// <summary>
2214
        /// Show a query consisting of two modules, using a QueryResolver to resolve
2215
        /// the "import module" declaration
2216
        /// </summary>
2217

    
2218
        public override void run(Uri samplesDir)
2219
        {
2220

    
2221
            String mod1 = "import module namespace m2 = 'http://www.example.com/module2';" +
2222
                          "m2:square(3)";
2223

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

    
2227
            Processor processor = new Processor();
2228
            XQueryCompiler compiler = processor.NewXQueryCompiler();
2229

    
2230
            InlineModuleResolver resolver = new InlineModuleResolver();
2231
            resolver.AddModule(new Uri("http://www.example.com/module2"), mod2);
2232
            compiler.QueryResolver = resolver;
2233
            XQueryExecutable exp = compiler.Compile(mod1);
2234
            XQueryEvaluator eval = exp.Load();
2235

    
2236
            XdmAtomicValue result = (XdmAtomicValue)eval.EvaluateSingle();
2237
            Console.WriteLine("Result type: " + result.Value.GetType());
2238
            Console.WriteLine("Result value: " + (long)result.Value);
2239
        }
2240

    
2241
        // A simple QueryResolver designed to show that the actual query
2242
        // text can come from anywhere: in this case, the resolver maintains
2243
        // a simple mapping of module URIs onto strings.
2244

    
2245
        public class InlineModuleResolver : IQueryResolver
2246
        {
2247

    
2248
            private Hashtable modules = new Hashtable();
2249

    
2250
            public void AddModule(Uri moduleName, String moduleText)
2251
            {
2252
                modules.Add(moduleName, moduleText);
2253
            }
2254

    
2255
            public Uri[] GetModules(String moduleUri, Uri baseUri, String[] locationHints)
2256
            {
2257
                Uri[] result = { new Uri(moduleUri) };
2258
                return result;
2259
            }
2260

    
2261
            public Object GetEntity(Uri absoluteUri)
2262
            {
2263
                return modules[absoluteUri];
2264
            }
2265
        }
2266

    
2267
    }
2268

    
2269

    
2270
    public class UriConnection
2271
    {
2272

    
2273
        // Get a stream for reading from a file:// URI
2274

    
2275
        public static Stream getReadableUriStream(Uri uri)
2276
        {
2277
            WebRequest request = (WebRequest)WebRequest.Create(uri);
2278
            return request.GetResponse().GetResponseStream();
2279
        }
2280

    
2281
        // Get a stream for writing to a file:// URI
2282

    
2283
        public static Stream getWritableUriStream(Uri uri)
2284
        {
2285
            FileWebRequest request = (FileWebRequest)WebRequest.CreateDefault(uri);
2286
            request.Method = "POST";
2287
            return request.GetRequestStream();
2288
        }
2289
    }
2290

    
2291
    ///
2292
    /// A sample XmlResolver. In the case of a URI ending with ".txt", it returns the
2293
    /// URI itself, wrapped as an XML document. In the case of the URI "empty.xslt", it returns an empty
2294
    /// stylesheet. In all other cases, it returns null, which has the effect of delegating
2295
    /// processing to the standard XmlResolver.
2296
    ///
2297

    
2298
    public class UserXmlResolver : XmlUrlResolver
2299
    {
2300

    
2301
        public String Message = null;
2302

    
2303
        public override object GetEntity(Uri absoluteUri, String role, Type ofObjectToReturn)
2304
        {
2305
            if (Message != null)
2306
            {
2307
                Console.WriteLine(Message + absoluteUri + " (role=" + role + ")");
2308
            }
2309

    
2310
            if (absoluteUri.ToString().EndsWith(".txt"))
2311
            {
2312
                MemoryStream ms = new MemoryStream();
2313
                StreamWriter tw = new StreamWriter(ms);
2314
                tw.Write("<uri>");
2315
                tw.Write(absoluteUri);
2316
                tw.Write("</uri>");
2317
                tw.Flush();
2318
                return new MemoryStream(ms.GetBuffer(), 0, (int)ms.Length);
2319
            }
2320
            if (absoluteUri.ToString().EndsWith("empty.xslt"))
2321
            {
2322
                String ss = "<transform xmlns='http://www.w3.org/1999/XSL/Transform' version='2.0'/>";
2323
                MemoryStream ms = new MemoryStream();
2324
                StreamWriter tw = new StreamWriter(ms);
2325
                tw.Write(ss);
2326
                tw.Flush();
2327
                return new MemoryStream(ms.GetBuffer(), 0, (int)ms.Length);
2328
            }
2329
            else
2330
            {
2331
                return null;
2332
            }
2333
        }
2334
    }
2335

    
2336
    public class UserResultDocumentHandler : IResultDocumentHandler
2337
    {
2338

    
2339
        private Hashtable results;
2340

    
2341
        public UserResultDocumentHandler(Hashtable table)
2342
        {
2343
            this.results = table;
2344
        }
2345

    
2346
        public XmlDestination HandleResultDocument(string href, Uri baseUri)
2347
        {
2348
            DomDestination destination = new DomDestination();
2349
            results[href] = destination;
2350
            return destination;
2351
        }
2352

    
2353
    }
2354
}
2355

    
2356

    
2357
//
2358
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
2359
// you may not use this file except in compliance with the License. You may obtain a copy of the
2360
// License at http://www.mozilla.org/MPL/
2361
//
2362
// Software distributed under the License is distributed on an "AS IS" basis,
2363
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
2364
// See the License for the specific language governing rights and limitations under the License.
2365
//
2366
// The Original Code is: all this file.
2367
//
2368
// The Initial Developer of the Original Code is Michael H. Kay.
2369
//
2370
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
2371
//
2372
// Contributor(s): none.
2373
//
2374

    
2375

    
2376

    
2377

    
(5-5/24)