Project

Profile

Help

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

he / latest9.8 / hec / samples / php / xqueryExamples.php @ 74287d98

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
                $proc->setContextItemFromFile($xmlfile);
12
                $proc->setQueryFile($queryFile);
13
                  //$proc->setProperty('base', '/');      
14
                $result = $proc->runQueryToString();               
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, $xqueryProc){
27
                $sourceNode = $proc->parseXmlFromString("<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();
31
                        if($str!=null) {
32
                                echo "XdmValue:".$str;
33
                        } */
34
                        $xqueryProc->setContextItem($sourceNode);
35
                } else {
36
                        echo "Xdmvalue is null";
37
                }
38
                $xqueryProc->setQueryContent("declare default element namespace 'http://one.uri/'; /foo");
39
                $result = $xqueryProc->runQueryToString();
40
                echo '<b>exampleSimple2:</b><br/>';                
41
                if($result != null) {               
42
                  echo 'Output:'.$result;
43
                } else {
44
                        echo "Result is null";
45
                }
46
                       $xqueryProc->clearParameters();
47
                $xqueryProc->clearProperties();
48
            }
49

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

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

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

    
69
                $value1 = $saxonProc->createAtomicValue(true);
70

    
71
                $queryProc->setParameter("p",$value1);
72
                $resultVal = $queryProc->runQueryToValue();
73

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

    
95
            }
96

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

    
119
            unset($xqueryProc);            
120
            unset($proc);
121
        
122
        
123
        ?>
124
    </body>
125
</html>
(12-12/14)