Project

Profile

Help

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

he / src / test / nunit / SaxonNUnit / SaxonNUnit / TestXPathCompiler.cs @ 3df04590

1
using System;
2
using System.Collections.Generic;
3
using System.Collections;
4
using System.IO;
5
using Saxon.Api;
6
using NUnit.Framework;
7
using System.Xml;
8
using SaxonNUnit;
9
using System.Text;
10
using net.sf.saxon;
11
using net.sf.saxon.dotnet;
12
using System.Linq;
13

    
14
namespace Extension
15
{
16

    
17
    public class TestReflectionHelper
18
    {
19

    
20
        public TestReflectionHelper() { }
21

    
22
       /* public static String p(String a, Object b)
23
        {
24
            return "p1 method, content: a, object b ignored" + a;
25
        } */
26
        public static String p(Object a, String b)
27
        {
28
            return "p2 method, content: b, object a ignored" + b;
29
        }
30
        public static String x(Object a)
31
        {
32
            return "x method, content: a" ;
33
        }
34

    
35
        public static String q() {
36
            return "foo-bar";
37
        }
38

    
39

    
40
    }
41

    
42
}
43

    
44
namespace Saxon9.NUnit
45
{
46

    
47
    
48
    class TestXPathCompiler
49
    {
50

    
51

    
52
        [Test]
53
        public void TestSimple()
54
        {
55
            try
56
            {
57
                Processor p = new Processor(false);
58
                XPathCompiler c = p.NewXPathCompiler();
59
                XPathExecutable e = c.Compile("count(//*)");
60
                DocumentBuilder b = p.NewDocumentBuilder();
61
                b.BaseUri = new Uri("http://www.example.com");
62
                XdmNode foo = b.Build(new StringReader("<foo><bar/></foo>"));
63
                XPathSelector s = e.Load();
64
                s.ContextItem = foo;
65
                Assert.AreEqual(2, ((XdmAtomicValue)s.EvaluateSingle()).GetLongValue());
66
            }
67
            catch (Exception e)
68
            {
69
                Assert.Fail(e.Message);
70
            }
71
        }
72

    
73
        [Test]
74
        public void TestSimpleDelegates()
75
        {
76
            try
77
            {
78
                Processor p = new Processor(false);
79
                XPathCompiler c = p.NewXPathCompiler();
80
                XPathExecutable e = c.Compile("count(//*)");
81
                DocumentBuilder b = p.NewDocumentBuilder();
82
                b.BaseUri = new Uri("http://www.example.com");
83
                XdmValue value = new XdmValue(XdmAtomicValue.MakeAtomicValue("test1"));
84
            
85
                value = value.Append(XdmAtomicValue.MakeAtomicValue("test2"));
86
                //IEnumerable<XdmItem> iterx = value.Select(Root);
87
                IEnumerator<XdmNode> iter = b.Build(new StringReader("<foo attr='value1'><bar attr2='value2' />text</foo>")).EnumerateAxis(XdmAxis.Child);
88
                bool moved = iter.MoveNext();
89
                XdmNode foo = iter.Current;
90
                value = value.Append(foo);
91

    
92
                IEnumerator<XdmNode> iter2 = b.Build(new StringReader("<doc attr='value1'><name attr2='value2' />Joe</doc>")).EnumerateAxis(XdmAxis.Child);
93
                bool moved2 = iter2.MoveNext();
94
                XdmNode foo2 = iter2.Current;
95
                value = value.Append(foo2);
96

    
97

    
98
                // test 1
99
                XdmValue value2 = new XdmValue(value.Where(Predicates.Eq(XdmAtomicValue.MakeAtomicValue("test1")).Or(Predicates.Eq(XdmAtomicValue.MakeAtomicValue("test2")))));
100
                string text1 = ((XdmAtomicValue)value2.ItemAt(0)).GetStringValue();
101
                Assert.AreEqual("test1", text1);
102
                Assert.AreEqual("test2", ((XdmAtomicValue)value2.ItemAt(1)).GetStringValue());
103

    
104
                //test 1a
105
                IEnumerable<XdmNode> valueNodes  = value.Where<XdmNode>(Predicates.HasLocalName("foo"));
106
                
107
                // test 2
108
                XdmValue value3 = new XdmValue(value.Where(i => i is XdmAtomicValue));
109
                Assert.AreEqual(2, value3.Count);
110

    
111

    
112
                // test 3
113
                IEnumerable<XdmItem> value4 = value.Where(new Saxon.Api.Predicate<XdmItem>(i => i is XdmAtomicValue));
114
                Assert.AreEqual(2, value4.Count());
115

    
116
                // test 3a
117
                Assert.True(value.AnyMatch(Predicates.IsNode()), "XdmValue contains a Node");
118

    
119
                // test 3b
120
                Assert.True(!value.AllMatch(Predicates.IsAtomic()), "XdmValue All Atomic");
121

    
122
                Assert.True(value.Where(Predicates.IsAtomic()).All(Predicates.IsAtomic().Func), "Should only contain Atomic values");
123
                //test3
124
                var iterx = foo.Select(Steps.Root());
125
                
126

    
127

    
128
                int count = iterx.Count();
129

    
130
                var myenum = iterx.GetEnumerator();
131
                while (myenum.MoveNext()) {
132
                    var root = myenum.Current;
133
                 
134

    
135
                }
136
                moved = myenum.MoveNext();
137

    
138
                
139
                //test 4a
140
                IEnumerator<XdmNode> textNodes = foo.Children(Predicates.IsText()).GetEnumerator();
141

    
142

    
143
                //test 4b
144
                Assert.True(Predicates.IsElement().Func(foo), "Check that node is an element node");
145
                moved = textNodes.MoveNext();
146

    
147
                //test 4c
148
                Assert.True(Predicates.IsText().Func(textNodes.Current), "Check that node is a text node");
149
                string text = textNodes.Current.StringValue;
150
                Assert.AreEqual("text", text);
151
            }
152
            catch (Exception e)
153
            {
154
                Assert.Fail(e.Message);
155
            }
156
        }
157

    
158
        /**
159
         * Test using declared variables
160
         */
161

    
162
        [Test]
163
        public void TestWithDeclaredVariables()
164
        {
165
            try
166
            {
167
                Processor p = new Processor(false);
168
                XPathCompiler c = p.NewXPathCompiler();
169
                c.DeclareVariable(new QName("a"));
170
                c.DeclareVariable(new QName("b"));
171
                XPathExecutable e = c.Compile("$a + $b");
172
                XPathSelector s = e.Load();
173
                s.SetVariable(new QName("a"), new XdmAtomicValue(2));
174
                s.SetVariable(new QName("b"), new XdmAtomicValue(2));
175
                Assert.AreEqual(4, ((XdmAtomicValue)s.EvaluateSingle()).GetLongValue());
176
            }
177
            catch (Exception e)
178
            {
179
                Assert.Fail(e.Message);
180
            }
181
        }
182

    
183
        /**
184
         * Test using undeclared variables
185
         */
186
        [Test]
187
        public void TestWithUndeclaredVariables()
188
        {
189
            try
190
            {
191
                Processor p = new Processor(false);
192
                XPathCompiler c = p.NewXPathCompiler();
193
                c.AllowUndeclaredVariables = (true);
194
                XPathExecutable e = c.Compile("$a + $b");
195
                XPathSelector s = e.Load();
196
                for (IEnumerator iq = e.EnumerateExternalVariables(); iq.MoveNext();)
197
                {
198
                    QName q = (QName)iq.Current;
199
                    s.SetVariable(q, new XdmAtomicValue(2));
200
                }
201
                Assert.AreEqual(4, ((XdmAtomicValue)s.EvaluateSingle()).GetLongValue());
202
            }
203
            catch (Exception e)
204
            {
205
                Assert.Fail(e.Message);
206
            }
207
        }
208

    
209
        /**
210
         * Test using undeclared variables when disallowed
211
         */
212
        [Test]
213
        public void TestWithDisallowedUndeclaredVariables()
214
        {
215
            try
216
            {
217
                Processor p = new Processor(false);
218
                XPathCompiler c = p.NewXPathCompiler();
219
                c.AllowUndeclaredVariables = (false);
220
                XPathExecutable e = c.Compile("$a + $b");
221
                XPathSelector s = e.Load();
222
                for (IEnumerator iq = e.EnumerateExternalVariables(); iq.MoveNext();)
223
                {
224
                    QName q = (QName)iq.Current;
225
                    s.SetVariable(q, new XdmAtomicValue(2));
226
                }
227
                Assert.Fail("No error reported for undeclared variables");
228
            }
229
            catch (Exception e)
230
            {
231
                Assert.True(true);
232
            }
233
        }
234

    
235

    
236
        /**
237
         * Test with statically-known collation
238
         */
239
        [Test]
240
        public void TestStaticallyKnownSaxonCollation()
241
        {
242
            try
243
            {
244
                Processor p = new Processor(false);
245
                XPathCompiler c = p.NewXPathCompiler();
246
                XPathExecutable e = c.Compile("compare('A', 'a', 'http://saxon.sf.net/collation?ignore-case=yes')");
247
                Assert.AreEqual(0, ((XdmAtomicValue)e.Load().EvaluateSingle()).GetLongValue());
248
            }
249
            catch (Exception e)
250
            {
251
                Assert.Fail(e.Message);
252
            }
253
        }
254

    
255
        /**
256
         * Test with dynamicall-computed collation
257
         */
258
        [Test]
259
        public void TestDynamicallyKnownSaxonCollation()
260
        {
261
            try
262
            {
263
                Processor p = new Processor(false);
264
                XPathCompiler c = p.NewXPathCompiler();
265
                XPathExecutable e = c.Compile("compare('A', 'a',  " +
266
                                                      "if (string(current-date()) eq '1900-01-01') then 'rubbish' else 'http://saxon.sf.net/collation?ignore-case=yes')");
267
                Assert.AreEqual(0, ((XdmAtomicValue)e.Load().EvaluateSingle()).GetLongValue());
268
            }
269
            catch (Exception e)
270
            {
271
                Assert.Fail(e.Message);
272
            }
273
        }
274

    
275
        /**
276
         * Test with function-lookup
277
         */
278
        [Test]
279
        public void TestFunctionLookup()
280
        {
281
            try
282
            {
283
                Processor p = new Processor(true);
284
                XPathCompiler c = p.NewXPathCompiler();
285
                XPathExecutable e = c.Compile("year-from-date(function-lookup(QName('http://www.w3.org/2001/XMLSchema', 'date'), 1)('2001-12-31'))");
286
                Assert.AreEqual(2001, ((XdmAtomicValue)e.Load().EvaluateSingle()).GetLongValue());
287
            }
288
            catch (Exception e)
289
            {
290
                Assert.Fail(e.Message);
291
            }
292
        }
293

    
294
        /**
295
         * Test for XPath 2.1 EQName syntax
296
         */
297
        [Test]
298
        public void TestEQNameAllowedIn30FunctionName()
299
        {
300
            try
301
            {
302
                Processor p = new Processor(true);
303
                XPathCompiler c = p.NewXPathCompiler();
304
                c.XPathLanguageVersion = ("3.0");
305
                XPathExecutable e = c.Compile("Q{http://www.w3.org/2001/XMLSchema}integer('1234')");
306
                XPathSelector s = e.Load();
307
                Assert.AreEqual(1234, ((XdmAtomicValue)s.EvaluateSingle()).GetLongValue());
308
            }
309
            catch (Exception e)
310
            {
311
                Assert.Fail(e.Message);
312
            }
313
        }
314

    
315
        [Test]
316
        public void TestEQNameAllowedIn30NameTest()
317
        {
318
            try
319
            {
320
                Processor p = new Processor(true);
321
                XPathCompiler c = p.NewXPathCompiler();
322
                c.XPathLanguageVersion = ("3.0");
323
                DocumentBuilder builder = p.NewDocumentBuilder();
324
                builder.BaseUri = new Uri("http://example.com");
325
                XdmNode root = builder.Build(new StringReader("<a:doc xmlns:a='urn:a'>93</a:doc>"));
326
                XPathExecutable e = c.Compile("data(/Q{urn:a}doc)");
327
                XPathSelector s = e.Load();
328
                s.ContextItem = (root);
329
                Assert.True(s.GetEnumerator().MoveNext(), "element not found");
330
            }
331
            catch (Exception e)
332
            {
333
                Assert.Fail(e.Message);
334
            }
335
        }
336

    
337
        /**
338
         * Test for XPath 3.0 EQName syntax
339
         */
340
        [Test]
341
        public void TestEQNameDisallowedIn20FunctionName()
342
        {
343
            try
344
            {
345
                Processor p = new Processor(false);
346
                XPathCompiler c = p.NewXPathCompiler();
347
                c.XPathLanguageVersion = ("3.0");
348
                c.Compile("'http://www.w3.org/2001/XMLSchema':integer('1234')");
349
                Assert.Fail("EQName syntax not allowed in 2.0, error not detected");
350
            }
351
            catch (Exception e) {
352
                // OK
353
            }
354
        }
355

    
356
        public void TestEQNameDisallowedIn20NameTest()
357
        {
358
            try
359
            {
360
                Processor p = new Processor(false);
361
                XPathCompiler c = p.NewXPathCompiler();
362
                c.XPathLanguageVersion = ("3.0");
363
                XdmNode root = p.NewDocumentBuilder().Build(new StringReader("<a:doc xmlns:a='urn:a'>93</a:doc>"));
364
                XPathExecutable e = c.Compile("data(/'urn:a':doc)");
365
                XPathSelector s = e.Load();
366
                s.ContextItem = root;
367
                Assert.Fail("EQName syntax not allowed in 2.0, error not detected");
368
            }
369
            catch (Exception e) {
370
                //OK
371
            }
372
        }
373

    
374

    
375
        /**
376
         * Test "let" expression allowed in XPath 3.0
377
         */
378
        [Test]
379
        public void TestLetAllowedIn21()
380
        {
381
            try
382
            {
383
                Processor p = new Processor(true);
384
                XPathCompiler c = p.NewXPathCompiler();
385
                c.XPathLanguageVersion = ("3.0");
386
                XPathExecutable e = c.Compile("let $n := 5 return 1229 + $n");
387
                XPathSelector s = e.Load();
388
                Assert.AreEqual(1234, ((XdmAtomicValue)s.EvaluateSingle()).GetLongValue());
389
            }
390
            catch (Exception e)
391
            {
392
                Assert.Fail(e.Message);
393
            }
394
        }
395

    
396
        /**
397
         * Test "let" expression disallowed in XPath 2.0
398
         */
399
        [Test]
400
        public void TestLetDisallowedIn20()
401
        {
402
            try
403
            {
404
                Processor p = new Processor(true);
405
                XPathCompiler c = p.NewXPathCompiler();
406
                c.XPathLanguageVersion = "2.0";
407
                XPathExecutable e = c.Compile("let $n := 5 return 1229 + $n");
408
                XPathSelector s = e.Load();
409
                Assert.Fail("No error reported for let in 2.0");
410
            }
411
            catch (Exception e)
412
            {
413
                // success
414
            }
415
        }
416

    
417
        /**
418
         * Test that it's OK (since 9.2) not to bind a value for a declared variable if the variable
419
         * isn't actually used
420
         */
421
        [Test]
422
        public void TestOkNotToBindUnusedButDeclaredVariable()
423
        {
424
            try
425
            {
426
                Processor p = new Processor(false);
427
                XPathCompiler c = p.NewXPathCompiler();
428
                c.DeclareVariable(new QName("vvv"));
429
                XPathExecutable e = c.Compile("2+2");
430
                XPathSelector s = e.Load();
431
                Assert.AreEqual(4, ((XdmAtomicValue)s.EvaluateSingle()).GetLongValue());
432
            }
433
            catch (Exception e)
434
            {
435
                Assert.Fail(e.Message);
436
            }
437
        }
438

    
439
        /**
440
         * Test that it's not OK not to bind a value for a declared variable if the variable
441
         * is actually used
442
         */
443
        [Test]
444
        public void TestFailureToBindVariable()
445
        {
446
            try
447
            {
448
                Processor p = new Processor(false);
449
                XPathCompiler c = p.NewXPathCompiler();
450
                c.DeclareVariable(new QName("vvv"));
451
                XPathExecutable e = c.Compile("2+$vvv");
452
                XPathSelector s = e.Load();
453
                XdmAtomicValue result = (XdmAtomicValue)s.Evaluate();
454
                Assert.Fail("undetected Assert.Failure to bind variable");
455
            }
456
            catch (Exception e)
457
            {
458
                // Assert.Failure expected
459
            }
460
        }
461

    
462
        /**
463
         * Test use of required context item type
464
         */
465
        [Test]
466
        public void TestContextItemTypeOk()
467
        {
468
            try
469
            {
470
                Processor p = new Processor(false);
471
                XPathCompiler c = p.NewXPathCompiler();
472
                c.ContextItemType = (XdmAtomicType.INTEGER);
473
                XPathExecutable e = c.Compile(".+3");
474
                XPathSelector s = e.Load();
475
                s.ContextItem = new XdmAtomicValue(2);
476
                XdmAtomicValue result = (XdmAtomicValue)s.Evaluate();
477
                Assert.AreEqual(result.GetLongValue(), 5);
478
            }
479
            catch (Exception e)
480
            {
481
                Assert.Fail(e.Message);
482
            }
483
        }
484

    
485
        public void TestContextItemTypeNotOk()
486
        {
487
            try
488
            {
489
                Processor p = new Processor(false);
490
                XPathCompiler c = p.NewXPathCompiler();
491
                c.ContextItemType = XdmAtomicType.STRING;
492
                XPathExecutable e = c.Compile("number(.)+3");
493
                XPathSelector s = e.Load();
494
                s.ContextItem = (new XdmAtomicValue(2));
495
                XdmAtomicValue result = (XdmAtomicValue)s.Evaluate();
496
                Assert.Fail("Context item is wrong type - not detected");
497
            }
498
            catch (Exception e)
499
            {
500

    
501
                // Assert.Failure expected
502
            }
503
        }
504

    
505
        [Test]
506
        public void TestContextItemTypeNotConverted()
507
        {
508
            try
509
            {
510
                Processor p = new Processor(false);
511
                XPathCompiler c = p.NewXPathCompiler();
512
                c.ContextItemType = (XdmAtomicType.DOUBLE);
513
                XPathExecutable e = c.Compile("number(.)+3");
514
                XPathSelector s = e.Load();
515
                s.ContextItem = new XdmAtomicValue(2);
516
                XdmAtomicValue result = (XdmAtomicValue)s.Evaluate();
517
                Assert.Fail("Context item is wrong type - not detected");
518
            }
519
            catch (Exception e)
520
            {
521

    
522
                // Assert.Failure expected
523
            }
524
        }
525

    
526
        [Test]
527
        public void TestContextItemTypeStaticError()
528
        {
529
            try
530
            {
531
                Processor p = new Processor(false);
532
                XPathCompiler c = p.NewXPathCompiler();
533
                c.ContextItemType = (XdmAtomicType.INTEGER);
534
                XPathExecutable e = c.Compile("substring(., 2, 5)");
535
                Assert.Fail("Context item is wrong type - not detected");
536
            }
537
            catch (Exception e)
538
            {
539
                Console.WriteLine(e.Message);
540
                // Assert.Failure expected
541
            }
542
        }
543

    
544

    
545

    
546

    
547
        [Test]
548
        public void TestStringLength()
549
        {
550
            // bug report from Norm Walsh, 2 Dec 2011
551
            try
552
            {
553
                String xpath = "boolean(string-length($href) > 0)";
554

    
555
                Processor processor = new Processor(false);
556
                Dictionary<String, String> nsBindings = new Dictionary<String, String>();
557
                Dictionary<QName, String> globals = new Dictionary<QName, String>();
558

    
559
                globals.Add(new QName("", "href"), "test");
560

    
561
                XdmNode doc = null;
562
                IList<XdmItem> results = new List<XdmItem>();
563

    
564
                XPathCompiler xcomp = processor.NewXPathCompiler();
565

    
566
                foreach (QName varname in globals.Keys)
567
                {
568
                    xcomp.DeclareVariable(varname);
569
                }
570

    
571
                foreach (String prefix in nsBindings.Keys)
572
                {
573
                    xcomp.DeclareNamespace(prefix, nsBindings[prefix]);
574
                }
575
                XPathExecutable xexec = xcomp.Compile(xpath);
576
                XPathSelector selector = xexec.Load();
577

    
578
                foreach (QName varname in globals.Keys)
579
                {
580
                    XdmAtomicValue value = new XdmAtomicValue("irrelevant");
581
                    selector.SetVariable(varname, value);
582
                }
583

    
584
                if (doc != null) {
585

    
586
                    selector.ContextItem = doc;
587
                }
588

    
589
                try
590
                {
591
                    foreach (XdmItem result in selector)
592
                    {
593
                        results.Add(result);
594
                    }
595
                }
596
                catch (Exception saue)
597
                {
598
                    throw saue;
599
                }
600

    
601
                Assert.AreEqual(1, results.Count);
602
                Assert.AreEqual("true", results[0].ToString());
603
            }
604
            catch (Exception e)
605
            {
606
                Assert.Fail();
607

    
608
            }
609
        }
610

    
611
        [Test]
612
        public void TestReflexiveExtension()
613
        {
614
            try
615
            {
616
                Processor proc = new Processor(true);
617
                XPathCompiler comp = proc.NewXPathCompiler();
618
                comp.DeclareNamespace("eg", "java:java.lang.Double");
619
                comp.DeclareVariable(new QName("arg"));
620
                XPathExecutable exp = comp.Compile("eg:toString($arg)");
621
                XPathSelector ev = exp.Load();
622
                ev.SetVariable(new QName("arg"), new XdmAtomicValue(1.0));
623
                XdmValue val = ev.Evaluate();
624
                String result = val.ToString();
625
                Assert.True(true);
626
            }
627
            catch (Exception err)
628
            {
629

    
630
                Assert.Fail(err.Message);
631
            }
632
        }
633

    
634
        
635

    
636
        [Test]
637
        public void TestReflexiveOverloadExtension()
638
        {
639
            try
640
            {
641
                Processor proc = new Processor(true);
642
                proc.BindExtensions("http://example.com", typeof(Extension.TestReflectionHelper)); //System.Type.GetType("Extension.TestReflectionHelper", true));
643
                //proc.SetProperty(FeatureKeys.TRACE_EXTERNAL_FUNCTIONS, "true");
644
                XPathCompiler comp = proc.NewXPathCompiler();
645
                comp.DeclareNamespace("this", "http://example.com");
646
                comp.DeclareNamespace("arg1", "clitype:System.Object");
647
                comp.DeclareNamespace("arg2", "clitype:System.String");
648
                comp.DeclareVariable(new QName("arg1"));
649
                comp.DeclareVariable(new QName("arg2"));
650
                XPathExecutable exp = comp.Compile("string-join(this:p($arg1,$arg2), this:q())");
651
                //XPathExecutable exp = comp.Compile("this:q()");
652
                XPathSelector ev = exp.Load();
653
                ev.SetVariable(new QName("arg1"), new XdmExternalObjectValue((Object)"test1"));
654
                ev.SetVariable(new QName("arg2"), XdmAtomicValue.MakeAtomicValue("test1"));
655
                XdmValue val = ev.Evaluate();
656
                String result = val.ToString();
657
                Assert.True(result.Contains("method, content"), result);
658
                //Assert.True(result.Contains("bar-foo"), result);
659
            }
660
            catch (Exception err)
661
            {
662
                Console.Error.WriteLine(err.StackTrace);
663

    
664
                Assert.Fail(err.Message);
665
            }
666
        }
667

    
668

    
669
        [Test]
670
    public void TestSchemaAwareEqualityGood()
671
{
672
    try
673
    {
674
        bool result = trySchemaAwareEquality(true);
675
        Assert.True(result, "result");
676
    }
677
    catch (Exception e)
678
    {
679
      
680
        Assert.Fail(e.Message);
681
    }
682
}
683

    
684
        [Test]
685
public void TestSchemaAwareEqualityBad()
686
{
687
    try
688
    {
689
        trySchemaAwareEquality(false);
690
        Assert.Fail();
691
    }
692
    catch (Exception e)
693
    {
694
        // success
695
    }
696
}
697

    
698
private bool trySchemaAwareEquality(bool isSchemaAware)
699
{
700

    
701
    Processor processor = new Processor(true);
702
SchemaManager sm = processor.SchemaManager;
703
String xsd = "<xs:schema xmlns:xs=\"http://www.w3.org/2001/XMLSchema\" \n" +
704
        "           targetNamespace=\"http://www.xpathtest.com/test\" \n" +
705
        "           xmlns:test=\"http://www.xpathtest.com/test\" \n" +
706
        "           elementFormDefault=\"qualified\">\n" +
707
        "  <xs:element name=\"comparables\">\n" +
708
        "    <xs:complexType>\n" +
709
        "      <xs:sequence>\n" +
710
        "        <xs:element ref=\"test:comparable\" maxOccurs=\"unbounded\"/>\n" +
711
        "      </xs:sequence>\n" +
712
        "    </xs:complexType>\n" +
713
        "  </xs:element>\n" +
714
        "  <xs:element name=\"comparable\">\n" +
715
        "    <xs:complexType>\n" +
716
        "      <xs:sequence>\n" +
717
        "        <xs:element name=\"string\" type=\"xs:string\"/>\n" +
718
        "        <xs:element name=\"int\" type=\"xs:integer\"/>\n" +
719
        "      </xs:sequence>\n" +
720
        "    </xs:complexType>\n" +
721
        "  </xs:element>\n" +
722
        "</xs:schema>";
723
sm.Compile(new MemoryStream(Encoding.ASCII.GetBytes(xsd)), new Uri("http://www.example.com"));
724
        SchemaValidator sv = sm.NewSchemaValidator();
725
XdmDestination xdmDest = new XdmDestination();
726
sv.SetDestination(xdmDest);
727
        String xml = "<comparables xmlns=\"http://www.xpathtest.com/test\">\n" +
728
                "  <comparable>\n" +
729
                "    <string>hello</string>\n" +
730
                "    <int>1</int>\n" +
731
                "  </comparable>\n" +
732
                "  <comparable>\n" +
733
                "    <string>hello</string>\n" +
734
                "    <int>1</int>\n" +
735
                "  </comparable>\n" +
736
                "</comparables>";
737
sv.SetSource(new MemoryStream(Encoding.UTF8.GetBytes(xml)), new Uri("http://www.example.com"));
738
            sv.Run();
739
            String xpathExpr = "//test:comparable[1]/test:int eq //test:comparable[2]/test:int";
740
XPathCompiler comp = processor.NewXPathCompiler();
741
        if (isSchemaAware) {
742
            comp.SchemaAware = true;
743
        }
744
        comp.DeclareNamespace("test", "http://www.xpathtest.com/test");
745
        XPathExecutable xpathExec = comp.Compile(xpathExpr);
746
        XPathSelector selector = xpathExec.Load();
747
        selector.ContextItem =xdmDest.XdmNode;
748
        return selector.EffectiveBooleanValue();
749

    
750
    }
751

    
752

    
753
        [Test]
754
        public void TestExPathUNCPaths() {
755
            try
756
            {
757
               // new DotNetQuery().Run();
758
               
759
                 Processor p = new Processor(true);
760
                 XPathCompiler c = p.NewXPathCompiler();
761
                 c.XPathLanguageVersion = ("3.1");
762
                 c.DeclareNamespace("file","http://expath.org/ns/file");
763
                 XPathExecutable e = c.Compile("file:exists('//crayfish/team/xmark/results8-8.xml')");
764
                 XPathSelector s = e.Load();
765
                 bool result = s.EffectiveBooleanValue();
766
                 Assert.True(result);
767
            }
768
            catch (Exception e)
769
            {
770
                Assert.Fail(e.Message);
771
            }
772
            /*
773
            try
774
            {
775
                Processor p = new Processor(true);
776
                XQueryCompiler c = p.NewXQueryCompiler();
777
               // c.XPathLanguageVersion = ("3.1");
778
                c.DeclareNamespace("file", "http://expath.org/ns/file");
779
                c.BaseUri = "file://crayfish/team/xmark";
780
                XQueryExecutable e = c.Compile("doc('file://crayfish/team/xmark/result8-8.xml')");
781
                //XPathExecutable e = c.Compile("file:read-text('https://www.saxonica.com/welcome/welcome.xml')");
782
                XQueryEvaluator s = e.Load();
783
                XdmItem result = s.EvaluateSingle();
784
                Assert.True(((XdmAtomicValue)result).GetBooleanValue());
785
            }
786
            catch (Exception e)
787
            {
788
                Assert.Fail(e.Message);
789
            }
790
            */
791

    
792
        }
793

    
794

    
795
        [Test]
796
        public void TestDecimalFormat()
797
{
798
    try
799
    {
800
        Processor p = new Processor(false);
801
        XPathCompiler c = p.NewXPathCompiler();
802
        c.XPathLanguageVersion = ("3.1");
803
        c.SetDecimalFormatProperty(new QName("deci"), "decimal-separator", "~");
804
        c.SetDecimalFormatProperty(new QName("deci"), "grouping-separator", "|");
805
        c.SetDecimalFormatProperty(new QName("deci"), "exponent-separator", "E");
806
        XPathExecutable e = c.Compile("format-number(123.4, '0|000~0E0', 'deci')");
807
        XPathSelector s = e.Load();
808
        String result = s.EvaluateSingle().GetStringValue();
809
        Assert.True(result.Equals("1|234~0E-1"));
810
    }
811
    catch (Exception e)
812
    {
813
        Assert.Fail(e.Message);
814
    }
815
}
816

    
817
        [Test]
818
public void TestBug3188()
819
{
820

    
821
    String xpathString = "/xbrli:xbrl/xbrli:context[xbrli:scenario/xbrldi:typedMember[fn:resolve-QName(string(@dimension), .) =\n"
822
            + "  (xs:QName('ocw-dim:EquityIndividualSpecificationAxis'),\n"
823
            + "   xs:QName('ocw-dim:PropertyPlantEquipmentDepreciationSpecificationAxis'),\n"
824
            + "   xs:QName('ocw-dim:RealEstateEconomicValueSpecificationAxis'),\n"
825
            + "   xs:QName('ocw-dim:ContingentLiabilitiesSpecificationAxis'),\n"
826
            + "   xs:QName('ocw-dim:ContingentAssetsSpecificationAxis'),\n"
827
            + "   xs:QName('ocw-dim:ProvisionsIndividualSpecificationAxis'),\n"
828
            + "   xs:QName('ocw-dim:LiabilitiesNoncurrentIndividualSpecificationAxis'),\n"
829
            + "   xs:QName('ocw-dim:RelatedPartySpecificationAxis'),\n"
830
            + "   xs:QName('ocw-dim:GrantSpecificationAxis'),\n"
831
            + "   xs:QName('ocw-dim:ProjectSubsidyFinancialSpecificationAxis'))]]";
832
    try
833
    {
834
        Processor processor = new Processor(true);
835

    
836
        DocumentBuilder docBuilder = processor.NewDocumentBuilder();
837
        FileStream inputFile = new FileStream(ConfigTest.DATA_DIR + "xbrlInstance.xml", FileMode.Open);
838
                docBuilder.BaseUri = new Uri(ConfigTest.DATA_DIR + "xbrlInstance.xml");
839
        XdmItem contextItem = docBuilder.Build(inputFile);
840

    
841
        XPathCompiler xpathCompiler = processor.NewXPathCompiler();
842
        xpathCompiler.DeclareNamespace("fn", "http://www.w3.org/2005/xpath-functions");
843
        xpathCompiler.DeclareNamespace("xbrli", "http://www.xbrl.org/2003/instance");
844
        xpathCompiler.DeclareNamespace("xbrldi", "http://xbrl.org/2006/xbrldi");
845
        xpathCompiler.DeclareNamespace("ocw-dim", "http://www.nltaxonomie.nl/nt11/ocw/20170301.b/dictionary/ocw-axes");
846

    
847
        XdmValue result = xpathCompiler.Evaluate(xpathString, contextItem);
848
        Assert.True(result is XdmNode);
849
        Assert.AreEqual(((XdmNode)result).NodeKind, XmlNodeType.Element);
850
        Assert.True("context".Equals(((XdmNode)result).NodeName.LocalName));
851
    }
852
    catch (Exception e)
853
    {
854
       
855
        Assert.Fail(e.Message);
856
    }
857
}
858

    
859
        /**
860
         * round#2 is rejected under version 2.0
861
         */
862
        [Test]
863
        public void TestAbsent30Function()
864
{
865
    try
866
    {
867
        Processor p = new Processor(false);
868
        XPathCompiler c = p.NewXPathCompiler();
869
        c.XPathLanguageVersion = ("2.0");
870
        XPathExecutable e = c.Compile("round(1.234, 2)");
871
        XdmItem result = e.Load().EvaluateSingle();
872
        Assert.Fail("unexpected success");
873
    }
874
    catch (StaticError e)
875
    {
876
        Assert.AreEqual("XPST0017", e.ErrorCode.LocalName);
877
    }
878
}
879

    
880

    
881

    
882

    
883
    }
884
}
885

    
(13-13/19)