Project

Profile

Help

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

he / latest10 / hec / samples / php / xslt30_PHPUnit.php @ c1cc9bd8

1
<?php
2
declare(strict_types=1);
3

    
4
use PHPUnit\Framework\TestCase;
5

    
6
final class XSLT30Test extends TestCase
7
{
8

    
9
    protected static $saxonProc;
10

    
11
    public static function setUpBeforeClass(): void
12
    {
13
        self::$saxonProc = new Saxon\SaxonProcessor(False);
14
    }
15

    
16

    
17
    public function testCreateXslt30ProcessorObject(): void
18
    {
19

    
20

    
21
        $proc = self::$saxonProc->newXslt30Processor();
22

    
23

    
24
        $this->assertInstanceOf(
25
            Saxon\Xslt30Processor::class,
26
            $proc
27
        );
28
    }
29

    
30
    /*  public function testCannotBeCreatedFromInvalidEmailAddress(): void
31
      {
32
          $this->expectException(InvalidArgumentException::class);
33

34
          Email::fromString('invalid');
35
      }*/
36

    
37
    public function testVersion(): void
38
    {
39
        $this->assertStringContainsString(
40
            'Saxon/C 1.2.1 running',
41
            self::$saxonProc->version()
42
        );
43
        $this->assertStringContainsString('9.9.1.5C from Saxonica', self::$saxonProc->version());
44
    }
45

    
46

    
47
    public function testSchemaAware(): void
48
    {
49
        $this->assertFalse(
50
            self::$saxonProc->isSchemaAware()
51
        );
52
    }
53

    
54

    
55
    public function testSchemaAware2(): void
56
    {
57

    
58
        $saxonProc2 = new Saxon\SaxonProcessor(True);
59

    
60
        $this->assertFalse($saxonProc2->isSchemaAware());
61
    }
62

    
63
    public function testContextNotRoot(): void
64
    {
65

    
66
        $trans = self::$saxonProc->newXslt30Processor();
67
        $node = self::$saxonProc->parseXmlFromString("<doc><e>text</e></doc>");
68

    
69
        $trans->compileFromString("<xsl:stylesheet version='2.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform'><xsl:variable name='x' select='.'/><xsl:template match='/'>errorA</xsl:template><xsl:template match='e'>[<xsl:value-of select='name(\$x)'/>]</xsl:template></xsl:stylesheet>");
70
        $this->assertNotNull($node);
71
        $this->assertInstanceOf(
72
            Saxon\XdmNode::class,
73
            $node
74
        );
75
        $this->assertTrue($node->getChildCount() > 0);
76
        $this->assertNotNull($node);
77
        $eNode = $node->getChildNode(0)->getChildNode(0);
78
        $this->assertNotNull($eNode);
79
        $trans->setGlobalContextItem($node);
80
        $trans->setInitialMatchSelection($eNode);
81
        $result = $trans->applyTemplatesReturningString();
82

    
83
        $this->assertStringContainsString("[", $result);
84

    
85

    
86
    }
87

    
88

    
89
    public function testResolveUri(): void
90
    {
91

    
92
        $transformer = self::$saxonProc->newXslt30Processor();
93

    
94
        $transformer->compileFromString("<xsl:stylesheet version='3.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform' xmlns:xs='http://www.w3.org/2001/XMLSchema' xmlns:err='http://www.w3.org/2005/xqt-errors'><xsl:template name='go'><xsl:try><xsl:variable name='uri' as='xs:anyURI' select=\"resolve-uri('notice trailing space /out.xml')\"/> <xsl:message select='\$uri'/><xsl:result-document href='{\$uri}'><out/></xsl:result-document><xsl:catch><xsl:sequence select=\"'\$err:code: ' || \$err:code  || ', \$err:description: ' || \$err:description\"/></xsl:catch></xsl:try></xsl:template></xsl:stylesheet>");
95

    
96

    
97
        $value = $transformer->callTemplateReturningValue("go");
98

    
99
        $item = $value->getHead();
100
        $this->assertStringContainsString("code", $item->getStringValue());
101

    
102
    }
103

    
104

    
105
    public function testEmbeddedStylesheet(): void
106
    {
107

    
108
        $transformer = self::$saxonProc->newXslt30Processor();
109

    
110
        // Load the source document
111
        $input = self::$saxonProc->parseXmlFromFile("../data/books.xml");
112
        //Console.WriteLine("=============== source document ===============");
113
        //Console.WriteLine(input.OuterXml);
114
        //Console.WriteLine("=========== end of source document ============");
115

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

    
119
        $path = "/processing-instruction(xml-stylesheet)[matches(.,'type\\s*=\\s*[''\"\"]text/xsl[''\" \"]')]/replace(., '.*?href\\s*=\\s*[''\" \"](.*?)[''\" \"].*', '$1')";
120

    
121
        $xPathProcessor = self::$saxonProc->newXPathProcessor();
122
        $xPathProcessor->setContextItem($input);
123
        $hrefval = $xPathProcessor->evaluateSingle($path);
124
        $this->assertNotNull($hrefval);
125
        echo "test cp0";
126
        $this->assertInstanceOf(Saxon\XdmItem::class, $hrefval);
127
        $this->assertTrue($hrefval->isAtomic());
128
        $href = $hrefval->getAtomicValue()->getStringValue();
129
        $this->assertNotEquals($href, "");
130
        // The stylesheet is embedded in the source document and identified by a URI of the form "#id"
131

    
132
        $transformer->compileFromFile("../styles/".$href);
133

    
134

    
135
        $this->assertInstanceOf(Saxon\XdmNode::class, $input);
136
        // Run it
137
        $node = $transformer->transformToValue($input);
138
        if ($transformer->getExceptionCount() > 0) {
139
            echo $transformer->getErrorMessage(0);
140
        }
141
        $this->assertNotNull($node);
142

    
143
        if ($transformer->getExceptionCount() > 0) {
144
            echo $transformer->getErrorMessage(0);
145
        }
146

    
147
    }
148

    
149

    
150
    public function testContextNotRootNamedTemplate(): void
151
    {
152

    
153
        $transformer = self::$saxonProc->newXslt30Processor();
154
        $node = self::$saxonProc->parseXmlFromString("<doc><e>text</e></doc>");
155

    
156

    
157
        $transformer->compileFromString("<xsl:stylesheet version='2.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform'><xsl:variable name='x' select='.'/><xsl:template match='/'>errorA</xsl:template><xsl:template name='main'>[<xsl:value-of select='name(\$x)'/>]</xsl:template></xsl:stylesheet>");
158
        $transformer->setGlobalContextItem($node);
159
        $result = $transformer->callTemplateReturningValue("main");
160
        if ($transformer->getExceptionCount() > 0) {
161
            echo $transformer->getErrorMessage(0);
162
        }
163

    
164
        $this->assertNotNull($result);
165
        if ($result->getHead() != NULL) {
166
            $this->assertStringContainsString("[]", $result->getHead()->getStringValue());
167
        }
168

    
169

    
170
        $result2 = $transformer->callTemplateReturningString("main");
171
        if ($transformer->getExceptionCount() > 0) {
172
            echo $transformer->getErrorMessage(0);
173
        }
174

    
175
        $this->assertNotNull($result2);
176
        $this->assertStringContainsString("[]", $result2);
177

    
178
    }
179

    
180

    
181
    public function testUseAssociated(): void
182
    {
183

    
184
        $transformer = self::$saxonProc->newXslt30Processor();
185

    
186

    
187
        $foo_xml = "trax/xml/foo.xml";
188
        $transformer->compileFromAssociatedFile($foo_xml);
189
        $transformer->setInitialMatchSelectionAsFile($foo_xml);
190
        $result = $transformer->applyTemplatesReturningString();
191

    
192
        $this->assertNotNull($result);
193

    
194
    }
195

    
196
    public function testTransformWithoutArgument1(): void
197
    {
198

    
199
        $transformer = self::$saxonProc->newXslt30Processor();
200

    
201

    
202
        $foo_xml = "trax/xml/foo.xml";
203
        $transformer->compileFromAssociatedFile($foo_xml);
204
        $transformer->setInitialMatchSelectionAsFile($foo_xml);
205
        $result = $transformer->transformToString();
206

    
207
        $this->assertNotNull($result);
208

    
209
    }
210

    
211
    public function testTransformWithoutArgument2(): void
212
    {
213

    
214
        $transformer = self::$saxonProc->newXslt30Processor();
215

    
216

    
217
        $foo_xml = "trax/xml/foo.xml";
218
        $transformer->compileFromAssociatedFile($foo_xml);
219
        $transformer->setInitialMatchSelectionAsFile($foo_xml);
220
        $resultValue = $transformer->transformToValue();
221
        $this->assertNotNull($resultValue);
222
        $resultItem = $resultValue->getHead();
223
        $this->assertNotNull($resultItem);
224
        $result = $resultItem->getStringValue();
225
        $this->assertNotNull($result);
226

    
227
    }
228

    
229

    
230

    
231
    public function testTransformWithoutArgument3(): void
232
    {
233

    
234
        $transformer = self::$saxonProc->newXslt30Processor();
235

    
236

    
237
        $foo_xml = "trax/xml/foo.xml";
238
        $transformer->compileFromAssociatedFile($foo_xml);
239
        $transformer->setInitialMatchSelectionAsFile($foo_xml);
240
        $transformer->setOutputFile("resultForTransformWithoutArgument.xml");
241
        $transformer->transformToFile();
242
        $this->assertTrue(file_exists("resultForTransformWithoutArgument.xml"));
243

    
244
                if (file_exists("resultForTransformWithoutArgument.xml")) {
245
                    unlink("resultForTransformWithoutArgument.xml");
246
                }
247

    
248
    }
249

    
250

    
251
    public function testNullStylesheet(): void
252
    {
253

    
254
        $transformer = self::$saxonProc->newXslt30Processor();
255
        $result = $transformer->applyTemplatesReturningString();
256

    
257
        $this->assertNull($result);
258
    }
259

    
260

    
261
    public function testXdmDestination(): void
262
    {
263
        $transformer = self::$saxonProc->newXslt30Processor();
264
        $transformer->compileFromString("<xsl:stylesheet version='2.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>" .
265
            "<xsl:template name='go'><a/></xsl:template>" .
266
            "</xsl:stylesheet>");
267

    
268
        $root = $transformer->callTemplateReturningValue("go");
269
        $this->assertNotNull($root);
270
        $this->assertNotNull($root->getHead());
271
        $node = $root->getHead()->getNodeValue();
272
        $this->assertTrue($node->getNodeKind() == 9, "result is document node");
273
    }
274

    
275
    public function testXdmDestinationWithItemSeparator(): void
276
    {
277
        $transformer = self::$saxonProc->newXslt30Processor();
278
        $transformer->compileFromString("<xsl:stylesheet version='2.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>" .
279
            "<xsl:template name='go'><xsl:comment>A</xsl:comment><out/><xsl:comment>Z</xsl:comment></xsl:template>" .
280
            "<xsl:output method='xml' item-separator='§'/>" .
281
            "</xsl:stylesheet>");
282

    
283
        $root = $transformer->callTemplateReturningValue("go");
284
        $node = $root->getHead()->getNodeValue();
285
        $this->assertEquals("<!--A-->§<out/>§<!--Z-->", $node);
286
        $this->assertTrue($node->getNodeKind() == 9, "result is document node");
287

    
288

    
289
    }
290

    
291

    
292
    public function testPipeline(): void
293
    {
294
        $transformer = self::$saxonProc->newXslt30Processor();
295

    
296
        $xsl = "<xsl:stylesheet version='2.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>" .
297
            "<xsl:template match='/'><a><xsl:copy-of select='.'/></a></xsl:template>" .
298
            "</xsl:stylesheet>";
299
        $xml = "<z/>";
300
        $stage1 = self::$saxonProc->newXslt30Processor();
301
        $stage1->compileFromString($xsl);
302
        $in = self::$saxonProc->parseXmlFromString($xml);
303

    
304
        $stage2 = self::$saxonProc->newXslt30Processor();
305
        $stage2->compileFromString($xsl);
306
        $stage3 = self::$saxonProc->newXslt30Processor();
307
        $stage3->compileFromString($xsl);
308

    
309
        $stage4 = self::$saxonProc->newXslt30Processor();
310
        $stage4->compileFromString($xsl);
311

    
312
        $stage5 = self::$saxonProc->newXslt30Processor();
313
        $stage5->compileFromString($xsl);
314

    
315
        $this->assertNotNull($xsl, "\$xsl check");
316
        $this->assertNotNull($in, "\$in check");
317
        $stage1->setProperty("!omit-xml-declaration", "yes");
318
        $stage1->setProperty("!indent", "no");
319
        $stage1->setInitialMatchSelection($in);
320

    
321
        $d1 = $stage1->applyTemplatesReturningValue();
322
        if ($stage1->getExceptionCount() > 0) {
323
            echo $stage1->getErrorMessage(0);
324
        }
325
        $this->assertNotNull($d1, "\$d1 check");
326
        $stage2->setProperty("!omit-xml-declaration", "yes");
327
        $stage2->setProperty("!indent", "no");
328
        $stage2->setInitialMatchSelection($d1);
329
        $d2 = $stage2->applyTemplatesReturningValue();
330
        $this->assertNotNull($d2, "\$d2");
331
        $stage3->setProperty("!omit-xml-declaration", "yes");
332
        $stage3->setProperty("!indent", "no");
333
        $stage3->setInitialMatchSelection($d2);
334
        $d3 = $stage3->applyTemplatesReturningValue();
335
        $this->assertNotNull($d3, "\$d3 check");
336
        $stage4->setProperty("!omit-xml-declaration", "yes");
337
        $stage4->setProperty("!indent", "no");
338
        $stage4->setInitialMatchSelection($d3);
339
        $d4 = $stage4->applyTemplatesReturningValue();
340
        $this->assertNotNull($d3, "\$d4 check");
341
        $stage5->setProperty("!indent", "no");
342
        $stage5->setProperty("!omit-xml-declaration", "yes");
343
        $stage5->setInitialMatchSelection($d4);
344
        $sw = $stage5->applyTemplatesReturningString();
345
        $this->assertNotNull($sw, "\$sw check");
346
        $this->assertStringContainsString($sw, "<a><a><a><a><a><z/></a></a></a></a></a>");
347

    
348
    }
349

    
350

    
351
    public function testPipelineShort(): void
352
    {
353
        $transformer = self::$saxonProc->newXslt30Processor();
354

    
355
        $xsl = "<xsl:stylesheet version='2.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>" .
356
            "<xsl:template match='/'><a><xsl:copy-of select='.'/></a></xsl:template>" .
357
            "</xsl:stylesheet>";
358
        $xml = "<z/>";
359
        $stage1 = self::$saxonProc->newXslt30Processor();
360
        $stage2 = self::$saxonProc->newXslt30Processor();
361

    
362

    
363
        $stage1->compileFromString($xsl);
364
        $stage2->compileFromString($xsl);
365

    
366
        $this->assertNotNull($xsl);
367
        $stage1->setProperty("!omit-xml-declaration", "yes");
368
        $stage2->setProperty("!omit-xml-declaration", "yes");
369
        $in = self::$saxonProc->parseXmlFromString($xml);
370
        $this->assertNotNull($in, "\$sin check");
371
        $stage1->setInitialMatchSelection($in);
372
        $out = $stage1->applyTemplatesReturningValue();
373
        $this->assertNotNull($out, "\$out check");
374
        $stage2->setInitialMatchSelection($out);
375
        $sw = $stage2->applyTemplatesReturningString();
376

    
377
        $this->assertStringContainsString($sw, "<a><a><z/></a></a>");
378

    
379
    }
380

    
381
    /*
382
        public function testSchemaAware11(): void {
383
            // Create a Processor instance.
384
            try {
385

386
                Processor proc = new Processor(true);
387
                proc.setConfigurationProperty(FeatureKeys.XSD_VERSION, "1.1");
388
                Xslt30Processor transformer = new Xslt30Processor(proc);
389
                transformer.compileFromString(null,
390
                        "<xsl:stylesheet version='2.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>" +
391
                                "<xsl:import-schema><xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema'>" +
392
                                "<xs:element name='e'><xs:complexType><xs:sequence><xs:element name='p'/></xs:sequence><xs:assert test='exists(p)'/></xs:complexType></xs:element>" +
393
                                "</xs:schema></xsl:import-schema>" +
394
                                "<xsl:variable name='v'><e><p/></e></xsl:variable>" +
395
                                "<xsl:template name='main'><xsl:copy-of select='$v' validation='strict'/></xsl:template>" +
396
                                "</xsl:stylesheet>", null, null);
397

398

399
                String[] params = new String[]{"!indent"};
400
                Object[] values = new Object[]{"no"};
401

402
                String sw = transformer.callTemplateReturningString(null, null,  "main", params, values);
403
                assertTrue(sw.contains("<e>"));
404
            } catch (SaxonApiException e) {
405
                e.printStackTrace();
406
                fail(e.getMessage());
407
            }
408

409
        }
410
    */
411

    
412
    public function testCallFunction()
413
    {
414

    
415
        $source = "<?xml version='1.0'?>" .
416
            "                <xsl:stylesheet xmlns:xsl='http://www.w3.org/1999/XSL/Transform'" .
417
            "                xmlns:xs='http://www.w3.org/2001/XMLSchema'" .
418
            "                xmlns:f='http://localhost/'" .
419
            "                version='3.0'>" .
420
            "                <xsl:function name='f:add' visibility='public'>" .
421
            "                  <xsl:param name='a'/><xsl:param name='b'/>" .
422
            "                  <xsl:sequence select='\$a + \$b'/></xsl:function>" .
423
            "                </xsl:stylesheet>";
424
        $transformer = self::$saxonProc->newXslt30Processor();
425

    
426
        $transformer->compileFromString($source);
427
        $paramArr = array(self::$saxonProc->createAtomicValue(2), self::$saxonProc->createAtomicValue(3));
428
        $v = $transformer->callFunctionReturningValue("{http://localhost/}add", $paramArr);
429
        $this->assertInstanceOf(Saxon\XdmItem::class, $v->getHead());
430
        $this->assertTrue($v->getHead()->isAtomic());
431
        $this->assertEquals($v->getHead()->getAtomicValue()->getLongValue(), 5);
432

    
433
    }
434

    
435

    
436
    public function testCallFunctionArgConversion()
437
    {
438
        $transformer = self::$saxonProc->newXslt30Processor();
439

    
440
        $source = "<?xml version='1.0'?>" .
441
            "                <xsl:stylesheet xmlns:xsl='http://www.w3.org/1999/XSL/Transform'" .
442
            "                xmlns:xs='http://www.w3.org/2001/XMLSchema'" .
443
            "                xmlns:f='http://localhost/'" .
444
            "                version='3.0'>" .
445
            "                <xsl:function name='f:add' visibility='public'>" .
446
            "                   <xsl:param name='a' as='xs:double'/>" .
447
            "                   <xsl:param name='b' as='xs:double'/>" .
448
            "                   <xsl:sequence select='\$a + \$b'/>" .
449
            "                </xsl:function>" .
450
            "                </xsl:stylesheet>";
451

    
452
        $transformer->compileFromString($source);
453

    
454
        $v = $transformer->callFunctionReturningValue("{http://localhost/}add", array(self::$saxonProc->createAtomicValue(2), self::$saxonProc->createAtomicValue(3)));
455
        $this->assertInstanceOf(Saxon\XdmItem::class, $v->getHead());
456
        $this->assertTrue($v->getHead()->isAtomic());
457
        $this->assertEquals($v->getHead()->getAtomicValue()->getDoubleValue(), 5.0e0);
458
        $this->assertStringContainsString("double", $v->getHead()->getAtomicValue()->getPrimitiveTypeName());
459

    
460
    }
461

    
462

    
463
    public function testCallFunctionWrapResults()
464
    {
465

    
466
        $transformer = self::$saxonProc->newXslt30Processor();
467

    
468
        $source = "<?xml version='1.0'?>" .
469
            "                <xsl:stylesheet xmlns:xsl='http://www.w3.org/1999/XSL/Transform'" .
470
            "                xmlns:xs='http://www.w3.org/2001/XMLSchema'" .
471
            "                xmlns:f='http://localhost/'" .
472
            "                version='3.0'>" .
473
            "                <xsl:param name='x' as='xs:integer'/>" .
474
            "                <xsl:param name='y' select='.+2'/>" .
475
            "                <xsl:function name='f:add' visibility='public'>" .
476
            "                   <xsl:param name='a' as='xs:double'/>" .
477
            "                   <xsl:param name='b' as='xs:double'/>" .
478
            "                   <xsl:sequence select='\$a + \$b + \$x + \$y'/>" .
479
            "                </xsl:function>" .
480
            "                </xsl:stylesheet>";
481

    
482
        $transformer->compileFromString($source);
483

    
484
        $transformer->setProperty("!omit-xml-declaration", "yes");
485
        $transformer->setParameter("x", self::$saxonProc->createAtomicValue(30));
486
        $transformer->setGlobalContextItem(self::$saxonProc->createAtomicValue(20));
487

    
488
        $sw = $transformer->callFunctionReturningString("{http://localhost/}add", array(self::$saxonProc->createAtomicValue(2), self::$saxonProc->createAtomicValue(3)));
489

    
490
        $this->assertEquals("57", $sw);
491
    }
492

    
493

    
494
    public function testCallFunctionArgInvalid()
495
    {
496
        $transformer = self::$saxonProc->newXslt30Processor();
497

    
498
        $source = "<?xml version='1.0'?>" .
499
            "                <xsl:stylesheet xmlns:xsl='http://www.w3.org/1999/XSL/Transform'" .
500
            "                xmlns:xs='http://www.w3.org/2001/XMLSchema'" .
501
            "                xmlns:f='http://localhost/'" .
502
            "                version='2.0'>" .
503
            "                <xsl:function name='f:add'>" .
504
            "                   <xsl:param name='a' as='xs:double'/>" .
505
            "                   <xsl:param name='b' as='xs:double'/>" .
506
            "                   <xsl:sequence select='\$a + \$b'/>" .
507
            "                </xsl:function>" .
508
            "                </xsl:stylesheet>";
509

    
510
        $transformer->compileFromString($source);
511
        $v = $transformer->callFunctionReturningValue("{http://localhost/}add", array(self::$saxonProc->createAtomicValue(2), self::$saxonProc->createAtomicValue(3)));
512

    
513
        $this->assertTrue($transformer->getExceptionCount() == 1);
514
        $this->assertStringContainsString("Cannot invoke function add#2 externally", $transformer->getErrorMessage(0));
515
        $this->assertNull($v);
516
    }
517

    
518

    
519
    public function testCallNamedTemplateWithTunnelParams()
520
    {
521
        $transformer = self::$saxonProc->newXslt30Processor();
522

    
523
        $source = "<?xml version='1.0'?>" .
524
            "                <xsl:stylesheet xmlns:xsl='http://www.w3.org/1999/XSL/Transform'" .
525
            "                xmlns:xs='http://www.w3.org/2001/XMLSchema'" .
526
            "                version='3.0'>" .
527
            "                <xsl:template name='t'>" .
528
            "                   <xsl:call-template name='u'/>" .
529
            "                </xsl:template>" .
530
            "                <xsl:template name='u'>" .
531
            "                   <xsl:param name='a' as='xs:double' tunnel='yes'/>" .
532
            "                   <xsl:param name='b' as='xs:float' tunnel='yes'/>" .
533
            "                   <xsl:sequence select='\$a + \$b'/>" .
534
            "                </xsl:template>" .
535
            "                </xsl:stylesheet>";
536

    
537
        $transformer->compileFromString($source);
538
        $transformer->setProperty("!omit-xml-declaration", "yes");
539
        $transformer->setProperty("tunnel", "true");
540
        $transformer->setInitialTemplateParameters(array("a" => self::$saxonProc->createAtomicValue(12), "b" => self::$saxonProc->createAtomicValue(5)));
541
        $sw = $transformer->callTemplateReturningString("t");
542
        $this->assertNotNull($sw);
543
        $this->assertEquals("17", $sw);
544

    
545
    }
546

    
547
    public function testCallTemplateRuleWithParams()
548
    {
549
        $transformer = self::$saxonProc->newXslt30Processor();
550

    
551
        $source = "<?xml version='1.0'?>" .
552
            "                <xsl:stylesheet xmlns:xsl='http://www.w3.org/1999/XSL/Transform'" .
553
            "                xmlns:xs='http://www.w3.org/2001/XMLSchema'" .
554
            "                version='3.0'>" .
555
            "                <xsl:template match='*'>" .
556
            "                   <xsl:param name='a' as='xs:double'/>" .
557
            "                   <xsl:param name='b' as='xs:float'/>" .
558
            "                   <xsl:sequence select='name(.), \$a + \$b'/>" .
559
            "                </xsl:template>" .
560
            "                </xsl:stylesheet>";
561

    
562
        $transformer->compileFromString($source);
563
        $transformer->setProperty("!omit-xml-declaration", "yes");
564
        $transformer->setInitialTemplateParameters(array("a" => self::$saxonProc->createAtomicValue(12), "b" => self::$saxonProc->createAtomicValue(5)));
565
        $in = self::$saxonProc->parseXmlFromString("<e/>");
566
        $transformer->setInitialMatchSelection($in);
567
        $sw = $transformer->applyTemplatesReturningString();
568

    
569
        $this->assertEquals("e 17", $sw);
570
    }
571

    
572

    
573
    public function testApplyTemplatesToXdm()
574
    {
575
        $source = "<?xml version='1.0'?>" .
576
            "                <xsl:stylesheet xmlns:xsl='http://www.w3.org/1999/XSL/Transform'" .
577
            "                xmlns:xs='http://www.w3.org/2001/XMLSchema'" .
578
            "                version='3.0'>" .
579
            "                <xsl:template match='*'>" .
580
            "                   <xsl:param name='a' as='xs:double'/>" .
581
            "                   <xsl:param name='b' as='xs:float'/>" .
582
            "                   <xsl:sequence select='., \$a + \$b'/>" .
583
            "                </xsl:template>" .
584
            "                </xsl:stylesheet>";
585
        $transformer = self::$saxonProc->newXslt30Processor();
586
        $transformer->compileFromString($source);
587
        $transformer->setProperty("!omit-xml-declaration", "yes");
588
        $transformer->setInitialTemplateParameters(array("a" => self::$saxonProc->createAtomicValue(12), "b" => self::$saxonProc->createAtomicValue(5)));
589
        $transformer->setResultAsRawValue(True);
590
        $input = self::$saxonProc->parseXmlFromString("<e/>");
591
        $transformer->setInitialMatchSelection($input);
592
        $result = $transformer->applyTemplatesReturningValue();
593
        $this->assertNotNull($result);
594
        $this->assertEquals(2, $result->size());
595
        $first = $result->itemAt(0);
596
        $this->assertTrue($first->isNode());
597
        $this->assertEquals("e", $first->getNodeValue()->getNodeName());
598
        $second = $result->itemAt(1);
599
        $this->assertTrue($second->isAtomic());
600
        $this->assertEquals(17e0, $second->getAtomicValue()->getDoubleValue());
601

    
602
    }
603

    
604

    
605
    public function testResultDocument(): void
606
    {
607
        // bug 2771
608
        $xsl = "<xsl:stylesheet version=\"3.0\" \n" .
609
            "  xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\">\n" .
610
            "\n" .
611
            "  <xsl:template match='a'>\n" .
612
            "    <c>d</c>\n" .
613
            "  </xsl:template>\n" .
614
            "\n" .
615
            "  <xsl:template match='whatever'>\n" .
616
            "    <xsl:result-document href='out.xml'>\n" .
617
            "      <e>f</e>\n" .
618
            "    </xsl:result-document>\n" .
619
            "  </xsl:template>\n" .
620
            "\n" .
621
            "</xsl:stylesheet>";
622
        $transformer = self::$saxonProc->newXslt30Processor();
623
        $transformer->compileFromString($xsl);
624
        $input = self::$saxonProc->parseXmlFromString("<a>b</a>");
625
        $transformer->setInitialMatchSelection($input);
626
        $xdmValue = $transformer->applyTemplatesReturningValue();
627

    
628
        $this->assertEquals(1, $xdmValue->size());
629

    
630
    }
631

    
632

    
633
    public function testApplyTemplatesToFile()
634
    {
635

    
636
        $xsl = "<xsl:stylesheet version=\"3.0\" \n" .
637
            "  xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\">\n" .
638
            "\n" .
639
            "  <xsl:template match='a'>\n" .
640
            "    <c>d</c>\n" .
641
            "  </xsl:template>\n" .
642
            "</xsl:stylesheet>";
643
        $transformer = self::$saxonProc->newXslt30Processor();
644
        $transformer->compileFromString($xsl);
645
        $input = self::$saxonProc->parseXmlFromString("<a>b</a>");
646
        $transformer->setOutputFile("output123.xml");
647
        $transformer->setInitialMatchSelection($input);
648
        $transformer->applyTemplatesReturningFile("output123.xml");
649
        $this->assertTrue(file_exists("output123.xml"));
650

    
651
        if (file_exists("output123.xml")) {
652
            unlink("output123.xml");
653
        }
654
    }
655

    
656

    
657
    public function testCallTemplateWithResultValidation(): void
658
    {
659
        $saxonProc2 = new Saxon\SaxonProcessor(True);
660
        $saxonProc2->setcwd("/home/ond1/work/svn/latest9.9-saxonc/samples/php/");
661
        $transformer = $saxonProc2->newXslt30Processor();
662

    
663
        $source = "<?xml version='1.0'?>" .
664
            "                <xsl:stylesheet xmlns:xsl='http://www.w3.org/1999/XSL/Transform'" .
665
            "                xmlns:xs='http://www.w3.org/2001/XMLSchema'" .
666
            "                version='3.0' exclude-result-prefixes='#all'>" .
667
            "                <xsl:import-schema><xs:schema><xs:element name='x' type='xs:int'/></xs:schema></xsl:import-schema>" .
668
            "                <xsl:template name='main'>" .
669
            "                   <xsl:result-document validation='strict'>" .
670
            "                     <x>3</x>" .
671
            "                   </xsl:result-document>" .
672
            "                </xsl:template>" .
673
            "                </xsl:stylesheet>";
674

    
675
        $transformer->compileFromString($source);
676
        $transformer->setProperty("!omit-xml-declaration", "yes");
677
        $sw = $transformer->callTemplateReturningString("main");
678
        $this->assertNotNull($sw);
679
        $this->assertEquals($sw, "<x>3</x>");
680

    
681
    }
682

    
683
    /*    public function testCallTemplateWithResultValidationFailure(): void {
684
            try {
685
                Xslt30Processor transformer = new Xslt30Processor(true);
686

687
                String source = "<?xml version='1.0'?>" +
688
                        "                <xsl:stylesheet xmlns:xsl='http://www.w3.org/1999/XSL/Transform'" +
689
                        "                xmlns:xs='http://www.w3.org/2001/XMLSchema'" +
690
                        "                version='3.0' expand-text='yes' exclude-result-prefixes='#all'>" +
691
                        "                <xsl:import-schema><xs:schema><xs:element name='x' type='xs:int'/></xs:schema></xsl:import-schema>" +
692
                        "                <xsl:param name='p'>zzz</xsl:param>" +
693
                        "                <xsl:template name='main'>" +
694
                        "                   <xsl:result-document validation='strict'>" +
695
                        "                     <x>{$p}</x>" +
696
                        "                   </xsl:result-document>" +
697
                        "                </xsl:template>" +
698
                        "                </xsl:stylesheet>";
699

700
                transformer.compileFromString(null, source, null, null);
701

702
                String[] params = new String[]{"!omit-xml-declaration"};
703
                Object[] pvalues = new Object[]{"yes"};
704
                String sw = transformer.callTemplateReturningString(null, null, "main", params, pvalues);
705
                fail("unexpected success");
706

707
            } catch (SaxonApiException e) {
708
                System.err.println("Failed as expected: " + e.getMessage());
709
            }
710
        }
711

712
    */
713

    
714
    public function testCallTemplateNoParamsRaw(): void
715
    {
716
        $transformer = self::$saxonProc->newXslt30Processor();
717
        $transformer->compileFromString("<xsl:stylesheet version='2.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>" .
718
            "<xsl:template name='xsl:initial-template'><xsl:sequence select='42'/></xsl:template>" .
719
            "</xsl:stylesheet>");
720

    
721
        $transformer->setResultAsRawValue(True);
722
        $result = $transformer->callTemplateReturningValue();
723
        $this->assertNotNull($result);
724
        $this->assertNotNull($result->getHead());
725
        $this->assertTrue($result->getHead()->isAtomic());
726
        $this->assertEquals($result->getHead()->getAtomicValue()->getLongValue(), 42);
727

    
728

    
729
    }
730

    
731

    
732
    public function testCallNamedTemplateWithParamsRaw()
733
    {
734
        $transformer = self::$saxonProc->newXslt30Processor();
735

    
736
        $source = "<?xml version='1.0'?>" .
737
            "                <xsl:stylesheet xmlns:xsl='http://www.w3.org/1999/XSL/Transform'" .
738
            "                xmlns:xs='http://www.w3.org/2001/XMLSchema'" .
739
            "                version='3.0'>" .
740
            "                <xsl:template name='t'>" .
741
            "                   <xsl:param name='a' as='xs:double'/>" .
742
            "                   <xsl:param name='b' as='xs:float'/>" .
743
            "                   <xsl:sequence select='\$a+1, \$b+1'/>" .
744
            "                </xsl:template>" .
745
            "                </xsl:stylesheet>";
746

    
747
        $transformer->compileFromString($source);
748
        $transformer->setResultAsRawValue(True);
749
        $transformer->setInitialTemplateParameters(array("a" => self::$saxonProc->createAtomicValue(12), "b" => self::$saxonProc->createAtomicValue(5)));
750
        $val = $transformer->callTemplateReturningValue("t");
751
        $this->assertNotNull($val);
752
        $this->assertEquals(2, $val->size());
753
        $this->assertTrue($val->itemAt(0)->isAtomic());
754
        $this->assertEquals(13, $val->itemAt(0)->getAtomicValue()->getLongValue());
755
        $this->assertTrue($val->itemAt(0)->isAtomic());
756
        $this->assertEquals(6, $val->itemAt(1)->getAtomicValue()->getLongValue());
757

    
758
    }
759

    
760
    /*
761
        public function testCatalog(){
762

763
            $transformer = self::$saxonProc->newXslt30Processor();
764
            Processor proc = transformer.getProcessor();
765

766

767
            try {
768
                XmlCatalogResolver.setCatalog(CWD_DIR+"../../catalog-test/catalog.xml", proc.getUnderlyingConfiguration(), true);
769

770
                transformer.applyTemplatesReturningValue(CWD_DIR+"../../catalog-test/", "example.xml","test1.xsl",null, null);
771
            } catch (XPathException e) {
772
                e.printStackTrace();
773
                fail();
774
            } catch (SaxonApiException e) {
775
                e.printStackTrace();
776
                fail();
777
            }
778

779
        }
780

781
    */
782

    
783
    public function testApplyTemplatesRaw()
784
    {
785
        $transformer = self::$saxonProc->newXslt30Processor();
786

    
787
        $source = "<?xml version='1.0'?>" .
788
            "                <xsl:stylesheet xmlns:xsl='http://www.w3.org/1999/XSL/Transform'" .
789
            "                xmlns:xs='http://www.w3.org/2001/XMLSchema'" .
790
            "                version='3.0'>" .
791
            "                <xsl:template match='*'>" .
792
            "                   <xsl:param name='a' as='xs:double'/>" .
793
            "                   <xsl:param name='b' as='xs:float'/>" .
794
            "                   <xsl:sequence select='., \$a + \$b'/>" .
795
            "                </xsl:template>" .
796
            "                </xsl:stylesheet>";
797

    
798
        $transformer->compileFromString($source);
799
        $node = self::$saxonProc->parseXmlFromString("<e/>");
800

    
801
        $transformer->setResultAsRawValue(True);
802
        $transformer->setInitialTemplateParameters(array("a" => self::$saxonProc->createAtomicValue(12), "b" => self::$saxonProc->createAtomicValue(5)));
803
        $transformer->setInitialMatchSelection($node);
804
        $result = $transformer->applyTemplatesReturningValue();
805

    
806

    
807
        $this->assertEquals(2, $result->size());
808
        $first = $result->itemAt(0);
809
        $this->assertNotNull($first);
810
        $this->assertTrue($first->isNode());
811
        $this->assertEquals($first->getNodeValue()->getNodeName(), "e");
812
        $second = $result->itemAt(1);
813
        $this->assertNotNull($second);
814
        $this->assertTrue($second->isAtomic());
815
        $this->assertEquals($second->getAtomicValue()->getDoubleValue(), "17e0");
816

    
817
    }
818

    
819

    
820
    public function testApplyTemplatesToSerializer()
821
    {
822
        $transformer = self::$saxonProc->newXslt30Processor();
823

    
824
        $source = "<?xml version='1.0'?>" .
825
            "                <xsl:stylesheet xmlns:xsl='http://www.w3.org/1999/XSL/Transform'" .
826
            "                xmlns:xs='http://www.w3.org/2001/XMLSchema'" .
827
            "                version='3.0'>" .
828
            "                <xsl:output method='text' item-separator='~~'/>" .
829
            "                <xsl:template match='.'>" .
830
            "                   <xsl:param name='a' as='xs:double'/>" .
831
            "                   <xsl:param name='b' as='xs:float'/>" .
832
            "                   <xsl:sequence select='., \$a + \$b'/>" .
833
            "                </xsl:template>" .
834
            "                </xsl:stylesheet>";
835

    
836
        $transformer->compileFromString($source);
837
        $transformer->setProperty("!omit-xml-declaration", "yes");
838
        $transformer->setResultAsRawValue(True);
839
        $transformer->setInitialTemplateParameters(array("a" => self::$saxonProc->createAtomicValue(12), "b" => self::$saxonProc->createAtomicValue(5)));
840

    
841
        $transformer->setInitialMatchSelection(self::$saxonProc->createAtomicValue(16));
842
        $sw = $transformer->applyTemplatesReturningString();
843

    
844
        $this->assertEquals("16~~17", $sw);
845

    
846
    }
847

    
848

    
849
    /*  public function testItemSeparatorToSerializer(): void {
850
          try {
851

852
              String sr =
853
                      "<xsl:stylesheet version='2.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>" +
854
                              "  <xsl:template name='go'>"
855
                              + "<xsl:comment>start</xsl:comment><a><b/><c/></a><xsl:comment>end</xsl:comment>"
856
                              + "</xsl:template>" +
857
                              "</xsl:stylesheet>";
858
              $transformer = self::$saxonProc->newXslt30Processor();
859
              transformer.compileFromString(null, sr, null, null);
860
              String[] params = new String[]{"!method", "!indent", "!item-separator"};
861
              Object[] pvalues = new Object[]{"xml", "no", "+++"};
862
              String sw = transformer.callTemplateReturningString(null, null, "go", params, pvalues);
863
              System.err.println(sw);
864
              assertTrue(sw.contains("<!--start-->+++"));
865
              assertTrue(sw.contains("+++<!--end-->"));
866
              assertTrue(sw.contains("<a><b/><c/></a>"));
867
          } catch (Exception e) {
868
              e.printStackTrace();
869
              fail(e.getMessage());
870
          }
871

872
      }
873

874
      public function testSequenceResult() throws SaxonApiException {
875
          try {
876

877
              String source = "<?xml version='1.0'?>" +
878
                      "                <xsl:stylesheet xmlns:xsl='http://www.w3.org/1999/XSL/Transform'" +
879
                      "                xmlns:xs='http://www.w3.org/2001/XMLSchema'" +
880
                      "                version='3.0'>" +
881
                      "                <xsl:template name='xsl:initial-template' as='xs:integer+'>" +
882
                      "                     <xsl:sequence select='(1 to 5)' />        " +
883
                      "                </xsl:template>" +
884
                      "                </xsl:stylesheet>";
885

886
              $transformer = self::$saxonProc->newXslt30Processor();
887
              transformer.compileFromString(null, source, null, null);
888
              String[] params = new String[]{"outvalue"};
889
              Object[] pvalues = new Object[]{true};
890
              XdmValue res = transformer.callTemplateReturningValue(null, null, null, params, pvalues);
891
              int count = res.size();
892
              XdmAtomicValue value = (XdmAtomicValue) res.itemAt(0);
893
          } catch (SaxonApiException e) {
894
              e.printStackTrace();
895
              fail();
896
          }
897
      }
898

899
  */
900

    
901

    
902
}
(16-16/17)