Project

Profile

Help

Revision aff640e6

Added by O'Neil Delpratt about 6 years ago

Update samples to the 0.3.1 version

View differences:

latest9.6/hec/samples/xqueryExamples.php
9 9
            /* simple example to show transforming to string */
10 10
            function exampleSimple1($proc, $xmlfile, $queryFile){
11 11
                $proc->setSourceFile($xmlfile);
12
                $proc->importXQueryFile($queryFile);
13
  	              
12
                $proc->setQueryFile($queryFile);
13
  	        //$proc->setProperty('base', '/');      
14 14
                $result = $proc->queryToString();               
15 15
		if($result != null) {               
16 16
		echo '<b>exampleSimple1:</b><br/>';		
......
23 23
            }
24 24
            
25 25
            /* simple example to show transforming to file */
26
            function exampleSimple2($proc, $xmlFile, $xslFile){
27
                $proc->setSourceFile($xmlFile);
28
                $proc->importStylesheetFile($xslFile);
29
                $filename = "out/output1.xml";
30
                $proc->transformToFile($filename);
31
		echo '<b>exampleSimple2:</b><br/>';		
32
		if (file_exists($filename)) {
33
		    echo "The file $filename exists";
34
		} else {
35
		    echo "The file $filename does not exist";
36
		}
37
       		$proc->clearParameters();
38
		$proc->clearProperties();
39
            }
40
            /* simple example to show importing a document as string and stylesheet as a string */
41
            function exampleSimple3($proc){
42
		$proc->clearParameters();
43
		$proc->clearProperties();
44
                $xdmNode = $proc->parseString("<doc><b>text value of out</b></doc>");
45
		if($xdmNode == null) {
46
			echo 'xdmNode is null';
47
			return;	
48
		}            
49
		$proc->setSourceValue($xdmNode);
50
                $proc->importStylesheetString("<xsl:stylesheet xmlns:xsl='http://www.w3.org/1999/XSL/Transform' version='2.0'>
51
					    	<xsl:template match='/'>
52
					    	    <xsl:copy-of select='.'/>
53
					    	</xsl:template>
54
					    </xsl:stylesheet>");
55

  
56
                $result = $proc->transformToString();
57
		echo '<b>exampleSimple3</b>:<br/>';
58
		if($result != null) {             		
59
		echo 'Output:'.$result;
60
		} else {
61
			echo "Result is null";
62
		}       
63
		$proc->clearParameters();
64
		$proc->clearProperties();            
65
            }
66

  
67

  
68
           
69
           
70
            function exampleParam($proc, $xmlFile, $xslFile){
71
                echo '<b>ExampleParam:</b><br/>';
72
                $proc->setSourceFile($xmlFile);
73
                $proc->importStylesheetFile($xslFile);
74
		$xdmvalue = $proc->createXdmValue(strval("Hello to you"));
75
		if($xdmvalue !=null){
76
			echo "Name of Class " , get_class($xdmvalue) , "\n"; 			
77
			/*$str = $xdmvalue->getStringValue();
26
            function exampleSimple2($proc){
27
		$sourceNode = $proc->parseString("<foo xmlns='http://one.uri/'><bar><b>text node in example</b></bar></foo>");
28
		if($sourceNode !=null){
29
			/*echo "Name of Class " , get_class($sourceNode) , "\n"; 			
30
			$str = $xdmvalue->getStringValue();
78 31
			if($str!=null) {
79 32
				echo "XdmValue:".$str;
80 33
			} */
81
			$proc->setParameter("", 'a-param', $xdmvalue);
34
			$proc->setSourceValue($sourceNode);
82 35
		} else {
83 36
			echo "Xdmvalue is null";
84 37
		}
85
                $result = $proc->transformToString();
86
		if($result != null) {                
87
			echo 'Output:'.$result."<br/>";
38
                $proc->setQueryContent("declare default element namespace 'http://one.uri/'; /foo");
39
                $result = $proc->queryToString();
40
		echo '<b>exampleSimple2:</b><br/>';		
41
		if($result != null) {               
42
		  echo 'Output:'.$result;
88 43
		} else {
89
			echo "Result is NULL<br/>";
44
			echo "Result is null";
90 45
		}
91
                //$par1 = $proc->getParameter('a-param'); //TODO there is a bug in this method
92
                if($par1 != NULL) {
93
                    echo 'Parameter exists';
94
                }
95
                //unset($par1);
96
                $proc->clearParameters();                
97
                //unset($result);
98
                echo 'again with a no parameter value<br/>';
99
		$proc->setProperty('!indent', 'yes'); //Serialization property indicated with a '!' symbol
100
                $result = $proc->transformToString();
101
                /*$prop1 = $proc->getProperty('!indent'); //TODO there is a bug in this method
102
                if($prop1 != NULL) {
103
                    echo 'getProperty = '.$prop1;
104
                }*/
105
                $proc->clearProperties();
106
                echo $result;
107
                echo '<br/>';
108
              //  unset($result);
109
                echo 'again with no parameter and no properties value set<br/>';
110
                $xdmvalue = $proc->createXdmValue(strval("goodbye to you"));
111
		$proc->setParameter('a-param', $xdmvalue);
112
                $result = $proc->transformToString();                
113
                echo $result;
114
		$proc->clearParameters();
115
		$proc->clearProperties(); 
116
                        
117
            }
118

  
119

  
120
            function exampleXMLFilterChain($proc, $xmlFile, $xsl1File, $xsl2File, $xsl3File){
121
                echo '<b>XML Filter Chain using setSource</b><br/>';                
122
                $proc->setSourceFile($xmlFile);
123
                $proc->importStylesheetFile($xsl1File);
124
                $xdmValue1 = $proc->transformToValue();
125
                
126
                $proc->importStylesheetFile($xsl2File);
127
                $proc->setSourceValue($xdmValue1);
128
                unset($xdmValue1);
129
                $xdmValue1 = $proc->transformToValue();
130
                
131
                $proc->importStylesheetFile($xsl3File);                
132
                $proc->setSourceValue($xdmValue1);
133
                $result = $proc->transformToString();
134
		if($result != null) {
135
                	echo 'Output:'.$result;        
136
		} else {
137
			echo 'Result is null';
138
				    $errCount = $proc->getExceptionCount();
139
				    if($errCount > 0 ){ 
140
				        for($i = 0; $i < $errCount; $i++) {
141
					       $errCode = $proc->getErrorCode(intval($i));
142
					       $errMessage = $proc->getErrorMessage(intval($i));
143
					       echo 'Expected error: Code='.$errCode.' Message='.$errMessage;
144
					   }
145
						$proc->exceptionClear();	
146
					}
147
		}                      
148
		$proc->clearParameters();
46
       		$proc->clearParameters();
149 47
		$proc->clearProperties();
150 48
            }
151
            
152
            function exampleXMLFilterChain2($proc, $xmlFile, $xsl1File, $xsl2File, $xsl3File){
153
                echo '<b>XML Filter Chain using Parameters</b><br/>';                
154
                $xdmNode = $proc->parseFile($xmlFile);
155
                $proc->setParameter('s', $xdmNode);
156
                $proc->importStylesheetFile($xsl1File);
157
                $xdmValue1 = $proc->transformToValue();
158
                $proc->clearParameters();
159
                
160
                $proc->importStylesheetFile($xsl2File);
161
                $proc->setParameter('s',$xdmValue1);
162
                $xdmValue1 = $proc->transformToValue();
163
                $proc->clearParameters();
164
                
165
                $proc->importStylesheetFile($xsl3File);                
166
                $proc->setParameter('s', $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 49

  
186
            /* simple example to detect and handle errors from a transformation */
187
            function exampleError1($proc, $xmlfile, $xslFile){
188
		echo '<b>exampleError1:</b><br/>';
189
                $proc->setSourceFile($xmlFile);
190
                $proc->importStylesheetFile($xslFile);
191
                
192
                $result = $proc->transformToString();
193
                
194
                if($result == NULL) {
195
                    $errCount = $proc->getExceptionCount();
196
				    if($errCount > 0 ){ 
197
				        for($i = 0; $i < $errCount; $i++) {
198
					       $errCode = $proc->getErrorCode(intval($i));
199
					       $errMessage = $proc->getErrorMessage(intval($i));
200
					       echo 'Expected error: Code='.$errCode.' Message='.$errMessage;
201
					   }
202
						$proc->exceptionClear();	
203
					}
204
                
205
                
206
                }                
207
                echo $result;
208
            	$proc->clearParameters();
209
		$proc->clearProperties();
210
            
211
            }   
212

  
213

  
214
            /* simple example to test transforming without an stylesheet */
215
            function exampleError2($proc, $xmlfile, $xslFile){
216
		echo '<b>exampleError2:</b><br/>';
217
                $proc->setSourceFile($xmlFile);
218
                $proc->importStylesheetFile($xslFile);
219
                
220
                $result = $proc->transformToString();
221
                
222
                if($result == NULL) {
223
                    $errCount = $proc->getExceptionCount();
224
				    if($errCount > 0 ){ 
225
				        for($i = 0; $i < $errCount; $i++) {
226
					       $errCode = $proc->getErrorCode(intval($i));
227
					       $errMessage = $proc->getErrorMessage(intval($i));
228
					       echo 'Expected error: Code='.$errCode.' Message='.$errMessage;
229
					   }
230
						$proc->exceptionClear();	
231
					}
232
                
233
                
234
                }                
235
                echo $result;            
236
           	$proc->clearParameters();
237
		$proc->clearProperties();
238
            
239
            }   
240 50
            
241 51
            
242
            $books_xml = "xml/books.xml";
52
            $books_xml = "query/books.xml";
243 53
            $books_to_html_xq = "query/books-to-html.xq";
244 54
            $baz_xml = "xml/baz.xml";
245 55
            $baz_xsl = "xsl/baz.xsl";
......
261 71
            echo 'Saxon Processor version: '.$version;
262 72
            echo '<br/>';        
263 73
            exampleSimple1($proc, $books_xml, $books_to_html_xq);
264
/*            echo '<br/>';
265
            exampleSimple2($proc, "xml/foo.xml", $foo_xsl);
266
            echo '<br/>';            
267
            exampleSimple3($proc);
268
            echo '<br/>';
269
            exampleParam($proc, $foo_xml, $foo_xsl);
270
            exampleError1($proc, $foo_xml, $err_xsl);
271
            echo '<br/>'; 
272
	    exampleError2($proc, $foo_xml, $err1_xsl);
273 74
            echo '<br/>';
274
            exampleXMLFilterChain($proc, $foo_xml, $foo_xsl, $foo2_xsl, $foo3_xsl);
275
            echo '<br/>';                    
276
            exampleXMLFilterChain2($proc, $foo_xml, $foo_xsl, $foo2_xsl, $foo3_xsl);          
277
            echo '<br/>'; */ 
278

  
279
//	    $proc->close();            
75
            exampleSimple2($proc);
76
            echo '<br/>';            
77
           
280 78
            unset($proc);
281 79
	
282 80
        

Also available in: Unified diff