Project

Profile

Help

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

he / latest9.6 / hec / samples / php / xsltExamples.php @ 2a9719f6

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
            /* simple example to show transforming to string */
10
            function exampleSimple1($proc, $xmlfile, $xslFile){
11
                $proc->setSourceFromFile($xmlfile);
12
                $proc->compileFromFile($xslFile);
13
                        
14
                $result = $proc->transformToString();               
15
                if($result != null) {               
16
                echo '<b>exampleSimple1:</b><br/>';                
17
                echo 'Output:'.$result;
18
                } else {
19
                        echo "Result is null";
20
                }
21
                $proc->clearParameters();
22
                $proc->clearProperties();            
23
            }
24
            
25
            /* simple example to show transforming to file */
26
            function exampleSimple2($proc, $xmlFile, $xslFile){
27
                echo '<b>exampleSimple2:</b><br/>';
28
                $proc->setSourceFromFile($xmlFile);
29
                $proc->compileFromFile($xslFile);
30
                $filename = "/home/ond1/temp/output1.xml";
31
                $proc->setOutputFile($filename);
32
                $proc->transformToFile();
33
                                
34
                if (file_exists($filename)) {
35
                    echo "The file $filename exists";
36
                    unlink($filename);
37
                } else {
38
                    echo "The file $filename does not exist";
39
                }
40
                       $proc->clearParameters();
41
                $proc->clearProperties();
42
            }
43
            /* simple example to show importing a document as string and stylesheet as a string */
44
            function exampleSimple3($saxonProc, $proc){
45
                echo '<b>exampleSimple3:</b><br/>';
46
                $proc->clearParameters();
47
                $proc->clearProperties();
48
                $xdmNode = $saxonProc->parseXmlFromString("<doc><b>text value of out</b></doc>");
49
                if($xdmNode == null) {
50
                        echo 'xdmNode is null';
51
                        return;        
52
                }            
53
                $proc->setSourceFromXdmValue($xdmNode);
54
                $proc->compileFromString("<xsl:stylesheet xmlns:xsl='http://www.w3.org/1999/XSL/Transform' version='2.0'>
55
                                                    <xsl:template match='/'>
56
                                                        <xsl:copy-of select='.'/>
57
                                                    </xsl:template>
58
                                            </xsl:stylesheet>");
59

    
60
                $result = $proc->transformToString();
61
                echo '<b>exampleSimple3</b>:<br/>';
62
                if($result != null) {                             
63
                echo 'Output:'.$result;
64
                } else {
65
                        echo "Result is null";
66
                }       
67
                         
68
                $proc->clearParameters();
69
                $proc->clearProperties(); 
70
                 unset($xdmNode); 
71
            }
72

    
73
                function exampleLoopVar($saxonProc, $proc, $xml, $xslt){
74
$params = array(
75
  "testparam1" => "testvalue1",
76
  "testparam2" => "testvalue2",
77
  "testparam3" => "testvalue3",
78
);
79
echo '<b>exampleLoopVar:</b><br/>';
80

    
81
$proc->setSourceFromFile($xml);
82
$proc->compileFromFile($xslt);
83
$values = array();
84
foreach($params as $name => $value) {
85
  $xdmValue = $saxonProc->createAtomicValue(strval($value));
86
if($xdmValue !=null) {
87
   // echo "Name of Class " , get_class($xdmValue) , "\n";
88
    $proc->setParameter($name, $xdmValue);
89
  } else {
90
    echo "Xdmvalue is null";
91
  }
92
        //unset($xdmValue);
93
}
94
echo "Before transformToString";
95
$result = $proc->transformToString();
96
if($result != null) {
97
  echo 'Output:'.$result;
98
} else {
99
  echo "Result is null";
100
}
101

    
102
$proc->clearParameters();
103
$proc->clearProperties();
104
                }
105

    
106

    
107
           
108
           
109
            function exampleParam($saxonProc, $proc, $xmlFile, $xslFile){
110
                echo "\n",'<b>ExampleParam:</b><br/>';
111

    
112
                $proc->setSourceFromFile($xmlFile);
113
                $proc->compileFromFile($xslFile);
114
                $xdmvalue = $saxonProc->createAtomicValue(strval("Hello to you"));
115
                if($xdmvalue !=null){
116
                        echo "Name of Class " , get_class($xdmvalue) , "\n";                         
117
                        $str = $xdmvalue->getStringValue();
118
                        if($str!=null) {
119
                                echo "XdmValue:".$str;
120
                        }
121
                        $proc->setParameter('a-param', $xdmvalue);
122
                } else {
123
                        echo "Xdmvalue is null";
124
                }
125
                $result = $proc->transformToString();
126
                if($result != null) {                
127
                        echo 'Output:'.$result."<br/>";
128
                } else {
129
                        echo "Result is NULL<br/>";
130
                }
131
               
132
                $proc->clearParameters();                
133
                //unset($result);
134
                echo 'again with a no parameter value<br/>';
135
                $proc->setProperty('!indent', 'yes'); //Serialization property indicated with a '!' symbol
136
                $result = $proc->transformToString();
137
               
138
                $proc->clearProperties();
139
                echo $result;
140
                echo '<br/>';
141
              //  unset($result);
142
                echo 'again with no parameter and no properties value set. This should fail as no contextItem set<br/>';
143
                $xdmvalue = $saxonProc->createAtomicValue(strval("goodbye to you"));
144
                $proc->setParameter('a-param', $xdmvalue);
145
                
146
                $result = $proc->transformToString();                
147
                echo $result;
148
                $proc->clearParameters();
149
                $proc->clearProperties(); 
150
                        
151
            }
152

    
153

    
154
            function exampleXMLFilterChain($proc, $xmlFile, $xsl1File, $xsl2File, $xsl3File){
155
                echo '<b>XML Filter Chain using setSource</b><br/>';                
156
                $proc->setSourceFromFile($xmlFile);
157
                $proc->compileFromFile($xsl1File);
158
                $xdmValue1 = $proc->transformToValue();
159
                
160
                $proc->compileFromFile($xsl2File);
161
                $proc->setSourceFromXdmValue($xdmValue1);
162
                unset($xdmValue1);
163
                $xdmValue1 = $proc->transformToValue();
164
                
165
                $proc->compileFromFile($xsl3File);                
166
                $proc->setSourceFromXdmValue($xdmValue1);
167
                $result = $proc->transformToString();
168
                if($result != null) {
169
                        echo 'Output:'.$result;        
170
                } else {
171
                        echo 'Result is null';
172
                                    $errCount = $proc->getExceptionCount();
173
                                    if($errCount > 0 ){ 
174
                                        for($i = 0; $i < $errCount; $i++) {
175
                                               $errCode = $proc->getErrorCode(intval($i));
176
                                               $errMessage = $proc->getErrorMessage(intval($i));
177
                                               echo 'Expected error: Code='.$errCode.' Message='.$errMessage;
178
                                           }
179
                                                $proc->exceptionClear();        
180
                                        }
181
                }                      
182
                $proc->clearParameters();
183
                $proc->clearProperties();
184
            }
185
            
186
            function exampleXMLFilterChain2($saxonProc, $proc, $xmlFile, $xsl1File, $xsl2File, $xsl3File){
187
                echo '<b>XML Filter Chain using Parameters</b><br/>';                
188
                $xdmNode = $saxonProc->parseXmlFromFile($xmlFile);
189
                if($xdmNode == NULL) {
190
                        echo 'source node is NULL';
191
                }
192
                $proc->setParameter('node', $xdmNode);
193
                $proc->compileFromFile($xsl1File);
194

    
195
                $xdmValue1 = $proc->transformToValue();
196
                 $errCount = $proc->getExceptionCount();
197
                if($errCount > 0 ){ 
198
                        for($i = 0; $i < $errCount; $i++) {
199
                               $errCode = $proc->getErrorCode(intval($i));
200
                               $errMessage = $proc->getErrorMessage(intval($i));
201
                               echo 'Expected error: Code='.$errCode.' Message='.$errMessage;
202
                                           }
203
                        $proc->exceptionClear();        
204
                }
205
                $proc->clearParameters();
206
               
207
                $proc->compileFromFile($xsl2File);
208
                $proc->setSourceFromXdmValue($xdmValue1);
209
                $xdmValue1 = $proc->transformToValue();
210
                $errCount = $proc->getExceptionCount();
211
                if($errCount > 0 ){ 
212
                        for($i = 0; $i < $errCount; $i++) {
213
                               $errCode = $proc->getErrorCode(intval($i));
214
                               $errMessage = $proc->getErrorMessage(intval($i));
215
                               echo 'Expected error: Code='.$errCode.' Message='.$errMessage;
216
                                           }
217
                        $proc->exceptionClear();        
218
                }
219
                $proc->clearParameters();
220
                
221
                $proc->compileFromFile($xsl3File);                
222
                $proc->setParameter('node', $xdmValue1);
223
                $result = $proc->transformToString();
224
                if($result != null) {
225
                        echo 'Output:'.$result;        
226
                } else {
227
                        echo 'Result is null';
228
                                    $errCount = $proc->getExceptionCount();
229
                                    if($errCount > 0 ){ 
230
                                        for($i = 0; $i < $errCount; $i++) {
231
                                               $errCode = $proc->getErrorCode(intval($i));
232
                                               $errMessage = $proc->getErrorMessage(intval($i));
233
                                               echo 'Expected error: Code='.$errCode.' Message='.$errMessage;
234
                                           }
235
                                                $proc->exceptionClear();        
236
                                        }
237
                }        
238
                $proc->clearParameters();
239
                $proc->clearProperties();
240
            }            
241

    
242
            /* simple example to detect and handle errors from a transformation */
243
            function exampleError1($proc, $xmlFile, $xslFile){
244
                echo '<br/><b>exampleError1:</b><br/>';
245
                $proc->setSourceFromFile($xmlFile);
246
                $proc->compileFromFile($xslFile);
247
                
248
                $result = $proc->transformToString();
249
                
250
                if($result == NULL) {
251
                    $errCount = $proc->getExceptionCount();
252
                                    if($errCount > 0 ){ 
253
                                        for($i = 0; $i < $errCount; $i++) {
254
                                               $errCode = $proc->getErrorCode(intval($i));
255
                                               $errMessage = $proc->getErrorMessage(intval($i));
256
                                               echo 'Expected error: Code='.$errCode.' Message='.$errMessage;
257
                                           }
258
                                                $proc->exceptionClear();        
259
                                        } else {
260
                                                echo '<b>Error not reported correctly</b>';
261
                                        }
262
                
263
                
264
                }                
265
                echo $result;
266
                    $proc->clearParameters();
267
                $proc->clearProperties();
268
            
269
            }   
270

    
271

    
272
            /* simple example to test transforming without an stylesheet */
273
            function exampleError2($proc, $xmlFile, $xslFile){
274
                echo '<b>exampleError2:</b><br/>';
275
                $proc->setSourceFromFile($xmlFile);
276
                $proc->compileFromFile($xslFile);
277
                
278
                $result = $proc->transformToString();
279
                
280
                if($result == NULL) {
281
                    $errCount = $proc->getExceptionCount();
282
                                    if($errCount > 0 ){ 
283
                                        for($i = 0; $i < $errCount; $i++) {
284
                                               $errCode = $proc->getErrorCode(intval($i));
285
                                               $errMessage = $proc->getErrorMessage(intval($i));
286
                                               echo 'Expected error: Code='.$errCode.' Message='.$errMessage;
287
                                           }
288
                                                $proc->exceptionClear();        
289
                                        }
290
                
291
                
292
                }                
293
                echo $result;            
294
                   $proc->clearParameters();
295
                $proc->clearProperties();
296
            
297
            }   
298
            
299
            
300
            $foo_xml = "xml/foo.xml";
301
            $foo_xsl = "xsl/foo.xsl";
302
            $baz_xml = "xml/baz.xml";
303
            $baz_xsl = "xsl/baz.xsl";
304
            $foo2_xsl = "xsl/foo2.xsl";
305
            $foo3_xsl = "xsl/foo3.xsl";
306
            $err_xsl = "xsl/err.xsl";            
307
            $err1_xsl = "xsl/err1.xsl";
308
            $text_xsl = "xsl/text.xsl";
309
            $cities_xml = "xml/cities.xml";
310
            $embedded_xml = "xml/embedded.xml";
311
            $multidoc_xsl = "xsl/multidoc.xsl";
312
            $identity_xsl = "xsl/identity.xsl"; 
313
            
314
            $saxonProc = new Saxon\SaxonProcessor();
315
            $proc = $saxonProc->newXsltProcessor();        
316
            $version = $saxonProc->version();
317
            echo 'Saxon Processor version: '.$version;
318
            echo '<br/>';        
319
            exampleSimple1($proc, $foo_xml, $foo_xsl);
320
            echo '<br/>';
321
            exampleSimple2($proc, "xml/foo.xml", $foo_xsl);
322
            echo '<br/>';            
323
            exampleSimple3($saxonProc, $proc);
324
            echo '<br/>';
325
            exampleLoopVar($saxonProc, $proc, $foo_xml, $foo_xsl);
326
            exampleParam($saxonProc, $proc, $foo_xml, $foo_xsl);
327
            exampleError1($proc, $foo_xml, $err_xsl);
328
            echo '<br/>'; 
329
            exampleError2($proc, $foo_xml, $err1_xsl);
330
            echo '<br/>';
331
            exampleXMLFilterChain($proc, $foo_xml, $foo_xsl, $foo2_xsl, $foo3_xsl);
332
            echo '<br/>';                    
333
            exampleXMLFilterChain2($saxonProc, $proc, $foo_xml, $foo_xsl, $foo2_xsl, $foo3_xsl);          
334
            echo '<br/>';  
335

    
336
            
337
            unset($proc);
338
            unset($saxonproc);
339
        
340
        
341
        ?>
342
    </body>
343
</html>
(14-14/14)