Project

Profile

Help

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

he / latest9.6 / hec / samples / xsltExamples.php @ 74569bc0

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->setSourceFile($xmlfile);
12
                $proc->setStylesheetFile($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

    
26
            function testCode($proc, $xmlFile, $xslFile){
27
                $proc->setSourceFile($xmlFile);
28
                $proc->setStylesheetFile($xslFile);
29
                $filename = "out/output1.xml";
30
                $result = $proc->transformToString();
31
                if($result != null) {               
32
                echo '<b>exampleSimple1:</b><br/>';                
33
                echo 'Output:'.$result;
34
                } else {
35
                        echo "Result is null";
36
                }
37
                       $proc->clearParameters();
38
                $proc->clearProperties();
39
            }
40
            
41
            /* simple example to show transforming to file */
42
            function exampleSimple2($proc, $xmlFile, $xslFile){
43
                $proc->setSourceFile($xmlFile);
44
                $proc->setStylesheetFile($xslFile);
45
                $filename = "out/output1.xml";
46
                $proc->transformToFile($filename);
47
                echo '<b>exampleSimple2:</b><br/>';                
48
                if (file_exists('/var/www/trax/'.$filename)) {
49
                    echo "The file $filename exists";
50
                } else {
51
                    echo "The file $filename does not exist";
52
                }
53
                       $proc->clearParameters();
54
                $proc->clearProperties();
55
            }
56
            /* simple example to show importing a document as string and stylesheet as a string */
57
            function exampleSimple3($proc){
58
                $proc->clearParameters();
59
                $proc->clearProperties();
60
                $xdmNode = $proc->parseString("<doc><b>text value of out</b></doc>");
61
                if($xdmNode == null) {
62
                        echo 'xdmNode is null';
63
                        return;        
64
                }          
65
                $proc->setSourceValue($xdmNode);                  
66
                $proc->setStylesheetContent("<xsl:stylesheet xmlns:xsl='http://www.w3.org/1999/XSL/Transform' version='2.0'>
67
                                                    <xsl:template match='/'>
68
                                                        <xsl:copy-of select='.'/>
69
                                                    </xsl:template>
70
                                            </xsl:stylesheet>");
71

    
72
                $result = $proc->transformToString();
73
                echo '<b>exampleSimple3</b>:<br/>';
74
                if($result != null) {                             
75
                echo 'Output:'.$result;
76
                } else {
77
                        echo "Result is null";
78
                }       
79
                $proc->clearParameters();
80
                $proc->clearProperties();            
81
            }
82

    
83
        function exampleOfXdmValuesInLoop($proc, $xml, $xslt) {
84
          echo '<b>Example of building parameters in a foreach loop:</b><br/>';
85
        $params = array(
86
          "testparam1" => "testvalue1",
87
          "testparam2" => "testvalue2",
88
          "testparam3" => "testvalue3",
89
        );
90

    
91
        $proc->clearParameters();
92
        $proc->clearProperties();
93
        $proc->setSourceFile($xml);
94
        $proc->setStylesheetFile($xslt);
95
        foreach($params as $name => $value) {
96
                echo 'name:'.$name.' value:'.$value.'<br/>';
97
                $xdmValue = $proc->createXdmValue(strval($value));
98
                if($xdmValue !=null) {
99
                    echo "Name of Class " , get_class($xdmValue) , "\n<br/>";
100
                    $proc->setParameter($name, $xdmValue);
101
                } else {
102
                    echo "Xdmvalue is null<br/>";
103
                  }
104
        }        
105

    
106
        $result = $proc->transformToString();
107
        if($result != null) {
108
          echo 'Output:'.$result;
109
        } else {
110
          echo "Result is null";
111
        }
112
        echo "<br/>";
113

    
114
        }
115
           
116
           
117
            function exampleParam($proc, $xmlFile, $xslFile){
118
                echo '<b>Example of Param usage:</b><br/>';
119
                $proc->setSourceFile($xmlFile);
120
                $proc->setStylesheetFile($xslFile);
121

    
122

    
123
                $xdmvalue = $proc->createXdmValue(strval("Hello to you"));
124
                if($xdmvalue !=null){
125
                        echo "Class " , get_class($xdmvalue) , " created \n";                         
126
                                                
127
                        $str = $xdmvalue->getStringValue();
128
                        if($str!=null) {
129
                                echo "XdmValue:".$str;
130
                        }
131
                        
132
                        $proc->setParameter("", 'a-param', $xdmvalue);
133
                } else {
134
                        echo "Xdmvalue is null";
135
                }
136
                $result = $proc->transformToString();
137
                if($result != null) {                
138
                        echo '<br/>Output:'.$result."<br/>";
139
                } else {
140
                        echo "Result is NULL<br/>";
141
                }
142
                $par1 = $proc->getParameter('a-param'); 
143
                if($par1 != NULL) {
144
                    echo 'Parameter exists';
145
                }
146
                unset($par1);
147
                $proc->clearParameters();                
148
                //unset($result);
149
                echo 'again with a no parameter value<br/>';
150
                //$proc->setProperty('!indent', 'yes'); //Serialization property indicated with a '!' symbol
151
                $result = $proc->transformToString(); 
152
                /////                
153
                /*$prop1 = $proc->getProperty('!indent'); //TODO there is a bug in this method
154
                if($prop1 != NULL) {
155
                    echo 'getProperty = '.$prop1;
156
                }*/
157
                ///
158
                $proc->clearProperties();
159
                echo $result;
160
                echo '<br/>';
161
              //  unset($result);
162
                echo 'again with no parameter and no properties value set<br/>';
163
                $xdmvalue = $proc->createXdmValue(strval("goodbye to you"));
164
                $proc->setParameter('a-param', $xdmvalue);
165
                $result = $proc->transformToString();                
166
                echo $result;
167
                $proc->clearParameters();
168
                $proc->clearProperties(); 
169
                        
170
            }
171

    
172

    
173
            function exampleXMLFilterChain($proc, $xmlFile, $xsl1File, $xsl2File, $xsl3File){
174
                echo '<b>XML Filter Chain using setSource</b><br/>';                
175
                $proc->setSourceFile($xmlFile);
176
                $proc->setStylesheetFile($xsl1File);
177
                $xdmValue1 = $proc->transformToValue();
178
                
179
                $proc->setStylesheetFile($xsl2File);
180
                $proc->setSourceValue($xdmValue1);
181
                unset($xdmValue1);
182
                $xdmValue1 = $proc->transformToValue();
183
                
184
                $proc->setStylesheetFile($xsl3File);                
185
                $proc->setSourceValue($xdmValue1);
186
                $result = $proc->transformToString();
187
                if($result != null) {
188
                        echo 'Output:'.$result;        
189
                } else {
190
                        echo 'Result is null';
191
                                    $errCount = $proc->getExceptionCount();
192
                                    if($errCount > 0 ){ 
193
                                        for($i = 0; $i < $errCount; $i++) {
194
                                               $errCode = $proc->getErrorCode(intval($i));
195
                                               $errMessage = $proc->getErrorMessage(intval($i));
196
                                               echo 'Expected error: Code='.$errCode.' Message='.$errMessage;
197
                                           }
198
                                                $proc->exceptionClear();        
199
                                        }
200
                }                      
201
                $proc->clearParameters();
202
                $proc->clearProperties();
203
            }
204
            
205
            function exampleXMLFilterChain2($proc, $xmlFile, $xsl1File, $xsl2File, $xsl3File){
206
                echo '<b>XML Filter Chain using Parameters</b><br/>';                
207
                $xdmNode = $proc->parseFile($xmlFile);
208
                $proc->setParameter('s', $xdmNode);
209
                $proc->setStylesheetFile($xsl1File);
210
                $xdmValue1 = $proc->transformToValue();
211
                $proc->clearParameters();
212
                $proc->clearProperties();
213
                $proc->setStylesheetFile($xsl2File);
214
                $proc->setParameter('s',$xdmValue1);
215
                $xdmValue1 = $proc->transformToValue();
216
                $proc->clearParameters();
217
                
218
                $proc->setStylesheetFile($xsl3File);                
219
                $proc->setParameter('s', $xdmValue1);
220
                $result = $proc->transformToString();
221
                if($result != null) {
222
                        echo 'Output:'.$result;        
223
                } else {
224
                        echo 'Result is null';
225
                                    $errCount = $proc->getExceptionCount();
226
                                    if($errCount > 0 ){ 
227
                                        for($i = 0; $i < $errCount; $i++) {
228
                                               $errCode = $proc->getErrorCode(intval($i));
229
                                               $errMessage = $proc->getErrorMessage(intval($i));
230
                                               echo 'Expected error: Code='.$errCode.' Message='.$errMessage;
231
                                           }
232
                                                $proc->exceptionClear();        
233
                                        }
234
                }        
235
                $proc->clearParameters();
236
                $proc->clearProperties();
237
            }            
238

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

    
266

    
267
            /* simple example to test transforming without an stylesheet */
268
            function exampleError2($proc, $xmlfile, $xslFile){
269
                echo '<b>exampleError2:</b><br/>';
270
                $proc->setSourceFile($xmlFile);
271
                $proc->setStylesheetFile($xslFile);
272
                
273
                $result = $proc->transformToString();
274
                
275
                if($result == NULL) {
276
                    $errCount = $proc->getExceptionCount();
277
                                    if($errCount > 0 ){ 
278
                                        for($i = 0; $i < $errCount; $i++) {
279
                                               $errCode = $proc->getErrorCode(intval($i));
280
                                               $errMessage = $proc->getErrorMessage(intval($i));
281
                                               echo 'Expected error: Code='.$errCode.' Message='.$errMessage;
282
                                           }
283
                                                $proc->exceptionClear();        
284
                                        }
285
                
286
                
287
                }                
288
                echo $result;            
289
                   $proc->clearParameters();
290
                $proc->clearProperties();
291
            
292
            }   
293
            
294
            
295
            $foo_xml = "xml/foo.xml";
296
            $foo_xsl = "xsl/foo.xsl";
297
            $baz_xml = "xml/baz.xml";
298
            $baz_xsl = "xsl/baz.xsl";
299
            $foo2_xsl = "xsl/foo2.xsl";
300
            $foo3_xsl = "xsl/foo3.xsl";
301
            $err_xsl = "xsl/err.xsl";            
302
            $err1_xsl = "xsl/err1.xsl";
303
            $text_xsl = "xsl/text.xsl";
304
            $cities_xml = "xml/cities.xml";
305
            $embedded_xml = "xml/embedded.xml";
306
            $multidoc_xsl = "xsl/multidoc.xsl";
307
            $identity_xsl = "xsl/identity.xsl"; 
308
            
309
            $proc = new SaxonProcessor();
310
                
311
            $version = $proc->version();
312
            echo 'Saxon Processor version: '.$version;
313
            echo '<br/>';        
314

    
315
            exampleSimple1($proc, $foo_xml, $foo_xsl);
316
            echo '<br/>';
317
            exampleSimple2($proc, "xml/foo.xml", $foo_xsl);
318
            echo '<br/>';            
319
            exampleSimple3($proc);
320
            echo '<br/>';
321
            exampleOfXdmValuesInLoop($proc, $foo_xml, $foo_xsl);
322
          //  exampleParam($proc, $foo_xml, $foo_xsl);
323
          //  exampleError1($proc, $foo_xml, $err_xsl);
324
            echo '<br/>'; 
325
        //    exampleError2($proc, $foo_xml, $err1_xsl);
326
            echo '<br/>';
327
  //          exampleXMLFilterChain($proc, $foo_xml, $foo_xsl, $foo2_xsl, $foo3_xsl);
328
            echo '<br/>';                    
329
//            exampleXMLFilterChain2($proc, $foo_xml, $foo_xsl, $foo2_xsl, $foo3_xsl);          
330
            echo '<br/>';  
331

    
332
//            $proc->close();            
333
            unset($proc);
334
        
335
        
336
        ?>
337
    </body>
338
</html>
(5-5/5)