Project

Profile

Help

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

he / latest9.8 / hec / samples / php / xsltExamples.php @ e54a0d4a

1
<!DOCTYPE html SYSTEM "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
2
<html xmlns="http://www.w3.org/1999/xhtml">
3
    <head>
4
        <title>Saxon/C API design use cases</title>
5
    </head>
6
    <body>
7
        <?php 
8

    
9
 function userFunction1()
10
            {
11
                
12
                echo("userspace function called with no paramXXX\n");
13

    
14
}
15
        
16
         // define a user space function
17
            function userFunction($param , $param2)
18
            {
19
                
20
                echo("userspace function called with two paramXXX\n");
21
         if(is_numeric($param2)){
22
echo("userspace function called cp1\n");
23
                $param32 = $param32*2;
24
                echo("PAram3 = ".$param2);
25
        } 
26
        
27
        if(is_a($param2, "Saxon\\XdmNode")) {
28
echo("userspace function called cp2\n");
29
                /*$proc = new Saxon\SaxonProcessor(true);
30
                $xpath = $proc->newXPathProcessor();
31
                $xpath->setContextItem($param3);
32
                $value = $xpath->evaluate("(//*)");*/
33
                $value = $param2->getStringValue();
34
                if($value != null) {
35
                echo("XdmNode value= ".$value);
36
                return $param2;
37
                } else {
38
                        echo("XdmNode value is null!!!");
39
                }
40
        }
41
        echo("userspace function called cp3\n");
42
           $resulti = "Result of userFunction+".$param;
43
           
44
           return $resulti;
45
            }
46

    
47
        function  userFunctionExample($saxon, $proc,$xmlfile, $xslFile){
48
                echo '<b>userFunctionExample:</b><br/>';
49
                global $resultg;
50

    
51
                $proc->setProperty("extc","/home/ond1/work/svn/latest9.8-saxonc/hec/Saxon.C.API/php7-src/PHP-Build/lib/php/extensions/debug-non-zts-20160303/saxon" ); //"/home/ond1/work/new-svn/latest9.8-hec/hec/samples/cppTests/cppExtensionFunction"
52
                $saxon->registerPHPFunction("/home/ond1/work/svn/latest9.8-saxonc/hec/Saxon.C.API/php7-src/PHP-Build/lib/php/extensions/debug-non-zts-20160303/saxon");
53
                 $proc->setSourceFromFile($xmlfile);
54
                $proc->compileFromFile($xslFile);
55
                
56
                        
57
                $result = $proc->transformToString();               
58
                if($result != null) {                               
59
                echo 'Output=======:'.$result;
60
                } else {
61
                        echo "Result is null";
62
                        if($proc->exceptionOccurred()){
63
                                echo "Exception occurred";
64
                        }
65
                        $errCount = $proc->getExceptionCount(); 
66
                        for($i =0;$i<$errCount;$i++) {
67
                                echo 'Error Message='.$proc->getErrorMessage($i);
68
                        }
69
                }
70
                
71
                $proc->clearParameters();
72
                $proc->clearProperties();
73

    
74
        }
75
            
76
            /* simple example to show transforming to string */
77
            function exampleSimple1($proc, $xmlfile, $xslFile){
78
                echo '<b>exampleSimple1:</b><br/>';
79
                $proc->setSourceFromFile($xmlfile);
80
                $proc->compileFromFile($xslFile);
81
                        
82
                $result = $proc->transformToString();               
83
                if($result != null) {               
84
                                
85
                echo 'Output:'.$result;
86
                } else {
87
                        echo "Result is null";
88
                }
89
                $proc->clearParameters();
90
                $proc->clearProperties();            
91
            }
92
            
93
            /* simple example to show transforming to file */
94
            function exampleSimple2($proc, $xmlFile, $xslFile){
95
                echo '<b>exampleSimple2:</b><br/>';
96
                $proc->setSourceFromFile($xmlFile);
97
                $proc->compileFromFile($xslFile);
98
                $filename = "/home/ond1/temp/output1.xml";
99
                $proc->setOutputFile($filename);
100
                $proc->transformToFile();
101
                                
102
                if (file_exists($filename)) {
103
                    echo "The file $filename exists";
104
                    unlink($filename);
105
                } else {
106
                    echo "The file $filename does not exist";
107
                }
108
                       $proc->clearParameters();
109
                $proc->clearProperties();
110
            }
111
            /* simple example to show importing a document as string and stylesheet as a string */
112
            function exampleSimple3($saxonProc, $proc){
113
                echo '<b>exampleSimple3:</b><br/>';
114
                $proc->clearParameters();
115
                $proc->clearProperties();
116
                $xdmNode = $saxonProc->parseXmlFromString("<doc><b>text value of out</b></doc>");
117
                if($xdmNode == null) {
118
                        echo 'xdmNode is null';
119
                        return;        
120
                }            
121
                $proc->setSourceFromXdmValue($xdmNode);
122
                $proc->compileFromString("<xsl:stylesheet xmlns:xsl='http://www.w3.org/1999/XSL/Transform' version='2.0'>
123
                                                    <xsl:template match='/'>
124
                                                        <xsl:copy-of select='.'/>
125
                                                    </xsl:template>
126
                                            </xsl:stylesheet>");
127

    
128
                $result = $proc->transformToString();
129
                echo '<b>exampleSimple3</b>:<br/>';
130
                if($result != null) {                             
131
                echo 'Output:'.$result;
132
                } else {
133
                        echo "Result is null";
134
                }       
135
                         
136
                $proc->clearParameters();
137
                $proc->clearProperties(); 
138
                 unset($xdmNode); 
139
            }
140

    
141
                function exampleLoopVar($saxonProc, $proc, $xml, $xslt){
142
$params = array(
143
  "testparam1" => "testvalue1",
144
  "testparam2" => "testvalue2",
145
  "testparam3" => "testvalue3",
146
);
147
echo '<b>exampleLoopVar:</b><br/>';
148
$proc->setSourceFromFile($xml);
149
$proc->compileFromFile($xslt);
150

    
151
foreach($params as $name => $value) {
152
echo "loop itr";
153
  $xdmValue = $saxonProc->createAtomicValue(strval($value));
154
if($xdmValue !=null) {
155
    $proc->setParameter($name, $xdmValue);
156
  } else {
157
    echo "Xdmvalue is null";
158
  }
159
        //unset($xdmValue);
160
}
161
echo "Before transformToString";
162
$result = $proc->transformToString();
163
if($result != null) {
164
  echo 'Output:'.$result;
165
} else {
166
  echo "Result is null";
167
}
168

    
169
$proc->clearParameters();
170
$proc->clearProperties();
171
                }
172

    
173

    
174
           
175
           
176
            function exampleParam($saxonProc, $proc, $xmlFile, $xslFile){
177
                echo "\n",'<b>ExampleParamXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXxYYYYY:</b><br/>';
178

    
179
                $proc->setSourceFromFile($xmlFile);
180
                $proc->compileFromFile($xslFile);
181
                $xdmvalue = $saxonProc->createAtomicValue(strval("Hello to you"));
182
                if($xdmvalue !=null){
183
                        echo "Name of Class " , get_class($xdmvalue) , "\n";                         
184
                        $str = $xdmvalue->getStringValue();
185
                        if($str!=null) {
186
                                echo "XdmValue:".$str;
187
                        }
188
                        $proc->setParameter('a-param', $xdmvalue);
189
                } else {
190
                        echo "Xdmvalue is null";
191
                }
192
                $result = $proc->transformToString();
193
                if($result != null) {                
194
                        echo 'Output:'.$result."<br/>";
195
                } else {
196
                        echo "Result is NULL<br/>";
197
                }
198
               
199
                $proc->clearParameters();                
200
                //unset($result);
201
                echo 'again with a no parameter value<br/>';
202
                $proc->setProperty('!indent', 'yes'); 
203
                $result = $proc->transformToString();
204
               
205
                $proc->clearProperties();
206
                echo $result;
207
                echo '<br/>';
208
              //  unset($result);
209
                echo 'again with no parameter and no properties value set. This should fail as no contextItem set<br/>';
210
                $xdmvalue = $saxonProc->createAtomicValue(strval("goodbye to you"));
211
                $proc->setParameter('a-param', $xdmvalue);
212
                
213
                $result = $proc->transformToString();   
214
                if($result != null) {             
215
                        echo 'Output ='.$result;
216
                } else {
217
                        echo 'Error in result';
218
                        $errCount = $proc->getExceptionCount(); 
219
                        for($i =0;$i<$errCount;$i++) {
220
                                echo 'Error Message='.$proc->getErrorMessage($i);
221
                        }
222
                }
223
                $proc->clearParameters();
224
                $proc->clearProperties(); 
225
                        
226
            }
227

    
228

    
229
            function exampleXMLFilterChain($proc, $xmlFile, $xsl1File, $xsl2File, $xsl3File){
230
                echo '<b>XML Filter Chain using setSource</b><br/>';                
231
                $proc->setSourceFromFile($xmlFile);
232
 echo '<b>XML Filter Chain using setSource cp0</b><br/>'; 
233
                $proc->compileFromFile($xsl1File);
234
 echo '<b>XML Filter Chain using setSource cp1</b><br/>'; 
235
                $xdmValue1 = $proc->transformToValue();
236
                echo '<b>XML Filter Chain using setSource cp2</b><br/>'; 
237
                $proc->compileFromFile($xsl2File);
238

    
239
                $proc->setSourceFromXdmValue($xdmValue1);
240

    
241
                unset($xdmValue1);
242
                $xdmValue1 = $proc->transformToValue();
243
 
244
                
245
                $proc->compileFromFile($xsl3File);                
246
                $proc->setSourceFromXdmValue($xdmValue1);
247
                $result = $proc->transformToString();
248
                if($result != null) {
249
                        echo 'Output:'.$result;        
250
                } else {
251
                        echo 'Result is null';
252
                                    $errCount = $proc->getExceptionCount();
253
                                    if($errCount > 0 ){ 
254
                                        for($i = 0; $i < $errCount; $i++) {
255
                                               $errCode = $proc->getErrorCode(intval($i));
256
                                               $errMessage = $proc->getErrorMessage(intval($i));
257
                                               echo 'Expected error: Code='.$errCode.' Message='.$errMessage;
258
                                           }
259
                                                $proc->exceptionClear();        
260
                                        }
261
                }                      
262
                $proc->clearParameters();
263
                $proc->clearProperties();
264
            }
265
            
266
            function exampleXMLFilterChain2($saxonProc, $proc, $xmlFile, $xsl1File, $xsl2File, $xsl3File){
267
                echo '<b>XML Filter Chain using Parameters1</b><br/>';                
268
                $xdmNode = $saxonProc->parseXmlFromFile($xmlFile);
269
 echo '<b>exampleXMLFilterChain2: XML Filter Chain using Parameters1 cp0</b><br/>'; 
270
                if($xdmNode == NULL) {
271
                        echo 'source node is NULLXXXXXXX';
272
                }
273
                $proc->setParameter('node', $xdmNode);
274
 echo '<b>XML Filter Chain using using Parameters1 cp1</b><br/>';
275
                $proc->compileFromFile($xsl1File);
276
  echo '<b>XML Filter Chain using using Parameters1 cp2</b><br/>';
277
                $xdmValue1 = $proc->transformToValue();
278
                if($xdmValue1== null) { 
279
                        echo '<b>XML Filter Chain using using Parameters1 cp3-0 nullYYYYYYY</b><br/>';
280
                }
281
 echo '<b>XML Filter Chain using using Parameters1 cp3</b><br/>';
282
                 $errCount = $proc->getExceptionCount();
283
                if($errCount > 0 ){ 
284
                        for($i = 0; $i < $errCount; $i++) {
285
                               $errCode = $proc->getErrorCode(intval($i));
286
                               $errMessage = $proc->getErrorMessage(intval($i));
287
                               echo 'Expected error: Code='.$errCode.' Message='.$errMessage;
288
                                           }
289
                        $proc->exceptionClear();        
290
                }
291
                $proc->clearParameters();
292
                echo '<b>XML Filter Chain using using Parameters1 cp4</b><br/>';
293
                $proc->compileFromFile($xsl2File);
294
 echo '<b>XML Filter Chain using using Parameters1 cp5</b><br/>';
295
                $proc->setSourceFromXdmValue($xdmValue1);
296
 echo '<b>XML Filter Chain using using Parameters1 cp6</b><br/>';
297
                $xdmValue1 = $proc->transformToValue();
298
                $errCount = $proc->getExceptionCount();
299
                if($errCount > 0 ){ 
300
                        for($i = 0; $i < $errCount; $i++) {
301
                               $errCode = $proc->getErrorCode(intval($i));
302
                               $errMessage = $proc->getErrorMessage(intval($i));
303
                               echo 'Expected error: Code='.$errCode.' Message='.$errMessage;
304
                                           }
305
                        $proc->exceptionClear();        
306
                }
307
                $proc->clearParameters();
308
                
309
                $proc->compileFromFile($xsl3File);                
310
                $proc->setParameter('node', $xdmValue1);
311
                $result = $proc->transformToString();
312
                if($result != null) {
313
                        echo 'Output:'.$result;        
314
                } else {
315
                        echo 'Result is null';
316
                                    $errCount = $proc->getExceptionCount();
317
                                    if($errCount > 0 ){ 
318
                                        for($i = 0; $i < $errCount; $i++) {
319
                                               $errCode = $proc->getErrorCode(intval($i));
320
                                               $errMessage = $proc->getErrorMessage(intval($i));
321
                                               echo 'Expected error: Code='.$errCode.' Message='.$errMessage;
322
                                           }
323
                                                $proc->exceptionClear();        
324
                                        }
325
                }        
326
                $proc->clearParameters();
327
                $proc->clearProperties();
328
            }            
329

    
330
            /* simple example to detect and handle errors from a transformation */
331
            function exampleError1($proc, $xmlFile, $xslFile){
332
                echo '<br/><b>exampleError1:</b><br/>';
333
                $proc->setSourceFromFile($xmlFile);
334
                $proc->compileFromFile($xslFile);
335
                $result = $proc->transformToString();
336
                if($result == NULL) {
337

    
338
                    $errCount = $proc->getExceptionCount();
339
                        echo 'Error found. count='.$errCount." ";
340
                                    if($errCount > 0 ){ 
341
                                        for($i = 0; $i < $errCount; $i++) {
342
                                               $errCode = $proc->getErrorCode(intval($i));
343
                                               $errMessage = $proc->getErrorMessage(intval($i));
344
                                               echo 'Expected error: Code='.$errCode.' Message='.$errMessage;
345
                                           }
346
                                                $proc->exceptionClear();        
347
                                        } else {
348
                                                echo '<b>Error not reported correctly</b>';
349
                                        }
350
                
351
                
352
                }                
353
                echo $result;
354
                    $proc->clearParameters();
355
                $proc->clearProperties();
356
            
357
            }   
358

    
359

    
360
            /* simple example to test transforming without an stylesheet */
361
            function exampleError2($proc, $xmlFile, $xslFile){
362
                echo '<b>exampleError2:</b><br/>';
363
                $proc->setSourceFromFile($xmlFile);
364
                $proc->compileFromFile($xslFile);
365
                
366
                $result = $proc->transformToString();
367
                
368
                if($result == NULL) {
369
                    $errCount = $proc->getExceptionCount();
370
                                    if($errCount > 0 ){ 
371
                                        for($i = 0; $i < $errCount; $i++) {
372
                                               $errCode = $proc->getErrorCode(intval($i));
373
                                               $errMessage = $proc->getErrorMessage(intval($i));
374
                                               echo 'Expected error: Code='.$errCode.' Message='.$errMessage;
375
                                           }
376
                                                $proc->exceptionClear();        
377
                                        }
378
                
379
                
380
                }                
381
                echo $result;            
382
                   $proc->clearParameters();
383
                $proc->clearProperties();
384
            
385
            }   
386
            
387
            
388
            $foo_xml = "xml/foo.xml";
389
            $foo_xsl = "xsl/foo.xsl";
390
            $baz_xml = "xml/baz.xml";
391
            $baz_xsl = "xsl/baz.xsl";
392
            $foo2_xsl = "xsl/foo2.xsl";
393
            $foo3_xsl = "xsl/foo3.xsl";
394
            $err_xsl = "xsl/err.xsl";            
395
            $err1_xsl = "xsl/err1.xsl";
396
            $text_xsl = "xsl/text.xsl";
397
            $cities_xml = "xml/cities.xml";
398
            $embedded_xml = "xml/embedded.xml";
399
            $multidoc_xsl = "xsl/multidoc.xsl";
400
            $identity_xsl = "xsl/identity.xsl"; 
401
            
402
            $saxonProc = new Saxon\SaxonProcessor(true);
403
                    
404
            $version = $saxonProc->version();
405
            echo 'Saxon Processor version: '.$version;
406
            echo '<br/>';   
407
            $proc = $saxonProc->newXsltProcessor();   
408
            echo '<br/>';  
409
           exampleSimple1($proc, $foo_xml, $foo_xsl);
410
            echo '<br/>';
411
             exampleSimple2($proc, "xml/foo.xml", $foo_xsl);
412
            echo '<br/>';            
413
            exampleSimple3($saxonProc, $proc);
414
            echo '<br/>';
415
            exampleLoopVar($saxonProc, $proc, $foo_xml, $foo_xsl);
416
            exampleParam($saxonProc, $proc, $foo_xml, $foo_xsl);
417
            exampleError1($proc, $foo_xml, $err_xsl);
418
            echo '<br/>'; 
419
            exampleError2($proc, $foo_xml, $err1_xsl);
420
            echo '<br/>';
421
            exampleXMLFilterChain($proc, $foo_xml, $foo_xsl, $foo2_xsl, $foo3_xsl);
422
            echo '<br/>';                    
423
           exampleXMLFilterChain2($saxonProc, $proc, $foo_xml, $foo_xsl, $foo2_xsl, $foo3_xsl);     
424
            echo '<br/>';  
425
           // userFunctionExample($saxonProc, $proc,  $foo_xml, 'xsl/testExFunc.xsl'); //corruption in memory
426
            echo '<br/>';
427
            unset($proc);
428
            unset($saxonproc);
429
        
430
        
431
        ?>
432
    </body>
433
</html>
(14-14/14)