Project

Profile

Help

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

he / latest9.6 / hec / samples / cppTests / testXPath.cpp @ 2a9719f6

1
#include "../../Saxon.C.API/SaxonProcessor.h"
2
#include "../../Saxon.C.API/XdmValue.h"
3
#include "../../Saxon.C.API/XdmItem.h"
4
#include "../../Saxon.C.API/XdmNode.h"
5
#include <string>
6

    
7
using namespace std;
8

    
9

    
10

    
11
// Test case on the evaluate method in XPathProcessor. Here we test that we have morethan one XdmItem.
12
void testXPathSingle(SaxonProcessor * processor, XPathProcessor * xpath){
13

    
14
       cout<<endl<<"Test testXPathSingle:"<<endl; 
15
      XdmNode * input = processor->parseXmlFromString("<out><person>text1</person><person>text2</person><person>text3</person></out>");
16

    
17
        xpath->setContextItem((XdmItem *)input);
18
        XdmItem * result = xpath->evaluateSingle("//person[1]");
19
        
20
        if(result == NULL) {
21
                 printf("result is null \n");
22
        } else {
23
                cout<<"Number of items="<<result->size()<<endl;
24
                cout<<"String Value of result="<<result->getStringValue(processor)<<endl;
25
        }
26
        delete result;
27

    
28
        
29

    
30
}
31

    
32
// Test case on the evaluate method in XPathProcessor. Here we test that we have morethan one XdmItem.
33
void testXPathValues(SaxonProcessor * processor, XPathProcessor * xpath){
34
     cout<<endl<<"Test testXPathValues:"<<endl; 
35
          xpath->clearParameters(true);
36
          xpath->clearProperties();
37
         XdmNode * input = processor->parseXmlFromString("<out><person>text1</person><person>text2</person><person1>text3</person1></out>");
38
        if(input == NULL) {return;}
39
        XdmNode ** children = input->getChildren();
40
        XdmNode ** children0 = children[0]->getChildren();
41
        int num = children[0]->getChildCount();
42
        delete []children;
43
        cout<<"number of children: "<<num<<endl;
44
        for(int i=0; i<num;i++) {
45
                cout<<"node name:"<<children0[i]->getNodeName()<<endl;
46
        }
47
        delete [] children0;
48

    
49
        xpath->setContextItem((XdmItem *)input);
50
        XdmValue * resultValues = xpath->evaluate("//person");
51
        
52
        if(resultValues == NULL) {
53
                 printf("result is null \n");
54
        } else {
55
                cout<<"Number of items="<<resultValues->size()<<endl;
56
                for(int i =0; i< resultValues->size();i++){
57
                        XdmItem * itemi = resultValues->itemAt(i);
58
                        if(itemi == NULL) {
59
                                cout<<"Item at position "<<i<<" should not be null"<<endl;
60
                                break;
61
                        }
62
                        cout<<"Item at "<<i<<" ="<<itemi->getStringValue(processor)<<endl;                
63
                }
64
        }
65
        delete resultValues;
66
        
67

    
68
}
69

    
70
// Test case on the evaluate method in XPathProcessor. Here we test that we have morethan one XdmItem.
71
void testXPathAttrValues(SaxonProcessor * processor, XPathProcessor * xpath){
72
     cout<<endl<<"Test testXPathValues1:"<<endl; 
73
          xpath->clearParameters(true);
74
          xpath->clearProperties();
75
         XdmNode * input = processor->parseXmlFromString("<out attr='valuex'><person attr1='value1' attr2='value2'>text1</person><person>text2</person><person1>text3</person1></out>");
76
        if(input == NULL) {return;}
77

    
78
        //cout<<"Name of attr1= "<<input->getAttributeValue("attr")<<endl;
79

    
80

    
81
        xpath->setContextItem((XdmItem *)input);
82
        XdmItem * result = xpath->evaluateSingle("(//person)[1]");
83
        
84
        if(result == NULL) {
85
                 printf("result is null \n");
86
        } else {
87
                XdmNode *nodeValue = (XdmNode*)result;
88
                cout<<"Attribute Count= "<<nodeValue->getAttributeCount()<<endl;
89
                const char *attrVal = nodeValue->getAttributeValue("attr1");
90
                if(attrVal != NULL) {
91
                        cout<<"Attribute value= "<<attrVal<<endl;
92
                }
93
                
94
                XdmNode ** attrNodes = nodeValue->getAttributeNodes();
95
                if(attrNodes == NULL) { return;}
96
        
97
                const char *name1 = attrNodes[0]->getNodeName();
98
                if(name1 != NULL) {
99
                        cout<<"Name of attr1= "<<name1<<endl;
100
                }
101
                XdmNode * parent = attrNodes[0]->getParent();
102
                if(parent != NULL) {
103
                        cout<<"Name of parent= "<<parent->getNodeName()<<endl;
104
                }
105
                XdmNode * parent1 = parent->getParent();
106
                if(parent1 != NULL) {
107
                        cout<<"Name of parent= "<<parent1->getNodeName()<<endl;
108
                }
109
        //TODO test if attr value is not there
110
        }
111
        
112
        
113

    
114
}
115

    
116
// Test case on the evaluate method in XPathProcessor. Here we test that we have morethan one XdmItem.
117
void testXPathOnFile(SaxonProcessor * processor, XPathProcessor * xpath){
118
             cout<<endl<<"Test testXPath with file source:"<<endl;
119
          xpath->clearParameters(true);
120
          xpath->clearProperties(); 
121
        xpath->setContextFile("cat.xml");
122

    
123
        XdmValue * resultValues = xpath->evaluate("//person");
124
        
125
        if(resultValues == NULL) {
126
                 printf("result is null \n");
127
        } else {
128
                cout<<"Number of items="<<resultValues->size()<<endl;
129
                for(int i =0; i< resultValues->size();i++){
130
                        XdmItem * itemi = resultValues->itemAt(i);
131
                        if(itemi == NULL) {
132
                                cout<<"Item at position "<<i<<" should not be null"<<endl;
133
                                break;
134
                        }
135
                        cout<<"Item at "<<i<<" ="<<itemi->getStringValue(processor)<<endl;                
136
                }
137
        }
138
        delete resultValues;
139
        
140

    
141
}
142

    
143
// Test case on the evaluate method in XPathProcessor. Here we test that we have morethan one XdmItem.
144
void testXPathOnFile2(SaxonProcessor * processor, XPathProcessor * xpath){
145
             cout<<endl<<"Test testXPath with file source:"<<endl;
146
          xpath->clearParameters(true);
147
          xpath->clearProperties(); 
148
        xpath->setContextFile("cat.xml");
149

    
150
        XdmItem * result = xpath->evaluateSingle("//person[1]");
151
        
152
        if(result == NULL) {
153
                 printf("result is null \n");
154
        } else {
155
                cout<<"Number of items="<<result->size()<<endl;
156
                if(result->isAtomic()) {
157
                        cout<<"XdmItem is atomic - Error"<<endl;
158
                } else {
159
                        cout<<"XdmItem is not atomic"<<endl;
160
                }
161
                XdmNode *node = (XdmNode*)result;
162
                if(node->getNodeKind() == ELEMENT){
163
                   cout<<"Result is a ELEMENT"<<endl;
164
                   cout<<"Node name: "<<node->getNodeName()<<endl;
165
                } else {
166
                        cout<<"Node is of kind:"<<node->getNodeKind()<<endl;
167
                }
168
                const char * baseURI = node->getBaseUri();
169
                if(baseURI != NULL) {
170
                        cout<<"baseURI of node: "<<baseURI<<endl;
171
                }
172
        }
173
        delete result;
174
        
175

    
176
}
177

    
178
int main()
179
{
180

    
181
    SaxonProcessor * processor = new SaxonProcessor(false);
182
    XPathProcessor * xpathProc = processor->newXPathProcessor();
183

    
184
    cout<<endl<<"Test: XPathProcessor with Saxon version="<<processor->version()<<endl; 
185
    testXPathSingle(processor, xpathProc);   
186
    testXPathValues(processor, xpathProc);
187
    testXPathAttrValues(processor, xpathProc);
188
    testXPathOnFile(processor, xpathProc);
189
    testXPathOnFile2(processor, xpathProc);
190

    
191

    
192
     processor->release();
193
    return 0;
194
}
(11-11/13)