Project

Profile

Help

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

he / latest9.8 / hec / samples / php / xqueryExamples.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
            /* simple example to show transforming to string */
10
            function exampleSimple1($proc, $xmlfile, $queryFile){
11
                echo '<b>exampleSimple1:</b><br/>';
12
                $proc->setContextItemFromFile($xmlfile);
13
echo '<b>exampleSimple cp1:</b><br/>';
14
                $proc->setQueryFile($queryFile);
15
echo '<b>exampleSimple cp2:</b><br/>';
16
                  //$proc->setProperty('base', '/');      
17
                $result = $proc->runQueryToString();  
18
echo '<b>exampleSimple cp3:</b><br/>';             
19
                if($result != null) {               
20
                                
21
                echo 'Output:'.$result;
22
                } else {
23
                        echo "Result is null";
24
                }
25
                $proc->clearParameters();
26
                $proc->clearProperties();            
27
            }
28
            
29
            /* simple example to show transforming to file */
30
            function exampleSimple2($proc, $xqueryProc){
31
                $sourceNode = $proc->parseXmlFromString("<foo xmlns='http://one.uri/'><bar><b>text node in example</b></bar></foo>");
32
                if($sourceNode !=null){
33
                        /*echo "Name of Class " , get_class($sourceNode) , "\n";                         
34
                        $str = $sourceNode->getStringValue();
35
                        if($str!=null) {
36
                                echo "XdmValue:".$str;
37
                        } */
38
                        $xqueryProc->setContextItem($sourceNode);
39
                } else {
40
                        echo "Xdmvalue is null";
41
                }
42
                $xqueryProc->setQueryContent("declare default element namespace 'http://one.uri/'; /foo");
43
                $result = $xqueryProc->runQueryToString();
44
                echo '<b>exampleSimple2:</b><br/>';                
45
                if($result != null) {               
46
                  echo 'Output:'.$result;
47
                } else {
48
                        echo "Result is null";
49
                }
50
                       $xqueryProc->clearParameters();
51
                $xqueryProc->clearProperties();
52
            }
53

    
54
 /* Test that the XQuery compiler can compile two queries without interference */
55
            function exampleSimple3($saxonProc){
56
                echo '<b>exampleSimple3:</b><br/>';
57
                $queryProc = $saxonProc->newXQueryProcessor();
58
                //$queryProc2 = $saxonProc->newXQueryProcessor();
59

    
60
                $sourceNode = $saxonProc->parseXmlFromString("<foo xmlns='http://one.uri/'><bar xmlns='http://two.uri'>12</bar></foo>");
61
                if($sourceNode !=null){
62
                        
63
                        $queryProc->setContextItem($sourceNode);
64
                } else {
65
                        echo "Xdmvalue is null";
66
                }
67
                $queryProc->declareNamespace("", "http://one.uri/");
68
                $queryProc->setQueryContent("declare variable \$p as xs:boolean external; exists(/foo) = \$p");
69

    
70
                //$queryProc2->declareNamespace("", "http://two.uri");
71
                //$queryProc2->setQueryContent("declare variable \$p as xs:integer external; /*/bar + \$p");
72

    
73
                $value1 = $saxonProc->createAtomicValue(true);
74

    
75
                $queryProc->setParameter("p",$value1);
76
                $resultVal = $queryProc->runQueryToValue();
77

    
78
                if($resultVal != NULL ){
79
                        $itemi =  $resultVal->itemAt(0);
80
                        $isAtomic = $itemi->isAtomic();
81
                        if($isAtomic) {
82
                                $valueb = $itemi->getAtomicValue();
83
                                $resultBool = $valueb->getBooleanValue();
84
                                echo "Test3: Result is atomic, valueStr=".$valueb->getStringValue().", Bool=".strval($resultBool)."<br/>";
85
        
86
                        } else {
87
                                echo "Test3: Result is not atomic";
88
                        }
89
        
90
                        unset($itemi);
91
                } else {
92
                    echo "result value is null";
93
                }
94
                unset($value1);
95
                unset($sourceNode);
96
                //unset($queryProc2);
97
                unset($queryProc);
98

    
99
            }
100

    
101
            
102
            
103
            $books_xml = "query/books.xml";
104
            $books_to_html_xq = "query/books-to-html.xq";
105
            $baz_xml = "xml/baz.xml";
106
            $cities_xml = "xml/cities.xml";
107
            $embedded_xml = "xml/embedded.xml";
108
            
109
            $proc = new Saxon\SaxonProcessor();
110
            $xqueryProc = $proc->newXQueryProcessor();
111
                
112
            $version = $proc->version();
113
               echo '<b>PHP XQuery examples</b><br/>';
114
            echo 'Saxon Processor version: '.$version;
115
            echo '<br/>';        
116
            exampleSimple1($xqueryProc, $books_xml, $books_to_html_xq);
117
            echo '<br/>';
118
            exampleSimple2($proc, $xqueryProc);
119
            echo '<br/>';
120
            exampleSimple3($proc); //give seg error
121
            echo '<br/>';               
122

    
123
            unset($xqueryProc);            
124
            unset($proc);
125
        
126
        
127
        ?>
128
    </body>
129
</html>
(12-12/14)