Project

Profile

Help

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

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

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

    
6
using namespace std;
7

    
8
void testValidator1(SaxonProcessor * processor, SchemaValidator * val){
9
        cout<<endl<<"Test Validate Schema from string"<<endl;        
10
 string invalid_xml = "<?xml version='1.0'?><request><a/><!--comment--></request>";
11
 const char* sch1 = "<xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema' elementFormDefault='qualified' attributeFormDefault='unqualified'><xs:element name='request'><xs:complexType><xs:sequence><xs:element name='a' type='xs:string'/><xs:element name='b' type='xs:string'/></xs:sequence><xs:assert test='count(child::node()) = 3'/></xs:complexType></xs:element></xs:schema>";
12

    
13

    
14
        string doc1 = "<Family xmlns='http://myexample/family'><Parent>John</Parent><Child>Alice</Child></Family>";
15
        
16
        val->registerSchemaFromString(sch1);
17

    
18
}
19

    
20
void testValidator2(){
21
  SaxonProcessor * processor = new SaxonProcessor(true);
22
  processor->setConfigurationProperty("xsdversion", "1.1");
23
  SchemaValidator * val = processor->newSchemaValidator();
24

    
25
  cout<<endl<<"Test 2: Validate Schema from string"<<endl;        
26
  string invalid_xml = "<?xml version='1.0'?><request><a/><!--comment--></request>";
27
  const char* sch1 = "<xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema' elementFormDefault='qualified' attributeFormDefault='unqualified'><xs:element name='request'><xs:complexType><xs:sequence><xs:element name='a' type='xs:string'/><xs:element name='b' type='xs:string'/></xs:sequence><xs:assert test='count(child::node()) = 3'/></xs:complexType></xs:element></xs:schema>";
28

    
29

    
30
        string doc1 = "<Family xmlns='http://myexample/family'><Parent>John</Parent><Child>Alice</Child></Family>";
31

    
32
         XdmNode * input = processor->parseXmlFromString(doc1.c_str());
33

    
34
        val->setSourceNode(input);
35

    
36
        
37
        
38
        val->registerSchemaFromString(sch1);
39

    
40
        val->validate();
41
        if(val->exceptionOccurred()) {
42
                cout<<endl<<"Doc is not valid!"<<endl;
43
        }
44

    
45
        delete val;
46

    
47
}
48

    
49
void testValidator3(SaxonProcessor * processor, SchemaValidator * val){
50
  processor->exceptionClear();
51
  val->clearParameters(true);
52
  val->clearProperties();
53
  cout<<endl<<"Test 3: Validate Schema from string"<<endl;        
54
  const char* sch1 = "<?xml version='1.0' encoding='UTF-8'?><schema targetNamespace='http://myexample/family' xmlns:fam='http://myexample/family' xmlns='http://www.w3.org/2001/XMLSchema'><element name='FamilyMember' type='string' /><element name='Parent' type='string' substitutionGroup='fam:FamilyMember'/><element name='Child' type='string' substitutionGroup='fam:FamilyMember'/><element name='Family'><complexType><sequence><element ref='fam:FamilyMember' maxOccurs='unbounded'/></sequence></complexType></element>  </schema>";
55
        
56
        val->registerSchemaFromString(sch1);
57

    
58
        val->validate("family.xml");
59
        if(!val->exceptionOccurred()) {
60
                cout<<endl<<"Doc1 is OK"<<endl;
61
        } else {
62
                cout<<endl<<"Error: Doc reported as invalid!"<<endl;
63
        }
64
}
65

    
66
void testValidator4(SaxonProcessor * processor, SchemaValidator * val){
67
  processor->exceptionClear();
68
  val->clearParameters(true);
69
  val->clearProperties();
70
  cout<<endl<<"Test 4: Validate source file with schema file. i.e. family.xml and family.xsd"<<endl;        
71

    
72
        val->registerSchemaFromFile("family-ext.xsd");
73
       //val->registerSchema("family.xsd");
74
        val->registerSchemaFromFile("family.xsd");
75
        val->validate("family.xml");
76
        if(!val->exceptionOccurred()) {
77
                cout<<endl<<"Doc1 is OK"<<endl;
78
        } else {
79
        cout<<endl<<"Error: Doc reported as valid!"<<endl;
80
        }
81

    
82
}
83

    
84
void testValidator5(SaxonProcessor * processor, SchemaValidator * val){
85
  processor->exceptionClear();
86
  val->clearParameters(true);
87
  val->clearProperties();
88
  cout<<endl<<"Test 5: Validate Schema from string"<<endl;        
89
  string invalid_xml = "<?xml version='1.0'?><request><a/><!--comment--></request>";
90
  const char* sch1 = "<xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema' elementFormDefault='qualified' attributeFormDefault='unqualified'><xs:element name='request'><xs:complexType><xs:sequence><xs:element name='a' type='xs:string'/><xs:element name='b' type='xs:string'/></xs:sequence><xs:assert test='count(child::node()) = 3'/></xs:complexType></xs:element></xs:schema>";
91

    
92

    
93
        string doc1 = "<request xmlns='http://myexample/family'><Parent>John</Parent><Child>Alice</Child></request>";
94

    
95
         XdmNode * input = processor->parseXmlFromString(doc1.c_str());
96

    
97
        //val->setSourceNode(input);
98

    
99
        //val->setProperty("string", doc1.c_str());
100
        val->setProperty("xsdversion", "1.1");
101
val->setParameter("node", (XdmValue *)input);
102
        val->registerSchemaFromString(sch1);
103
        val->setProperty("report-node", "true");
104
        //val->setProperty("report-file", "validation-report2.xml");        
105
        val->setProperty("verbose", "true");
106
        val->validate();
107
        XdmNode * node = val->getValidationReport(); 
108
        if(node != NULL) {
109
                cout<<endl<<"Validation Report"<<node->getStringValue()<<endl;
110
        } else {
111
                cout<<endl<<"Error: Validation Report is NULL"<<endl;
112
        }
113

    
114
        
115

    
116
}
117

    
118
int main()
119
{
120

    
121
        SaxonProcessor * processor = new SaxonProcessor(true);
122
       cout<<"Test: SchemaValidator with Saxon version="<<processor->version()<<endl<<endl;
123

    
124
        //processor->setConfigurationProperty("xsdversion", "1.1");
125
        
126
         SchemaValidator * validator = processor->newSchemaValidator();
127
        testValidator1(processor, validator);
128
        testValidator2();
129

    
130
        testValidator3(processor, validator);
131
        processor->setConfigurationProperty("http://saxon.sf.net/feature/multipleSchemaImports", "on");
132
        SchemaValidator * validator2 = processor->newSchemaValidator();
133
        testValidator4(processor, validator2);
134
        processor->setConfigurationProperty("xsdversion", "1.1");
135
        SchemaValidator * validator3 = processor->newSchemaValidator();
136
        testValidator5(processor, validator3);
137
        delete validator;
138
        //delete validator2;
139
        delete validator3;
140
        processor->release();
141
        return 0;
142
}
(10-10/13)